Skip to content

API Reference parrot.clients.budget_scope

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.clients.budget_scope

View source

Question budget scopes, process-local registry and suspension/snapshot transfer (FEAT-550, spec §2.6 / §3 M2).

Attribute BUDGET_KWARGS

Type: frozenset[str]

Class BudgetDefaults

Constructor-level budget settings of a client or bot (spec §2.1).

Parameter Type Default Description
token_budget Optional[int] None
budget_mode str 'estimated'
final_answer_reserve 'int | float' 0.15
registry Optional[BudgetRegistry] None

Fields:

Field Type Default Description
token_budget Optional[int] None
budget_mode str 'estimated'
final_answer_reserve 'int | float' 0.15
registry Optional[BudgetRegistry] None

Class BudgetRegistry

Bounded process-local registry; each operation stays on its owning loop.

Parameter Type Default Description
max_retained int 1024
retention_seconds int 3600

Method BudgetRegistry.create

Allocate one root identity or fail if retained capacity is exhausted.

create(policy: TokenBudgetPolicy) -> BudgetScope

Parameter Type Default Description
policy TokenBudgetPolicy

Returns: BudgetScope

Method BudgetRegistry.suspend

Mint a fresh resume nonce and return the namespaced state envelope (spec §2.6).

suspend(scope: BudgetScope) -> dict[str, Any]

Parameter Type Default Description
scope BudgetScope

Returns: dict[str, Any]

Method BudgetRegistry.resume

Consume the suspension nonce after policy, identity and floor checks.

resume(state: dict[str, Any], *, snapshot: Optional[BudgetSnapshot] = None) -> BudgetScope

Parameter Type Default Description
state dict[str, Any]
snapshot Optional[BudgetSnapshot] None

Returns: BudgetScope

Method BudgetRegistry.export_settled

Transfer a quiescent suspension; refuse in-flight/uncertain ownership.

export_settled(operation_id: str) -> BudgetSnapshot

Parameter Type Default Description
operation_id str

Returns: BudgetSnapshot

Method BudgetRegistry.release

Release retained terminal state; never discard an active reservation.

release(operation_id: str) -> None

Parameter Type Default Description
operation_id str

Method BudgetRegistry.mark_closed

Record terminal state so a snapshot can never reopen it (called by BudgetScope.aexit).

mark_closed(operation_id: str) -> None

Parameter Type Default Description
operation_id str

Class BudgetRequest

Resolved outcome of one call's budget keywords.

Parameter Type Default Description
policy Optional[TokenBudgetPolicy]
scope Optional[BudgetScope]
snapshot Optional[BudgetSnapshot]
disabled bool

Fields:

Field Type Default Description
policy Optional[TokenBudgetPolicy]
scope Optional[BudgetScope]
snapshot Optional[BudgetSnapshot]
disabled bool

Attributes:

Attribute Type Description
active bool

Class BudgetScope

Live ledger reference, owner identity and immutable inherited policy.

Parameter Type Default Description
ledger QuestionBudget
registry 'BudgetRegistry'
is_root bool
owner_call_id Optional[str] None

Attributes:

Attribute Type Description
operation_id str
policy TokenBudgetPolicy
owner_designated bool

Method BudgetScope.designate_owner

Mark this root's answer owner once; descendants can never claim (spec §2.1/§2.3).

designate_owner(call_id: str) -> None

Parameter Type Default Description
call_id str

Method BudgetScope.child

Derive a descendant scope: spending rights only, never finalization ownership (spec §2.1).

child() -> 'BudgetScope'

Returns: 'BudgetScope'

Attribute TOKEN_BUDGET_STATE_KEY

Function budget_entry

Pick the right wrapper for fn; idempotent on already-wrapped functions.

budget_entry(fn: Callable, *, method_name: str) -> Callable

Parameter Type Default Description
fn Callable
method_name str

Returns: Callable

Function current_budget_scope

Return the live inherited scope, or None when no question budget is active.

current_budget_scope() -> Optional['BudgetScope']

Returns: Optional['BudgetScope']

Function get_default_registry

Process-wide default registry used when a client/bot is not handed one explicitly.

get_default_registry() -> BudgetRegistry

Returns: BudgetRegistry

Function resolve_budget_request

Pop the §2.1 keywords from call_kwargs (mutating it) and decide root/child/disabled.

resolve_budget_request(call_kwargs: dict[str, Any], *, defaults: BudgetDefaults, method_name: str) -> BudgetRequest

Parameter Type Default Description
call_kwargs dict[str, Any]
defaults BudgetDefaults
method_name str

Returns: BudgetRequest

Function wrap_budgeted_async_generator

Wrap an async-generator method: hold the scope binding through iteration (spec §2.4).

wrap_budgeted_async_generator(fn: Callable, *, method_name: str) -> Callable

Parameter Type Default Description
fn Callable
method_name str

Returns: Callable

Function wrap_budgeted_coroutine

Wrap a coroutine method: resolve keywords, bind scope for the await, strip keywords.

wrap_budgeted_coroutine(fn: Callable, *, method_name: str) -> Callable

Parameter Type Default Description
fn Callable
method_name str

Returns: Callable

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally