Managing and Reducing Costs › Strategy 7: Set Budget Limits
Michael BrenndoerferAugust 26, 20252 min read
AI Agent HandbookAll chapters Part 15 of 15Performance and Cost Optimization - Doing More with Less
Managing and Reducing Costs › Strategy 7: Set Budget LimitsOne section · Sections in this chapter10 / 15 ↓
Sections in this chapter15
Prevent runaway costs by implementing budget controls in your agent.
Example: Budget-Aware Agent (Multi-Provider)
In[11]:
Codeimport os
from anthropic import Anthropic
from datetime import datetime, timedelta
class BudgetControlledAgent:
"""Agent with built-in budget limits and alerts."""
def __init__(self, daily_budget=10.0):
self.client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
self.daily_budget = daily_budget
self.current_day = datetime.now().date()
self.daily_spending = 0.0
self.total_spending = 0.0
def _reset_daily_budget_if_needed(self):
"""Reset daily spending counter at midnight."""
today = datetime.now().date()
if today != self.current_day:
self.current_day = today
self.daily_spending = 0.0
def _calculate_cost(self, usage):
"""Calculate cost from token usage."""
input_cost = (usage.input_tokens / 1_000_000) * 3.00
output_cost = (usage.output_tokens / 1_000_000) * 15.00
return input_cost + output_cost
def respond(self, query):
"""Generate response if within budget."""
self._reset_daily_budget_if_needed()
# Check if we're over budget
if self.daily_spending >= self.daily_budget:
return {
"response": None,
"error": f"Daily budget of ${self.daily_budget:.2f} exceeded. Current spending: ${self.daily_spending:.2f}",
"budget_remaining": 0.0
}
# Make the call
response = self.client.messages.create(
model="claude-sonnet-4-5",
max_tokens=512,
messages=[{"role": "user", "content": query}]
)
# Track spending
cost = self._calculate_cost(response.usage)
self.daily_spending += cost
self.total_spending += cost
# Check if approaching budget limit
budget_remaining = self.daily_budget - self.daily_spending
warning = None
if budget_remaining < self.daily_budget * 0.2: # Less than 20% remaining
warning = f"Warning: Only ${budget_remaining:.2f} remaining in daily budget"
return {
"response": response.content[0].text,
"cost": cost,
"daily_spending": self.daily_spending,
"budget_remaining": budget_remaining,
"warning": warning
}
def get_spending_summary(self):
"""Get spending statistics."""
return {
"daily_spending": self.daily_spending,
"daily_budget": self.daily_budget,
"budget_used_percent": (self.daily_spending / self.daily_budget) * 100,
"total_spending": self.total_spending
}
## Test budget controls
agent = BudgetControlledAgent(daily_budget=0.10) # $0.10 daily limit
queries = [
"What is Python?",
"Explain machine learning.",
"How do neural networks work?",
"What is deep learning?",
"Describe reinforcement learning.",
"What are transformers?",
"Explain attention mechanisms.",
"What is GPT?",
"How does BERT work?",
"What is transfer learning?"
]
for i, query in enumerate(queries, 1):
print(f"\n--- Query {i} ---")
result = agent.respond(query)
if result["response"]:
print(f"Response: {result['response'][:100]}...")
print(f"Cost: ${result['cost']:.6f}")
print(f"Daily spending: ${result['daily_spending']:.4f}")
if result["warning"]:
print(f"⚠️ {result['warning']}")
else:
print(f"❌ {result['error']}")
break
summary = agent.get_spending_summary()
print(f"\n=== Spending Summary ===")
print(f"Daily budget: ${summary['daily_budget']:.2f}")
print(f"Daily spending: ${summary['daily_spending']:.4f}")
print(f"Budget used: {summary['budget_used_percent']:.1f}%")Out[11]:
Console--- Query 1 ---
Response: Python is a high-level, interpreted programming language created by Guido van Rossum and first relea... Cost: $0.004398 Daily spending: $0.0044 --- Query 2 ---
Response: # Machine Learning Explained **Machine learning** is a branch of artificial intelligence where comp... Cost: $0.004986 Daily spending: $0.0094 --- Query 3 ---
Response: # How Neural Networks Work Neural networks are computing systems inspired by biological brains. Her... Cost: $0.005199 Daily spending: $0.0146 --- Query 4 ---
Response: Deep learning is a subset of machine learning that uses artificial neural networks with multiple lay... Cost: $0.004131 Daily spending: $0.0187 --- Query 5 ---
Response: # Reinforcement Learning **Reinforcement learning (RL)** is a type of machine learning where an age... Cost: $0.005124 Daily spending: $0.0238 --- Query 6 ---
Response: # Transformers Transformers are a type of **deep learning architecture** introduced in 2017 that ha... Cost: $0.004671 Daily spending: $0.0285 --- Query 7 ---
Response: # Attention Mechanisms Attention mechanisms allow neural networks to **focus on specific parts of t... Cost: $0.006171 Daily spending: $0.0347 --- Query 8 ---
Response: GPT stands for **Generative Pre-trained Transformer**. It's a type of AI language model developed by... Cost: $0.003576 Daily spending: $0.0383 --- Query 9 ---
Response: # How BERT Works BERT (Bidirectional Encoder Representations from Transformers) is a language model... Cost: $0.005709 Daily spending: $0.0440 --- Query 10 ---
Response: # Transfer Learning Transfer learning is a machine learning technique where a model developed for o... Cost: $0.004416 Daily spending: $0.0484 === Spending Summary === Daily budget: $0.10 Daily spending: $0.0484 Budget used: 48.4%
Budget controls prevent unexpected bills and force you to think about cost optimization. If you hit your budget limit regularly, it's a signal that you need to optimize your agent's efficiency.