Why AI Agents Fail in Production and How to Fix Context Rot

An agent can start with the right plan and still finish with the wrong fix. Learn how to spot stale context in a trace, give the agent better evidence and verify that the repair works.

An amber scanner illuminates the current teal trace sheet among older translucent copies

Published

The agent is solving yesterday’s problem

Ten minutes into a coding task, the agent finds the right function, reproduces the failure and makes a reasonable edit. An hour later, it is debugging a version of the file it already replaced. A passing test appears in the trace, but it covered a narrower scenario than the one that brought the team here.

That is what context rot looks like in production. As an agent accumulates old observations, duplicate files and noisy logs, it can become less reliable at choosing the evidence that applies to its next decision. Open the trace around the first wrong turn and look at what the agent could see at that moment.

What breaks as the context grows

The context window is the input the model can work with on a given turn. It can include instructions, conversation history, tool definitions, retrieved documents and previous tool results. Chroma found that performance varied with input length and the presence of distracting information in controlled tasks.

A file read, a later edit and a test result can all be accurate records. The agent still needs to know which revision each record describes and whether the test exercised the original failure. A single oversized tool response can crowd out more useful evidence than many small calls, so tool count alone is a poor signal.

  • When the same file appears repeatedly, include its revision and reread the changed range.
  • When a stack trace buries a failure, surface the assertion, test name and run reference first.
  • When a resumed plan carries an old assumption, compare the checkpoint with the current specification and checkout.

Rebuild context around the next decision

Compaction replaces a long transcript with a smaller checkpoint. A natural time to compact is after a completed subtask or when repeated tool output starts taking space from the next check. Keep the approved specification and repository instructions available by revision, then reload the few files that matter for the next action.

Suppose an agent is fixing duplicate inventory updates. The checkpoint should identify the candidate revision and carry forward the failing concurrency test. A theory about where the transaction goes wrong belongs next to the test result with a clear label that it still needs verification.

After the reset, the agent should be able to name the acceptance rule, the current failure and the next check. Refresh file excerpts and test references when the revision changes. Keep full logs and earlier patches available by reference.

A workstation reduces a stream of trace sheets to a small checkpoint card beside a current specification
A compact checkpoint carries forward the verified state and points back to the approved specification.

Keep a progress note someone else can resume

A progress note should tell the next session where to begin and what it can verify. Update it when the work moves to a new state, and attach a real run reference to each confirmed result.

For the inventory repair, write down the goal of one stock adjustment per customer account and event. Record the approved specification, current revision and passing sequential replay. Keep the remaining failure visible. Two simultaneous deliveries can still create two outbox records. The next step is to rerun that test and inspect where the application claims ownership of an event.

When several workers are investigating, keep separate notes and assign one coordinator to update the shared checkpoint. Derive acceptance status from the verifier so an overwritten note cannot erase a known failure or turn a hypothesis into a passing result.

Give bulky investigations their own context

A worker can inspect years of migration history to answer one narrow question about event identity. Ask it to return the relevant migration references, the constraints it observed and any unanswered questions. The main agent can check those references before changing the code.

Set a time or tool budget and specify what the worker should return. Keep one owner for a patch when changes touch the same code and depend on shared decisions. Separate context works especially well for research with a small deliverable or a review that benefits from examining the candidate independently.

Two workstations separated by glass exchange a small set of evidence cards
An investigation can keep its raw evidence in a separate context and return the few findings the main agent needs.

Limit tool output before it reaches the model

A line limit can still admit one enormous line. Enforce a byte cap in the tool adapter to bound the raw data sent into the model, then monitor token usage separately. Different tools can have different limits based on what their output needs to communicate.

Start with a structured test summary. Include the process exit status, failed case, decisive assertion and whether the excerpt was cut short. If the full result is needed, provide a reference and an offset so the next request can retrieve the relevant section.

A reader that closes a pipe early can affect how the child process exits. A production runner can continue draining standard output and standard error while collecting a size limited excerpt. If it retains the full output, apply access controls and a retention policy.

A brass aperture sends a few diagnostic strips to a teal station and diverts the rest into an archive tray
Give the agent the smallest useful excerpt and keep a reference to the full run when it needs more detail.

Replay the same task to see whether the repair works

Run the same task with the same repository snapshot, model and verifier under both context policies. Include a run where an important decision disappears after a reset, another with a stale file read and a third with an oversized or untrusted tool response. Compare acceptance rate, time to a verified result, tool output bytes, model input tokens and human corrections.

Compare a run that succeeded after compaction with one that failed. Inspect the input immediately before the reset and the first consequential action afterward. If the requirement is absent in both places, check retrieval and the approved specification. If it vanishes at the reset, change what the checkpoint carries forward. If the agent still violates a visible requirement, add an executable check for that behavior.

Keep the specification revision, candidate revision, policy version and references to decisive checks with each run. That gives the next engineer enough evidence to reconstruct the failure without reading the entire conversation.

Common questions about context rot

How many tool calls cause context rot? There is no fixed count. A few large or conflicting results can cause more trouble than many small, current observations. Check whether the agent can still identify the constraint that governs its next action.

Can a larger context window fix it? A larger window gives the agent room for more evidence. It still needs reliable revision references, limits on noisy output and a way to retrieve the current source.

How do compaction and persistent memory differ? Compaction rebuilds the working context from a smaller representation. Persistent memory stores decisions, progress and evidence references outside the window so a later session can load them.

Should every tool have the same output limit? Set a limit based on the tool’s job. A test summary and a source excerpt carry different kinds of evidence, and each tool should make truncation visible.

Tagged
  • Context engineering
  • AI agents
  • Agent reliability

Want this applied to your project?

Tell us what you are building and we will help you define the technical plan behind it.

Contact via WhatsApp+92 300 8698044
Cost Estimator
Message us on WhatsApp