Skip to main content

One-line spending control for AI agents. @budget(max_usd=10.00) decorator for LLM APIs and x402 payments.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bonanza_labs_agent_budget-0.1.0.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bonanza_labs_agent_budget-0.1.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file bonanza_labs_agent_budget-0.1.0.tar.gz.

File metadata

File hashes

Hashes for bonanza_labs_agent_budget-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7cab7af4d0b966b485210fd121457e8e3146bd5039e6bf1f17f2d17bc6452a7c
MD5 628566fc44a8a6778b969c5a73164f9b
BLAKE2b-256 d806a0f11c385350d58b35b6f138e8404c5aeb0f0b2b2a147c231c310e457acc

See more details on using hashes here.

File details

Details for the file bonanza_labs_agent_budget-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for bonanza_labs_agent_budget-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 905e6e90fc7d5d6ae0ec96a98619165516f7e07dde28f7e3ffdabba625ee8baf
MD5 db36f5e88e2bb0217fbd32504d24fcf9
BLAKE2b-256 e0f18214b6d29b327c61e1791166b7899e1049a99ab671d41026a3fcf069398f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page