Skip to main content

agent-budget

One-line spending control for AI agents.

@budget(max_usd=10.00)
async def my_agent(query: str):
    ...  # anything inside stays under $10

Tracks LLM API costs (Anthropic, OpenAI, Gemini, and more) plus x402 payments.
Raises BudgetExceeded the moment your agent tries to go over.


Install

pip install bonanza-labs-agent-budget
# PyPI name (CLI/import unchanged). From git:
# pip install "git+https://github.com/c6zks4gssn-droid/agent-budget.git"

No required dependencies. Works with any LLM SDK.


Quickstart

from agent_budget import budget, BudgetExceeded
from agent_budget.decorators import track_llm, track_payment, current_spend

@budget(max_usd=5.00)
async def research_agent(query: str) -> str:
    # Call your LLM
    response = await llm.chat(query)

    # Manually record the cost (or use auto_patch=True)
    track_llm("claude-sonnet-4-6",
              input_tokens=response.usage.input_tokens,
              output_tokens=response.usage.output_tokens)

    # Fetch paid content via x402
    data = await fetch_x402("https://data.example.com/report")
    track_payment(0.10, label="x402 report fetch")

    print(f"Spent so far: ${current_spend():.4f}")
    return response.text


# Usage
try:
    result = await research_agent("latest AI research")
except BudgetExceeded as e:
    print(f"Agent hit the limit: spent ${e.spent:.4f} of ${e.limit:.2f}")

Auto-patch (zero-instrumentation mode)

Let agent-budget automatically intercept every Anthropic or OpenAI call:

@budget(max_usd=10.00, auto_patch=True)
async def my_agent(query: str):
    # Every client.messages.create() call is tracked — no manual tracking needed
    response = await anthropic_client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        messages=[{"role": "user", "content": query}]
    )
    return response.content[0].text

Install with the extras:

pip install "bonanza-labs-agent-budget[anthropic]"   # auto-patch Anthropic SDK
pip install "bonanza-labs-agent-budget[openai]"      # auto-patch OpenAI SDK
pip install "bonanza-labs-agent-budget[all]"         # both

Behaviour options

@budget(
    max_usd=10.00,          # Hard limit in USD
    on_exceed="raise",      # "raise" | "warn" | "ignore"
    auto_patch=True,        # Auto-track Anthropic + OpenAI SDK calls
    report_on_exit=True,    # Print spend report when function exits
)
async def my_agent(): ...
on_exceed Behaviour
"raise" Raises BudgetExceeded immediately (default)
"warn" Emits a RuntimeWarning, continues
"ignore" Silent — useful for logging/testing

Manual tracking (no decorator)

from agent_budget import BudgetTracker

tracker = BudgetTracker(max_usd=5.00)

# Record any LLM call
tracker.track_llm("gpt-4o", input_tokens=1200, output_tokens=400)

# Record an x402 payment
tracker.track_payment(amount_usd=0.25, label="data-purchase")

# Any custom cost
tracker.track_custom(amount_usd=0.01, label="tool-call-fee")

print(tracker.report())
# {
#   "limit_usd": 5.0,
#   "spent_usd": 0.030725,
#   "remaining_usd": 4.969275,
#   "entries": [...]
# }

withBudget (functional style)

from agent_budget import withBudget

safe_agent = withBudget(my_agent, max_usd=5.00)
result = await safe_agent("hello")

Supported models (built-in price table)

Provider Models
Anthropic claude-opus-4-8, claude-sonnet-4-6, claude-haiku-4-5, claude-3.5-*
OpenAI gpt-4o, gpt-4o-mini, o3, o3-mini, o4-mini, gpt-4-turbo
Google gemini-2.0-flash, gemini-2.5-pro, gemini-2.5-flash
Meta llama-3.3-70b, llama-3.1-405b
Mistral mistral-large, mistral-small

Unknown models fall back to $5.00 / $15.00 per M tokens. Override any price:

from agent_budget import PRICE_TABLE
PRICE_TABLE["my-custom-model"] = {"input": 0.50, "output": 1.50}

x402 integration

agent-budget tracks x402 payments when you call track_payment().
For full spending firewall enforcement on x402 payments, see
bonanza-labs.com — production-grade policy engine
with human approval queues, vendor blocklists, and audit logs.


FAQ

Does this make network calls?
No. The price table is entirely inline. Zero network calls, zero telemetry.

Does this work with streaming?
Yes for manual tracking — call track_llm() with the final token counts from the stream's usage object. Auto-patch doesn't cover streaming yet (coming in 0.2.0).

What if I nest @budget decorators?
Each call to a @budget-decorated function gets its own isolated context.
Inner contexts don't affect outer ones — you can nest safely.

Thread-safe?
Yes. Uses contextvars.ContextVar for async isolation and threading.Lock inside each context.


License

Apache 2.0 — see LICENSE.

Built by Bonanza Labs.
For teams: managed dashboard, policy engine, and approval queue at bonanza-labs.com/firewall.

Metadata

Release files for bonanza-labs-agent-budget 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bonanza-labs-agent-budget 0.1.0
File Size Uploaded
bonanza_labs_agent_budget-0.1.0.tar.gz 14.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bonanza-labs-agent-budget 0.1.0
File Interpreter ABI Platform
bonanza_labs_agent_budget-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 25.8 kB

Release files / bonanza_labs_agent_budget-0.1.0.tar.gz

Download URL bonanza_labs_agent_budget-0.1.0.tar.gz
Size 14.0 kB
Tags Source
SHA-256 checksum
How to use checksums
7cab7af4d0b966b485210fd121457e8e3146bd5039e6bf1f17f2d17bc6452a7c
BLAKE2b-256 checksum
How to use checksums
d806a0f11c385350d58b35b6f138e8404c5aeb0f0b2b2a147c231c310e457acc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Release files / bonanza_labs_agent_budget-0.1.0-py3-none-any.whl

Download URL bonanza_labs_agent_budget-0.1.0-py3-none-any.whl
Size 11.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
905e6e90fc7d5d6ae0ec96a98619165516f7e07dde28f7e3ffdabba625ee8baf
BLAKE2b-256 checksum
How to use checksums
e0f18214b6d29b327c61e1791166b7899e1049a99ab671d41026a3fcf069398f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page