Skip to content

feat(cost): add per-run dollar budget sample (BudgetedModel) - #328

Open
karmendra8386 wants to merge 1 commit into
strands-agents:mainfrom
karmendra8386:sample/319-budgeted-agent
Open

karmendra8386 wants to merge 1 commit into
strands-agents:mainfrom
karmendra8386:sample/319-budgeted-agent

Conversation

@karmendra8386

Copy link
Copy Markdown
Contributor

Implements the proposal in #319 (thanks @domondi1 for the proposal and the Inferrail design discussion there — this is the complementary client-side approach I mentioned in the thread).

What it does

Adds python/03-integrate/cost/budgeted-agent/: a BudgetedModel wrapper that enforces a hard dollar ceiling on a single agent run.

  • The wrapper sits at the model boundary — the exact point providers bill. Before every model call it checks the remaining budget and raises BudgetExceededError once the ceiling is crossed, so a runaway tool loop cannot slip past: it always has to come back through the model.
  • As each response streams back, it prices the provider-reported usage from the {"metadata": {"usage": ...}} event every Strands provider emits, so accounting is exact rather than estimated.
  • No gateway and no provider-specific code: works with Bedrock, Anthropic, OpenAI, or any other Strands model.

The demo runs an agent told to retry an always-failing tool with a $0.05 budget and prints the halted run's call count and spend.

Verification

Verified locally with a stub model that always requests the failing tool: the wrapper halts the loop after exactly 2 model calls / $0.09 spent against a $0.05 budget, and accounts spend exactly. (The budget error reaches caller code wrapped in EventLoopException as original_exception, like any model error — documented in the README.)

Files

  • budgeted_model.py — BudgetedModel + BudgetExceededError
  • agent.py — demo
  • README.md — problem, design, run instructions; points to tokenecon's Strands CostTracker for per-task accounting across runs
  • requirements.txt

Implements the proposal in strands-agents#319: a BudgetedModel wrapper that enforces a
hard dollar ceiling on a single agent run at the model boundary, pricing
provider-reported usage from stream metadata events. Includes a demo with
a flaky tool that would otherwise retry indefinitely.

Verified locally with a stub model: the wrapper halts a runaway tool loop
after the budget is crossed (2 calls / $0.09 against a $0.05 budget) and
accounts spend exactly.
@github-actions

Copy link
Copy Markdown

Latest scan for commit: 075781e | Updated: 2026-10-14 21:35:45 UTC

✅ Security Scan Report (PR Files Only)

Scanned Files

  • python/03-integrate/cost/budgeted-agent/README.md
  • python/03-integrate/cost/budgeted-agent/agent.py
  • python/03-integrate/cost/budgeted-agent/budgeted_model.py
  • python/03-integrate/cost/budgeted-agent/requirements.txt

Security Scan Results

Critical High Medium Low Info
0 0 0 0 0

Threshold: High

No security issues detected in your changes. Great job!

This scan only covers files changed in this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant