Agent Analyzer

Documentation

AI Agent Token Usage: Understand Context Growth and LLM Costs

Token usage is the primary driver of LLM cost in most agent workflows. Understanding how tokens accumulate — and why context grows across LLM calls — is essential for optimizing agent performance.

Input tokens vs. output tokens

Every LLM API call involves two distinct token counts:

inputTokens

Input (prompt) tokens

The tokens you send to the model — the system prompt, conversation history, tool results, and any additional context. This grows as the agent accumulates more context across steps.

outputTokens

Output (completion) tokens

The tokens returned by the model — its reasoning steps, function call arguments, or final response. Generally more expensive per token than input tokens on most model APIs.

Agent Analyzer sums both across all steps:

// From analyzer.ts — calculateTokenUsage
for (const s of steps) {
  totalInputTokens += s.inputTokens ?? 0
  totalOutputTokens += s.outputTokens ?? 0
}
totalTokens = totalInputTokens + totalOutputTokens

Why context grows across LLM calls

In a typical ReAct-style agent loop, the model receives the full conversation and tool results on every call. As the agent executes more steps — retrieving data, calling tools, receiving results — each new piece of information is appended to the context that gets sent on the next LLM call.

The result: input tokens grow monotonically. The fifth LLM call in an agent run is often 3–10× more expensive per token count than the first.

Context growth — example from a 7-LLM-call agent run

LLM CallinputTokensGrowth vs. first
classify_request4,1201.0×
understand_context6,8001.7×
process_search_results10,4002.5×
synthesize_orders14,2003.4×
evaluate_policy_rules18,6004.5×
draft_response24,1005.9×
validate_and_respond29,8007.2×

Context grew 7.2× from the first to the last LLM call. From LLM call 4 onward, input token count exceeds 3× the baseline — triggering the context growth detector.

Context growth detector

Agent Analyzer compares the inputTokens of the first and last LLM steps in the trace. If the ratio is 3× or more, it flags a context growth issue:

// From analyzer.ts — detectContextGrowth
const first = llmSteps[0].inputTokens
const last  = llmSteps[llmSteps.length - 1].inputTokens
const ratio = last / first

if (ratio >= 3) {
  severity = ratio >= 5 ? 'high' : 'medium'
  // "Input tokens grew X× from first to last LLM call"
}

HIGH severity

ratio ≥ 5× (e.g. 4,120 → 20,600+ tokens)

MEDIUM severity

ratio ≥ 3× and < 5× (e.g. 4,120 → 12,360 tokens)

Token growth visualization

The results page includes a cumulative input token chart that plots inputTokens for each LLM step in order. This makes it easy to see how steeply context is growing and at which step the growth accelerates. Tool steps and retrieval steps are excluded from this chart since they do not contribute input tokens to subsequent LLM calls directly.

Reducing token usage

When context growth is flagged, the issue recommendation suggests:

  • Summarize context between LLM calls rather than appending full results
  • Use a sliding-window strategy that truncates older context
  • Filter tool outputs to include only the relevant fields before passing to the model
  • Split the workflow into smaller sub-agents with limited context scope

For cost implications see AI agent cost analysis. For latency implications see AI agent latency.

Analyze your agent trace

See token usage per step, cumulative context growth chart, and growth detection on your own trace.

Analyze a trace →