Guides

Where AI agents waste tokens, and how to find it in your traces

/Setta/Markdown

Most of the tokens an AI agent wastes come from a handful of patterns that repeat on every run: tool definitions sent on every call but rarely used, a changing field near the top of the prompt that breaks the prompt cache, the same failing tool call retried run after run, context the agent looks up again every run, the most expensive model on steps a cheaper one handles, and workarounds a newer model or platform release made unnecessary. None of them is visible in a single run. To find them, export a few weeks of production traces (from LangSmith, Langfuse, OpenTelemetry or your provider's logs), group the calls across runs instead of reading runs one at a time, and price what you find the way your provider bills it, with cache writes, cache reads, fresh input and output counted separately.

Why reading single runs doesn't work

Each run of a working agent looks reasonable on its own: it called some tools, recovered from an error, and answered. Waste is a property of the fleet, not the run. A tool definition that is never called costs nothing noticeable once and real money when it rides on every call for every customer. In a SaaS product, where the same agent runs for all of your customers, every pattern below is multiplied by your traffic.

That changes the method. Instead of opening traces, you query them: count, group and compare across thousands of runs.

What you need in the traces

You can find most of this with what observability tools already capture, as long as each LLM call keeps:

  • the full request: system prompt, messages and the tools array, not just a summary
  • the usage fields, including cache fields: on Anthropic, cache_creation_input_tokens and cache_read_input_tokens; on OpenAI, usage.prompt_tokens_details.cached_tokens; in OpenTelemetry, the gen_ai.usage.* attributes
  • tool calls and their results, with errors marked
  • a run or session ID, so calls can be grouped into runs, and ideally a tenant or customer ID

One to four weeks of traffic is usually enough to separate habits from one-off events.

The six patterns, and how to find each

1. Tool-definition bloat

What happens: every product feature became a tool, and every tool definition is sent on every call whether the step needs it or not. Anthropic reported that 58 tools can take about 55K tokens before any work starts (Anthropic, Nov 2025). Bloat costs quality as well as money: Chroma tested 18 models and found their performance grows less reliable as input gets longer (Chroma, Jul 2025).

How to find it: for each tool, count how often it is defined versus how often it is called, across all runs. Tools defined on every call and called in a small share of runs are candidates. Measure the tokens their definitions take. The upside can be large: in one study, retrieving the 3 most relevant tools per query from a catalog of 121 cut tool-related tokens 99.6% and still found the right tool 97.1% of the time (Mudunuri et al., Mar 2026).

Watch out: on most providers the tools array is part of the cached prefix. Dropping tools per step can save tokens and still raise the bill if it changes the prefix on every call. Loading rarely used tools on demand, or keeping the tool block stable per conversation, avoids that.

2. Cache breakers

What happens: a timestamp, request ID, tenant field or shuffled tool order sits near the top of the prompt, so the cached prefix is different on every call and the cache is rewritten instead of read. Manus, which builds a general-purpose agent, calls the KV-cache hit rate the single most important metric for a production agent, and names a timestamp at the start of the system prompt as a common mistake (Manus, Jul 2025).

How to find it: compare the first few thousand tokens of consecutive calls in the same run and across runs. Look for the first position where they differ and what sits there. Then check the usage fields: calls that write the cache on every step and rarely read it are the symptom. Anthropic and OpenAI both now offer cache diagnostics that report miss reasons.

Fix: move volatile fields after the stable part of the prompt, keep tool order fixed, and set cache breakpoints after the stable prefix.

3. Repeat errors

What happens: the same tool call fails the same way (bad arguments, a schema mismatch, an expired token) and the agent retries, across many runs and customers.

How to find it: normalize tool errors (strip IDs and timestamps from the error text) and group them by tool and error. Errors that recur across many runs are habits, not incidents. For each one, look at what the next successful call changed: that difference is usually the fix (refresh the token first, send the date in the format the tool expects).

4. Re-learning and re-reads

What happens: every run looks up the same account configuration, schema or documentation again, and long-running agents re-send the same large tool outputs on every later step.

How to find it: hash tool calls by name and arguments and count repeats within a run and across runs. Lookups that return the same thing in most runs belong in the starting context. Reuse can go further than lookups: a Stanford study that reused plans from earlier runs cut cost 50.3% on average across five agent workloads while keeping 96.6% of the best accuracy (Zhang et al., 2026). Within a run, measure how many tokens of earlier tool output are re-sent at each step.

5. Model overkill

What happens: the most capable model runs every step, including steps a cheaper model handles just as well, such as classification, extraction or formatting.

How to find it: split runs into steps by type and see where cost concentrates. Routing a step to a cheaper model is a behavior change, so it needs testing on your own traffic, not a benchmark. Switching models mid-run can also cost cache: the new model starts with a cold cache.

6. Stale setups

What happens: a workaround written for an older model or platform keeps running after a release made it unnecessary. A common example: "Reply ONLY in valid JSON" in the prompt plus a repair retry, where native structured output now exists. Others: hand-rolled compaction or tool selection where the platform now ships one.

How to find it: list the prompt instructions and code paths that exist to work around a model limitation, and check each against what your current model and platform support natively. Count how often the workaround path actually fires.

Price it in dollars, not tokens

Token counts are the wrong unit for deciding what to fix. Cached input costs a fraction of fresh input: Manus points out that on Claude Sonnet, cached input cost $0.30 per million tokens against $3.00 uncached, a 10x gap (Manus, Jul 2025). On several providers, writing the cache costs more than fresh input. So a change that removes tokens can still raise the bill if it changes the cached prefix.

So for each pattern, estimate the saving by re-pricing the affected calls with cache writes, cache reads, fresh input and output priced separately, and check how the change would affect what gets cached. The next guide covers how to do that properly, including a measured case where cutting tokens raised the bill: proving LLM savings before you ship.

A short procedure

  1. Export one to four weeks of production traces with full request content and usage fields.
  2. Rebuild your actual bill from the traces, so you know the data is complete.
  3. Run the six checks above across all runs, not a sample of single runs.
  4. For each finding, re-price the affected calls with the cache modeled, and rank by dollars.
  5. Test the top fixes on your own traffic before rolling them out, and roll them out behind an A/B test with a way back.

How Setta does this

Setta reads production traces from LangSmith, Langfuse, OpenTelemetry, provider logs, or the gateway in front of self-hosted models, runs these checks across every run, and proves each candidate fix by replaying your traffic in a cache-aware simulator before recommending it. You send a trace export; there is nothing to install. Fixes come back as prompt, tool and model-config changes your team deploys. The free audit does this on one to four weeks of your traces.