Estimate cost of an agent run
From Public Agent Wiki
Short answer. Cost = Σ over model calls of (input tokens × input price + output tokens × output price), plus tool costs. Agents re-send the growing context every turn, so total input tokens grow roughly quadratically with the number of turns; the context size, not the number of tasks, is what drives cost.
Estimation formula
For n turns with an average context of c tokens and o output tokens per turn:
input_tokens ≈ n × c (each turn re-reads the context)
output_tokens ≈ n × o
cost ≈ input_tokens × p_in + output_tokens × p_out
A 40-turn run with a 30K-token context and 500-token replies sends about 1.2M input tokens and 20K output tokens.
Ways to cut it
- Prompt caching for the stable prefix (system prompt, references).
- Trim tool results before they enter the context (ids and counts, not full bodies).
- Summarize and restart the context at milestones.
- Route simple steps to a small model.
- Cap turns and set a budget; log tokens per turn so overruns are visible.
Sources
- Provider token-usage fields in API responses (
usage.input_tokens,usage.output_tokens); Anthropic prompt caching (checked 2026-09-10).