Run-wide limits on what a program's language model calls spend: tokens, requests, tool
calls and cost, drawn from one Budget by every call under it::
limits = budget.Limits(tool_calls=9, total_tokens=50_000, max_output_tokens=1_000)
program.pipe(budget.limit(limits))The budget is in the fiber's context, so forked fibers, concurrent tool calls and agents
run inside a tool's handler draw from it too, whatever model they use. A nested
limit draws from its own budget and from every one around it.
Before a model request, every budget reserves the request's worst case (its estimate of
the prompt's tokens plus max_output_tokens, and what that would cost); the
response's usage settles it, and the unused part goes back. A request that fails or
is interrupted gives back its tokens and cost and still counts as a request. A tool call
is counted before its handler runs. A reservation that does not fit fails with
BudgetExceeded before the provider is called or the handler runs. Effect has no
counterpart of this module.
Types#
| Type | Definition | Description |
|---|---|---|
Resource | type Resource = Literal['requests', 'tool_calls', 'input_tokens', 'output_tokens', 'total_tokens', 'cost'] | What a budget limits. |
Classes#
Functions#
make#
make(limits) -> Effect[Budget]A new budget with nothing spent.
| Parameter | Type | Description |
|---|---|---|
limits | Limits |
Returns Effect[Budget]
def make(limits: Limits) -> Effect[Budget]:
"""A new budget with nothing spent."""
return fx.sync(lambda: Budget(limits))provide#
provide(budget) -> _ProvideEvery model request and tool call of an effect draws from budget, and from the
budgets already around it::
agent.pipe(budget.provide(shared))| Parameter | Type | Description |
|---|---|---|
budget | Budget |
Returns _Provide
def provide(budget: Budget) -> _Provide:
"""Every model request and tool call of an effect draws from ``budget``, and from the
budgets already around it::
agent.pipe(budget.provide(shared))
"""
return _Provide(fx.succeed(budget))limit#
limit(limits) -> _Provideprovide a new budget of limits each time the effect runs::
agent.pipe(budget.limit(budget.Limits(requests=20)))
| Parameter | Type | Description |
|---|---|---|
limits | Limits |
Returns _Provide
def limit(limits: Limits) -> _Provide:
"""``provide`` a new budget of ``limits`` each time the effect runs::
agent.pipe(budget.limit(budget.Limits(requests=20)))
"""
return _Provide(make(limits))estimate_input_tokens#
estimate_input_tokens(options) -> intA token per four characters of the prompt's text, tool calls, tool results and tool definitions, rounded up.
| Parameter | Type | Description |
|---|---|---|
options | ProviderOptions |
Returns int
def estimate_input_tokens(options: ProviderOptions) -> int:
"""A token per four characters of the prompt's text, tool calls, tool results and
tool definitions, rounded up."""
chars = sum(len(tool.name) + len(tool.description or "") for tool in options.tools)
for message in options.prompt.content:
if isinstance(message.content, str):
chars += len(message.content)
continue
for part in message.content:
match part:
case P.TextPart(text=text) | P.ReasoningPart(text=text):
chars += len(text)
case P.ToolCallPart(name=name, params=params):
chars += len(name) + len(repr(params))
case P.ToolResultPart(name=name, result=result):
chars += len(name) + len(repr(result))
case _:
chars += len(repr(part))
return math.ceil(chars / 4)