Your agent needs a token budget (or it'll drain your account overnight)
Last week I watched someone's agent burn through $400 in API credits in six hours. The agent got stuck in a loop, calling expensive vision models to "analyze" the same screenshot 847 times.
The fix wasn't better error handling or smarter prompts. It was a simple token budget that cuts the agent off before it goes rogue.
Here's the pattern that prevents budget disasters:
class TokenBudget:
def __init__(self, daily_limit=50000, hourly_limit=10000):
self.daily_limit = daily_limit
self.hourly_limit = hourly_limit
self.daily_used = 0
self.hourly_used = 0
self.hour_start = datetime.now().hour
def check_budget(self, estimated_tokens):
current_hour = datetime.now().hour
if current_hour != self.hour_start:
self.hourly_used = 0
self.hour_start = current_hour
if self.hourly_used + estimated_tokens > self.hourly_limit:
raise BudgetExceededError("Hourly token limit reached")
if self.daily_used + estimated_tokens > self.daily_limit:
raise BudgetExceededError("Daily token limit reached")
def consume_tokens(self, actual_tokens):
self.daily_used += actual_tokens
self.hourly_used += actual_tokensBut token counting gets tricky. Different models charge differently, and you can't always predict token usage before making a call.
The cost-based approach works better:
class CostBudget:
def __init__(self, daily_budget=50.00, hourly_budget=10.00):
self.daily_budget = daily_budget
self.hourly_budget = hourly_budget
self.daily_spent = 0.0
self.hourly_spent = 0.0
# Model pricing (per 1M tokens)
self.pricing = {
'gpt-4': {'input': 30.0, 'output': 60.0},
'gpt-3.5-turbo': {'input': 0.5, 'output': 1.5},
'claude-3-opus': {'input': 15.0, 'output': 75.0}
}
def estimate_cost(self, model, input_tokens, max_output_tokens):
rates = self.pricing.get(model, self.pricing['gpt-4'])
input_cost = (input_tokens / 1_000_000) * rates['input']
max_output_cost = (max_output_tokens / 1_000_000) * rates['output']
return input_cost + max_output_cost
def can_afford(self, estimated_cost):
return (self.hourly_spent + estimated_cost <= self.hourly_budget and
self.daily_spent + estimated_cost <= self.daily_budget)Warning: Don't just set limits and walk away. Your agent needs graceful degradation when it hits budget limits — switch to cheaper models, defer non-urgent tasks, or pause until the next budget cycle.
The real power comes from adaptive budgeting. High-priority tasks get more budget. Low-priority tasks get routed to cheaper models or queued for later:
def route_by_budget(task, budget):
if task.priority == 'urgent':
# Use best model regardless of cost
return 'gpt-4'
elif budget.can_afford_premium():
return 'gpt-4'
elif budget.can_afford_standard():
return 'gpt-3.5-turbo'
else:
# Queue for later or use free local model
return 'defer'I've seen too many people discover their agent spent $1,200 on API calls when they check their bill at month-end. A simple budget system prevents those disasters and forces you to think about cost-effectiveness from day one.
Your agent should be profitable, not expensive. Budget constraints make it smarter, not dumber.