LLM Cost Optimization Through Observability-Driven Flywheel Improvements
Structured traces reveal where agents waste tokens so fixes target the right layer.

LLM cost optimization in production agents is primarily a harness problem. Prompts, tools, memory, and workflows create work that need not happen, even at a lower token price. An observability-driven flywheel makes that waste visible, attributes it to a specific layer, and tests a targeted change before deployment. Its value comes from turning production evidence into repeatable reductions in cost without sacrificing task quality.
Why agentic AI costs outpace model pricing expectations
Uber's CTO described how the entire annual AI budget was consumed by April 2026. That gap between planned and actual spending comes from the cost of finishing a task, which goes beyond the cost of a single prompt. A chatbot has one model call, while an agent classifies, plans, retrieves, calls tools, inspects results, retries, and summarizes. Each step adds cost, so a task in production can look very different from the request used to plan its budget.
Datadog's State of AI Engineering 2026 report found that agentic frameworks had more than doubled in adoption. The same report showed that token usage per LLM request more than doubled for median organizations year over year and quadrupled for the heaviest users. Frameworks introduce tool fan-out, retries, and branching that multiply consumption automatically. Average request cost obscures this structure because it combines simple tasks with expensive trajectories that require retrieval, planning, and repeated attempts.
Where the waste actually lives — harness layers, not model pricing
The harness determines how much work the model performs to complete a task. Bloated system prompts re-inject verbose instructions and repeated policy definitions on every call. Reasoning loops turn old thoughts into new context, so a five-step ReAct loop consumes far more tokens than a direct answer. These are failures of context and execution design.
Tools and coordination add their own multipliers. Each external call requires tokens to package the request and incurs the API fee, while specialist agents spend tokens passing context between them. A malformed tool response can trigger a silent retry cascade that repeats this work without advancing the task. In a 2026 production agent, many prompts, multiple model IDs, retrievers, tools, and a planner create independent surfaces where cost can drift.
A cheaper model addresses unit price, but it does not remove redundant calls or unnecessary context. The same bloated workflow still wastes money, and reduced quality can trigger more retries. Harness waste can be corrected without retraining or switching models, provided its origin is visible.
Why harness waste stays invisible without trace-level instrumentation
Standard infrastructure monitoring cannot show which prompt version, tool call, or execution branch did work that was not needed. It can expose a problem with the service without explaining the agent's path through it. Production traces provide the flight recorder for that path. They connect tool call frequency, context growth across turns, retry rates, and branch cost to the execution that produced them.
Datadog's State of AI Engineering 2026 report found that nearly a third of all LLM call errors in March 2026 were caused by provider rate limits. That finding becomes actionable within an application when traces reveal where those failures enter its execution chain. The distinction matters because an error total alone does not identify the affected step or the resulting retries.
Structured trace pipelines also give larger teams an audit trail for sensitive data moving through prompts and a basis for anomaly alerting. OpenTelemetry provides an emerging path toward portable agent traces through GenAI semantic conventions, currently in Development status. The essential requirement is visibility into the harness. That visibility produces the evidence that feeds improvement.
The three harness failure categories that traces reliably surface
Tool schema drift appears when changing tool schemas, retrieval indexes, or upstream APIs alter agent behavior without a model update. Traces expose the mismatch through schema validation failures, malformed function call arguments, and sudden spikes in tool errors. Those failures can lead to more calls and retries before the task is done. Drift is therefore a system-level property of the harness.
Prompt ambiguity produces a different pattern. Adding a tool, a policy line, or a formatting change can alter how the model interprets surrounding instructions, even when those instructions retain their wording. Trace signatures include unrelated cases failing after small prompt edits, JSON arguments missing required fields, and bad retrieval chunks changing answers. A particularly costly variant occurs when a tool returns empty but the agent proceeds as though it succeeded, building further work on a false premise.
Workflow loops occur when the same tool invocations repeat or branches do not end. Each iteration re-processes accumulated context, adding cost without corresponding progress toward completion. Confident explanations do not make that repeated work useful. The diagnostic signal lies in the execution pattern.
Why root cause attribution must be layer-specific to drive targeted fixes
A failed run identifies an outcome. The failure must first be assigned across the relevant categories, including model, harness, environment, and grader. Within the harness, attribution must distinguish prompt, tool, workflow, memory, and product logic. A model failure might help set post-training objectives, while a harness failure should guide changes to the system around it.
This distinction is difficult on long traces because the visible symptom often appears far after its cause. Inspecting only the final steps can turn a downstream effect into the presumed origin. Structured attribution gives a finding that states the root cause, the evidence for it, the confidence in it, and the fix direction. Those outputs connect the diagnosis to a specific engineering surface.
The practical objection is that structured attribution takes time. However, changing the wrong layer creates new failures and adds debugging cycles, making a quick but unfocused intervention expensive overall. An instruction failure calls for a change to the prompt, while a tool contract failure calls for a fix to the schema or boundary middleware. Precision narrows both the proposed change and the work needed to establish whether it helped.
How the observability-driven flywheel closes the loop from trace signal to verified fix
The flywheel organizes improvement into execute, diagnose, change, and validate. Execution captures the task, trajectory, and outcome as a structured triplet. Diagnosis uses that record to identify the failure mechanism and attribute it to the responsible layer. The resulting finding supplies the evidence for a change rather than leaving engineers to choose an intervention from the outcome alone.
The change is then scoped to the attributed cause. It might revise an instruction, repair a tool schema, cap a loop, truncate context, or deduplicate retrieval. Validation replays the planned change against past traces before it goes into production. A sustained score drop or cost regression blocks deployment, so a plausible fix does not become an accepted fix merely because it reduces tokens in one example.
This loop compounds because each validated correction removes a specific source of waste from the remaining problem. Later iterations can address narrower failures with better evidence instead of repeatedly reopening the same broad diagnosis. The improvement record also preserves the connection between a symptom, its cause, and the change that addressed it.
What each harness layer looks like as an optimization target in practice
Prompt traces often show system instructions as the largest single token consumer, with boilerplate and policy definitions repeated verbatim on every call. That evidence makes redundant text a concrete trimming target whose savings can be compared before and after the change. Global policies and strategies belong in prompts, but deterministic execution constraints belong in narrowly scoped middleware. Separating those responsibilities avoids using more instruction text to enforce behavior at the wrong boundary.
At the tool layer, traces can show when schemas take up too much space and when the same invocations happen within a single trajectory. When there are many tools, deferred tool loading cuts the number of schemas processed on every call, so it uses fewer input tokens and makes fewer misselection errors. Tool boundary middleware is a place to enforce deterministic constraints without making the prompt longer. Redundant invocations likewise call for a deterministic constraint at that boundary rather than a general appeal to the model to be more economical.
At the memory layer, growing context across turns reveals the cost of injecting more history. More history can increase token spend while degrading output quality. Distilling experience into reusable knowledge stored outside the context window addresses that burden and improves retrieval precision.
Workflow traces make coordination overhead measurable by comparing coordination-chatter token counts with task-completion tokens. Repeated calls and non-terminating branches point toward structural changes, including iteration caps, early-exit conditions, and collapsing unnecessarily nested subagent calls. Disabling subagent nesting by default can prevent delegation loops. These changes address execution structure rather than asking a revised prompt to compensate for it.
Evaluation has distinct surfaces as well. Unit tests cover deterministic correctness in tools and policies, while task-level replay checks fixed prompts against expected artifacts or state transitions. Long-horizon harness evals cover resumption, compaction, approval branching, and subagent handoffs. Each layer catches regressions that the others can miss, so a passing unit test is not evidence that an entire workflow remains sound.
Why replay validation is the gate that prevents optimization from introducing new failures
A harness change can cut costs in common cases and still break behavior in other cases. A prompt trim, for example, may remove a constraint that prevented hallucination on edge cases. The prompt trim reduces tokens at once, but unit tests may miss the loss of that protection. Replay against production traces checks how the change handles the cases that needed those instructions.
The golden set provides that test surface through curated traces with expected outputs or rubric scores. It includes common cases and known failure modes and runs through the changed pipeline before it goes into production. Tracking aggregate scores over time makes a sustained decline a reason to investigate before it becomes an unexplained production surprise. Running replay on every deploy also subjects both small prompt edits and major releases to scrutiny.
A build fails when new prompts push mean tokens per request above a defined budget or a model change raises per-call cost above a defined threshold. These gates treat cost as a quality dimension without substituting it for correctness. Replay must also show that the attributed fix improves the cases where the failure originated.
Maintaining the golden set requires engineering effort, but that burden is lower than debugging unvalidated regressions in production. Validated cases become evidence for future iterations, while unchecked changes accumulate hidden failures. Without it, optimization returns to the same after-the-fact forensics that trace instrumentation was meant to replace.
Building the flywheel as an engineering discipline rather than a one-time project
Trace capture and structured attribution run on every deploy and during incidents. Golden-set maintenance is owned, versioned, and reviewed like a test suite. Cost regression gates belong in CI, and harness changes go through the same review as code changes, from a proposed change to evidence and validation before they reach deployment.
This is a standing engineering responsibility because the harness contains many independently changing parts. Treating prompts, tools, memory, and workflow logic as incidental configuration leaves their combined cost unmanaged. Treating them as testable engineering surfaces makes each intervention narrower and its effect easier to establish. The durable answer to agent cost overruns is not simply a cheaper model, but a continuous loop that identifies unnecessary work and proves that removing it preserves task quality.


