Managing and Reducing Costs › Understanding the Cost Structure
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 › Understanding the Cost StructureOne section · Sections in this chapter2 / 15 ↓
Sections in this chapter15
Before we optimize, let's understand what you're paying for. Most language model APIs charge per token, with different rates for input and output.
Here's a simplified example of typical pricing (November 2025):
| Model | Input (per 1M tokens) | Output (per 1M tokens) | Best For |
|---|---|---|---|
| Claude Sonnet 4.5 | $3.00 | $15.00 | Complex reasoning, agents |
| GPT-5 | $2.50 | $10.00 | General-purpose tasks |
| Gemini 2.5 Flash | $0.40 | $1.20 | Simple queries, high volume |
| Gemini 2.5 Pro | $1.25 | $5.00 | Multimodal, large context |
Notice that output tokens cost more than input tokens. This makes sense because generating text requires more computation than processing it. It also means that verbose responses are expensive.
Let's calculate the cost of a typical interaction:
In[3]:
Codedef calculate_interaction_cost(input_tokens, output_tokens, model="claude-sonnet-4-5"):
"""Calculate the cost of a single model interaction."""
# Pricing per million tokens (November 2025 rates)
pricing = {
"claude-sonnet-4-5": {"input": 3.00, "output": 15.00},
"gpt-5": {"input": 2.50, "output": 10.00},
"gemini-2.5-flash": {"input": 0.40, "output": 1.20},
"gemini-2.5-pro": {"input": 1.25, "output": 5.00}
}
rates = pricing[model]
# Calculate cost (rates are per million tokens)
input_cost = (input_tokens / 1_000_000) * rates["input"]
output_cost = (output_tokens / 1_000_000) * rates["output"]
total_cost = input_cost + output_cost
return {
"input_cost": input_cost,
"output_cost": output_cost,
"total_cost": total_cost
}
## Example: A conversation with context
input_tokens = 1500 # System prompt + conversation history + query
output_tokens = 500 # Agent's response
cost = calculate_interaction_cost(input_tokens, output_tokens, "claude-sonnet-4-5")
print(f"Input cost: ${cost['input_cost']:.6f}")
print(f"Output cost: ${cost['output_cost']:.6f}")
print(f"Total cost: ${cost['total_cost']:.6f}")
print(f"\nCost per 1000 interactions: ${cost['total_cost'] * 1000:.2f}")Out[3]:
ConsoleInput cost: $0.004500 Output cost: $0.007500 Total cost: $0.012000 Cost per 1000 interactions: $12.00
Output:
Input cost: $0.004500
Output cost: $0.007500
Total cost: $0.012000
Cost per 1000 interactions: $12.00A single interaction costs about one cent. That seems small, but multiply it by thousands of users and millions of interactions, and you're looking at serious money.