Documentation
AI Agent Cost Analysis: Find Expensive Steps and LLM Calls
AI agent costs can grow quickly when a workflow makes multiple LLM calls with large contexts. Understanding which individual steps drive the most cost is the first step toward optimization.
Why agent costs grow
Most LLM APIs charge per token — both for the tokens you send (prompt/input) and the tokens the model returns (completion/output). In a multi-step agent workflow, the input to each LLM call often includes the accumulated context of all previous steps. As the agent progresses through its reasoning loop, the input token count grows, and so does the cost of each subsequent call.
A single agent run that looks simple from the outside — one user question, one answer — can involve seven or more LLM calls, each increasingly expensive due to the growing context window.
How Agent Analyzer calculates cost
Agent Analyzer reads the cost field from each step in your trace and sums them:
// From analyzer.ts
export function calculateTotalCost(steps: AgentStep[]): number {
return steps.reduce((sum, s) => sum + (s.cost ?? 0), 0)
}Steps without a cost field contribute zero. The analyzer does not automatically calculate cost from token counts — you need to include the actual cost in each step if you want cost analysis.
Important
Agent Analyzer does not fetch current model pricing or calculate cost from tokens automatically. Cost figures in the analysis come directly from the cost field you provide in each step. If your tracing library records step costs (e.g. from the model provider's usage API), include those values.
Cost per step
For each step, the analyzer computes its cost as a percentage of the total run cost:
pct = (step.cost / totalCost) * 100
Steps are sorted by cost descending so you immediately see which step is the biggest contributor. In the results view, each step shows its absolute cost in USD and its percentage of the run total.
Example — 13-step customer support agent
| Step | Type | Cost | % of run |
|---|---|---|---|
| validate_and_respond | llm | $0.1363 | 48.0% |
| draft_response | llm | $0.1387 | 48.8% |
| evaluate_policy_rules | llm | $0.0030 | 1.1% |
| synthesize_orders | llm | $0.0024 | 0.8% |
| process_search_results | llm | $0.0018 | 0.6% |
| understand_context | llm | $0.0012 | 0.4% |
| classify_request | llm | $0.0007 | 0.2% |
| tool steps (5×) | tool | $0.00 | 0% |
Total run cost: $0.2840. The two Claude 3.5 Sonnet steps account for 96.8% of total cost.
Expensive step detector
Agent Analyzer automatically flags any step that accounts for 30% or more of the total run cost as a HIGH severity issue:
// From analyzer.ts — detectExpensiveSteps
if (pct >= 30) {
// flagged as HIGH severity issue
// detail: "$cost, which is X% of the total $totalCost run cost"
// recommendation: inspect prompt size and output length
}This helps you identify immediately which step is the primary cost driver and whether it warrants optimization — for example, reducing prompt size, using a cheaper model, or restructuring the workflow to avoid sending the full context.
Cost per model
The analyzer also groups LLM steps by their model field and shows total cost, total input tokens, total output tokens, and call count for each model used in the run. This lets you compare the cost contribution of different models in the same workflow.
For more on token-related costs, see AI agent token usage.
Analyze your agent trace
See cost per step, cost per model, and expensive step detection on your own trace.
Analyze a trace →