Agent Analyzer

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

StepTypeCost% of run
validate_and_respondllm$0.136348.0%
draft_responsellm$0.138748.8%
evaluate_policy_rulesllm$0.00301.1%
synthesize_ordersllm$0.00240.8%
process_search_resultsllm$0.00180.6%
understand_contextllm$0.00120.4%
classify_requestllm$0.00070.2%
tool steps (5×)tool$0.000%

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 →