Implement cost optimization for agent systems:
Cost Factors:
- Model selection (GPT-4 vs GPT-3.5)
- Token usage (input/output)
- Tool call costs
- Infrastructure
Methods:
select_model(complexity, budget): Route to cheapest viable model- Simple tasks → cheaper model
- Complex tasks → expensive model
- Return None if budget insufficient
estimate_cost(model, in_tokens, out_tokens): Predict costtrack_usage(model, in_tokens, out_tokens): Record spendoptimize_memory_usage(messages, target): Compress to save tokensget_cost_report(): Breakdown by component
Cost Formula:
cost = (input_tokens * input_price + output_tokens * output_price) / 1000
Examples
Example 1:
Input:
opt = CostOptimizer(budget=1.0); model = opt.select_model('simple', 1.0); modelOutput:
'gpt-3.5'Explanation: Simple task routed to cheaper model
Starter Code
class CostOptimizer:
"""
Optimize agent execution costs through model routing and token management.
"""
def __init__(self, budget):
self.budget = budget
self.spent = 0
self.model_costs = {
'gpt-4': {'input': 0.03, 'output': 0.06}, # per 1K tokens
'gpt-3.5': {'input': 0.0015, 'output': 0.002}
}
self.usage_history = []
def select_model(self, task_complexity, remaining_budget):
"""
Select appropriate model based on task and budget.
Returns model name or None if budget insufficient.
"""
# Your implementation here
pass
def estimate_cost(self, model, input_tokens, expected_output_tokens):
"""Estimate cost for operation"""
# Your implementation here
pass
def track_usage(self, model, input_tokens, output_tokens):
"""Track actual usage and deduct from budget"""
# Your implementation here
pass
def optimize_memory_usage(self, messages, target_tokens):
"""
Optimize memory to fit target token count.
Uses compression, summarization.
"""
# Your implementation here
pass
def get_cost_report(self):
"""Generate cost breakdown report"""
# Your implementation here
passPython3
ReadyLines: 1Characters: 0
Ready