Controlling Costs
Production agents can incur significant API costs. Cost controls ensure the system operates within budget and detects unexpected cost spikes early.
6 min•By Priygop Team•Updated 2026
Cost Control Strategies
- Use the cheapest model that meets quality requirements — GPT-4o-mini for simple tasks, GPT-4o for complex ones
- Cache frequently used data — avoid re-fetching data that doesn't change between steps
- Limit context size — trim history to the last N steps rather than including the full conversation
- Set per-task cost limits — if a single task exceeds a budget threshold, stop and escalate
- Set daily/monthly budget alerts — be notified before costs become a problem
- Batch requests where possible — combine multiple small tool calls into one larger one
Cost Budget Guard
Cost Budget Guard
# Cost budget guard for production agents
class CostBudgetGuard:
def __init__(self, max_cost_per_task_usd: float = 0.10,
max_daily_cost_usd: float = 50.00):
self.max_task_cost = max_cost_per_task_usd
self.max_daily_cost = max_daily_cost_usd
self.task_cost = 0.0
self.daily_cost = 0.0
self.daily_date = None
def check_and_add(self, token_cost_usd: float) -> dict:
"""Add a cost and check if any budget is exceeded."""
from datetime import date
today = date.today()
# Reset daily counter on new day
if self.daily_date != today:
self.daily_cost = 0.0
self.daily_date = today
self.task_cost += token_cost_usd
self.daily_cost += token_cost_usd
if self.task_cost > self.max_task_cost:
return {"allowed": False, "reason": "task_budget_exceeded",
"task_cost": self.task_cost, "limit": self.max_task_cost}
if self.daily_cost > self.max_daily_cost:
return {"allowed": False, "reason": "daily_budget_exceeded",
"daily_cost": self.daily_cost, "limit": self.max_daily_cost}
return {"allowed": True, "task_cost": self.task_cost,
"daily_cost": self.daily_cost}
def reset_task(self):
"""Call at the start of each new task."""
self.task_cost = 0.0
guard = CostBudgetGuard(max_cost_per_task_usd=0.10, max_daily_cost_usd=50.0)
guard.reset_task()
for i, cost in enumerate([0.01, 0.03, 0.05, 0.04], 1):
result = guard.check_and_add(cost)
print(f" Step {i}: $" + f"{cost:.2f} -> {'OK' if result['allowed'] else 'BLOCKED: ' + result.get('reason', '')} "
f"(task total: $" + f"{result['task_cost']:.3f})")Key Takeaways
- Production agents can incur significant API costs.
- Use the cheapest model that meets quality requirements — GPT-4o-mini for simple tasks, GPT-4o for complex ones
- Cache frequently used data — avoid re-fetching data that doesn't change between steps
- Limit context size — trim history to the last N steps rather than including the full conversation