Managing and Reducing Costs › Tracking Costs in Your Agent

Michael BrenndoerferAugust 26, 20252 min read
AI Agent HandbookAll chapters
Part 15 of 15Performance and Cost Optimization - Doing More with Less
Chapter 43Managing and Reducing Costs › Tracking Costs in Your Agent
Reading section 3 of 15 · Tracking Costs in Your Agent · 2 minSection 3 of 15 · 2 min

Book contents

Section 3 of 15 · 2 min read

Managing and Reducing Costs › Tracking Costs in Your AgentOne section · Sections in this chapter3 / 15 ↓

Before you can optimize, you need visibility into what you're spending. Let's add cost tracking to our assistant:

In[4]:
Code
import os
from anthropic import Anthropic
from datetime import datetime

class CostTrackingAgent:
    """Agent that tracks API costs for monitoring and optimization."""
    
    def __init__(self):
        self.client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
        self.cost_log = []
        
        # Pricing per million tokens
        self.pricing = {
            "claude-sonnet-4-5": {"input": 3.00, "output": 15.00}
        }
    
    def _calculate_cost(self, usage, model):
        """Calculate cost from token usage."""
        rates = self.pricing[model]
        input_cost = (usage.input_tokens / 1_000_000) * rates["input"]
        output_cost = (usage.output_tokens / 1_000_000) * rates["output"]
        return input_cost + output_cost
    
    def respond(self, query):
        """Generate response and track costs."""
        model = "claude-sonnet-4-5"
        
        response = self.client.messages.create(
            model=model,
            max_tokens=1024,
            messages=[{"role": "user", "content": query}]
        )
        
        # Calculate and log cost
        cost = self._calculate_cost(response.usage, model)
        
        self.cost_log.append({
            "timestamp": datetime.now(),
            "query": query[:50] + "..." if len(query) > 50 else query,
            "input_tokens": response.usage.input_tokens,
            "output_tokens": response.usage.output_tokens,
            "cost": cost,
            "model": model
        })
        
        return response.content[0].text
    
    def get_cost_summary(self):
        """Get summary of costs."""
        if not self.cost_log:
            return "No interactions yet."
        
        total_cost = sum(entry["cost"] for entry in self.cost_log)
        total_tokens = sum(
            entry["input_tokens"] + entry["output_tokens"] 
            for entry in self.cost_log
        )
        
        return {
            "total_interactions": len(self.cost_log),
            "total_cost": total_cost,
            "total_tokens": total_tokens,
            "average_cost_per_interaction": total_cost / len(self.cost_log),
            "most_expensive": max(self.cost_log, key=lambda x: x["cost"])
        }

## Test the tracking
agent = CostTrackingAgent()

agent.respond("What is Python?")
agent.respond("Explain machine learning in simple terms.")
agent.respond("How do neural networks work?")

summary = agent.get_cost_summary()
print(f"Total interactions: {summary['total_interactions']}")
print(f"Total cost: ${summary['total_cost']:.4f}")
print(f"Average cost: ${summary['average_cost_per_interaction']:.4f}")
print(f"\nMost expensive query: {summary['most_expensive']['query']}")
print(f"Cost: ${summary['most_expensive']['cost']:.4f}")
Out[4]:
Console
Total interactions: 3
Total cost: $0.0136
Average cost: $0.0045

Most expensive query: How do neural networks work?
Cost: $0.0050

This gives you visibility into where your money goes. You might discover that certain queries are far more expensive than others, or that a small percentage of interactions account for most of your costs.