agent-cost-guardrails
Budget limits and cost guardrails for AI agent frameworks. Prevents runaway API spend with hard budget enforcement, circuit breakers, and per-agent cost tracking.
Zero infrastructure required -- no gateway, no proxy, no external service. Pure Python middleware that hooks into your framework at the process level.
Features
- Hard budget limits with
BudgetExceededErroron overspend - Per-call token limits and tokens-per-minute rate limiting
- Circuit breaker that trips after N consecutive violations
- Alert callbacks at configurable thresholds (50%, 80%, 100%)
- Cost breakdown by model and agent
- Thread-safe for multi-agent parallel runs
- Bundled pricing for 30+ models (OpenAI, Anthropic, Google, Mistral, DeepSeek, Meta)
- Custom pricing overrides for any model
Supported Frameworks
| Framework | Integration | Hook Mechanism |
|---|---|---|
| CrewAI | CrewAIGuardrails |
@before_llm_call / @after_llm_call |
| AutoGen/AG2 | AutoGenGuardrails |
safeguard_llm_inputs / safeguard_llm_outputs |
| LangGraph | LangGraphGuardrails |
BaseCallbackHandler |
Installation
pip install agent-cost-guardrails
Install with framework-specific extras:
pip install agent-cost-guardrails[crewai] # CrewAI integration
pip install agent-cost-guardrails[autogen] # AutoGen/AG2 integration
pip install agent-cost-guardrails[langgraph] # LangGraph/LangChain integration
pip install agent-cost-guardrails[all] # All frameworks
Quick Start
Context Manager
from agent_cost_guardrails import BudgetGuard
with BudgetGuard(max_usd=5.00) as guard:
# Before each LLM call
guard.pre_call_check(estimated_tokens=2000)
# After each LLM call - record actual usage
guard.post_call_record("gpt-4o", input_tokens=1500, output_tokens=800)
print(guard.cost_report())
Decorator
from agent_cost_guardrails import budget_limit
@budget_limit(max_usd=5.00)
def run_my_agents(guard=None):
guard.pre_call_check()
guard.post_call_record("gpt-4o", input_tokens=1000, output_tokens=500)
return guard.cost_report()
result = run_my_agents()
CrewAI
from agent_cost_guardrails.integrations import CrewAIGuardrails
guards = CrewAIGuardrails(max_usd=5.00, max_tokens_per_call=4096)
guards.install() # Registers hooks globally
crew.kickoff()
print(guards.cost_report())
AutoGen / AG2
from agent_cost_guardrails.integrations import AutoGenGuardrails
guards = AutoGenGuardrails(max_usd=10.00)
guards.wrap_agent(assistant_agent)
guards.wrap_agent(user_proxy_agent)
# Run your chat - budget enforced automatically
print(guards.cost_report())
LangGraph / LangChain
from agent_cost_guardrails.integrations import LangGraphGuardrails
guards = LangGraphGuardrails(max_usd=2.00)
result = graph.invoke(
state,
config={"callbacks": [guards.callback_handler]}
)
print(guards.cost_report())
Alert Callbacks
def my_alert(threshold, current_cost, max_budget):
print(f"ALERT: {threshold*100}% budget used (${current_cost:.2f}/${max_budget:.2f})")
guard = BudgetGuard(
max_usd=10.00,
alert_thresholds=[0.5, 0.8, 1.0],
on_alert=my_alert,
)
Custom Pricing
from agent_cost_guardrails import set_custom_pricing
set_custom_pricing({
"my-fine-tuned-model": {
"input_per_mtok": 5.0, # $5.00 per 1M input tokens
"output_per_mtok": 15.0, # $15.00 per 1M output tokens
}
})
Cost Report
report = guard.cost_report()
# {
# "total_cost_usd": 0.0325,
# "total_input_tokens": 5000,
# "total_output_tokens": 2000,
# "total_calls": 3,
# "budget_usd": 10.0,
# "remaining_usd": 9.9675,
# "cost_by_model": {"gpt-4o": 0.0325},
# "cost_by_agent": {"researcher": 0.02, "writer": 0.0125},
# "tokens_by_model": {"gpt-4o": {"input": 5000, "output": 2000}}
# }
License
MIT -- see LICENSE for details.
Metadata
Release files for agent-cost-guardrails 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| agent_cost_guardrails-0.1.0.tar.gz | 14.1 kB | Without provenance | Details for agent_cost_guardrails-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| agent_cost_guardrails-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for agent_cost_guardrails-0.1.0-py3-none-any.whl |
Total release size: 29.2 kB
Release files / agent_cost_guardrails-0.1.0.tar.gz
| Download URL | agent_cost_guardrails-0.1.0.tar.gz |
|---|---|
| Size | 14.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cc96a60f1b189eab27f15de26a58a7e577e7848bb92fd2841dcd80561816d2eb
|
|
BLAKE2b-256 checksum How to use checksums |
14dd989751ed89a0138d5dd3e497b4fba944465a5669d762b2ddbb787a8eaa6a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / agent_cost_guardrails-0.1.0-py3-none-any.whl
| Download URL | agent_cost_guardrails-0.1.0-py3-none-any.whl |
|---|---|
| Size | 15.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d0c9e85f6008c3a7dfed1bcc955e322aa9e3ebdcb5a3beec960a005dd54c4c16
|
|
BLAKE2b-256 checksum How to use checksums |
5043947da316c68a9081c6548c19ad886e5b6648152c232946b63b4f3cdc0f18
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Security for agent-cost-guardrails 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.