IamBishal

Ticket Thirty: Context Rot Inside a Tool Loop

My context-engineering article admits context rot has never been measured inside a tool loop. So I gave an agent a long queue, two standing instructions, and six ways to manage its window.

One agent works a 24-ticket support queue in a single window under six context policies, 720 tickets on Claude Haiku and Sonnet. No context rot up to 73k tokens, no instruction lost; the remedies changed cost, not accuracy.

frozen refunds escalated, across all six policies
0 / 150
running summaries that kept both instructions
230 / 230
Sonnet cost per queue with trimmed tools
−56%
unambiguous tickets solved
629 / 630
  • Experiment
  • 7 min read
  • AI Architecture
  • Evals & AgentOps
  • Python
  • Claude
  • AI agents
  • Context engineering
Figure 1. The window under each policy

Overview

In "Context Engineering for AI Agents" I cited two studies showing models use long inputs unevenly, then admitted that neither measured an agent: "I'm not aware of either effect being measured inside a tool loop." The article's remedies were concrete: trim tool output, clear stale results, summarise with constraints pinned, start fresh windows, and keep the tool count down.

So I measured them. One agent works through a queue of 24 support tickets in a single conversation, over real Kestrel Outfitters orders. Two standing instructions arrive once, in the opening message: an escalation freeze that changes the right outcome of 5 tickets, and an audit flag on every ticket. They sit in the conversation, not the system prompt, because that is the part a compaction rewrites.

Six context policies decide what each request's window holds: everything (raw), trimmed tool output, cleared stale results, a running summary, the same summary with the instructions pinned, and a fresh window per ticket. Untrimmed lookups return about 1,060 tokens each, so the raw window reaches 73k tokens by the last ticket. A second study runs each ticket alone with 10 tools or 40, thirty of them look-alikes.

Grading reads only the ledger of tool calls. Before any paid run, an oracle and a forgetful fake proved the grader catches a lost instruction. 30 queues and 144 tool episodes cost USD 10.77; Sonnet ran 2 queue orders instead of 3 to fit the cap.

The answer surprised me. No policy declined across the queue, the freeze held on all 150 tickets it applied to, and the generic summariser kept both instructions in all 230 summaries. The remedies paid off in cost: trimming cut Sonnet's bill per queue by 56%, a fresh window per ticket by 61%. The failures that did occur came from my design, and both are disclosed: three tickets over already-returned orders, and a stub wording that made the model distrust its own history.

Did it hold up?

Full results

The hypothesis, written down before the first run, and what the results said.

  1. Wrong

    Context rot shows up inside a tool loop: under raw, accuracy or instruction-following declines from the first third of the queue to the last.

    No decline up to a 73k-token window. Both models kept the freeze and the audit flag on every raw ticket; the only misses were returned-order handoffs.

  2. Confirmed

    Trimming at the tool boundary removes most of the input with no worse accuracy.

    Mean window down 64-65%, cost down 56% (Sonnet), 100% of unambiguous tickets solved, the same as raw.

  3. Partly

    Clearing stale tool results is nearly lossless.

    Lossless with a stub that says what happened; the first wording cost 9 tickets. Cost fell far less than tokens, because clearing rebuilds the cache.

  4. Wrong

    An unpinned summary loses constraints: it breaks the freeze or drops the audit flag more often than a pinned one.

    All 230 summaries kept both instructions; 0 freeze breaks. The one summary failure was a stale open item, under the pinned policy.

  5. Confirmed

    A fresh window per ticket is the accuracy ceiling, and the cheapest per ticket.

    100% of tickets on both models, and the cheapest policy: USD 0.41 per Sonnet queue against 1.07 for raw.

  6. Wrong

    Tool selection degrades with 40 similar tools.

    No call to any look-alike in 144 tickets; every ticket solved with 10 tools and with 40. The extra tools cost tokens only.

What it does

  • A 24-ticket queue in one window, with two standing instructions given once
  • Six context policies: raw, trim, clear, summary, summary+pin, fresh
  • Realistic verbose tools (about 1,060 tokens a lookup) against trimmed ones (about 60)
  • A tool-count study: each ticket with 10 tools or 40, thirty of them look-alikes
  • Code-only grading from the tool-call ledger, broken down by queue position
  • An oracle and a forgetful fake that prove the grader sees a lost instruction
  • A hard spending cap checked before every model call, shared by concurrent runs
  • Every running summary checked for each instruction; full transcripts committed

How it's built

  1. Inputs

    • 24-ticket queue3 shuffled orders
    • Two standing instructionsEscalation freeze, audit flag
  2. Six context policies

    • raweverything
    • trimshort tool output
    • clearstale results stubbed
    • summary, summary+pin
    • fresh window per ticket
  3. Outputs

    • Ticket grades720, from the ledger
    • Instruction checksFreeze, audit, summaries
    • Window and costTokens per call, USD
policies.py
# policies.py: six ways to turn one agent's history into the next request's window.
STUB = ("[Result removed from the context after {ticket} was finished, to save space. You received the full "
        "result at the time and acted on it. The call was {call}; call the tool again if you need the details.]")

def build_messages(policy, logs, summary):
    current = logs[-1]
    msgs = []
    if policy in ("raw", "trim", "clear"):          # trim differs in the tools' output, not here
        for i, log in enumerate(logs):
            add_user(msgs, opening_for(log.ticket_id) if i == 0 else announce(log.ticket_id))
            msgs += stub(log) if (policy == "clear" and log.closed) else log.turns
        return msgs
    if policy == "fresh" or summary is None:          # a new window per ticket
        add_user(msgs, opening_for(current.ticket_id))
        return msgs + current.turns
    head = f"Summary of the work so far (it replaces the earlier conversation):\n{summary}"
    if policy == "summary+pin":                       # the standing instructions, word for word
        head = f"Standing instructions from the start of the queue, kept word for word:\n{CONSTRAINTS}\n\n{head}"
    add_user(msgs, f"{head}\n\n{announce(current.ticket_id)}")
    return msgs + current.turns

Figures

Figure 1. The window under each policy1 of 4

What I learned

  • Measure before you engineer. Up to 73k tokens, with explicit instructions, neither model showed context rot; the remedies earned their place on cost, not accuracy.
  • Trimming at the tool boundary is the cheapest win: two thirds of the window gone, cost more than halved, accuracy unchanged.
  • A stub is a message to the model. "Cleared" read as "you never had this", and Haiku stopped working on 9 tickets. Say when and why a result was removed.
  • Clearing breaks the cached prefix once per ticket, so it cut Sonnet's tokens by 55% but its cost by only 27%.
  • Labelled instructions survive summaries. The generic summariser wrote its own "standing instructions" section every time.
  • Summaries carry more than constraints. One kept a stale "pending approval" item alive, and the agent stalled on it; pinning didn't help.
  • Check your ground truth against the data. Three of my orders were already returned, and the careful answer, a handoff, scored as a failure.

Next steps

  • Drop already-returned orders from the ticket builder and rerun
  • A queue of 60 to 100 tickets, past 150k tokens
  • Instructions phrased casually mid-conversation, not labelled up front
  • Tickets that depend on earlier ones, so a policy has to carry state
  • Look-alike tools with overlapping descriptions, not just similar names

Run it yourself

Everything on this page comes from code and data in the repository. The dry run is free and needs no API key. A full live rerun costs about USD 11.

Terminal
git clone https://github.com/bishal-saha/ai-lab-code.git
cd ai-lab-code/context-rot-benchmark
uv sync
uv run ticket-thirty --dry-run       # an oracle and a forgetful fake, $0
mv results results-published         # the live runner resumes from committed results
cp .env.example .env                 # then add your ANTHROPIC_API_KEY
uv run --env-file .env ticket-thirty --max-usd 15
uv run python -m ticket_thirty.report

Bishal SahaLet's build together

Have a system to design, or an AI feature to get right?

Tell me about the problem. You'll get a clear, honest view of the best way forward.

  • Reply within 24 hoursOn business days, with my honest view and a next step.
  • Accepting select engagementsArchitecture, AI features and full builds.
  • Remote, worldwideWorking with startups, SMEs and enterprises.