O(N^2) History Re-feeding Trap and Closed-Loop Context Pruning

DEEP DIVE SERIES · #10

The O(N²) History Re-feeding Trap: How Multi-Turn Sessions Silently Explode Enterprise Token Budgets

5 min read

When enterprise engineering teams begin deploying autonomous AI coding agents (such as Claude Code, Cursor, or custom multi-agent harnesses), their initial financial projections assume a linear cost model:

Projected Cost ≈ Turns × Average Turn Tokens × Price per Token

Within weeks of real-world deployment, the finance department sounds the alarm. A single engineer’s session has consumed 2.6 million tokens across 40 turns, costing $45 for a task that should have taken $1.50.

The root cause of this budget blowout is the O(N²) History Re-feeding Trap.


Loop Engineering and Session Bloat

1. The Mathematical Reality of Cumulative Prompt Ingestion

Unlike web servers that maintain persistent state on the backend, LLM inference endpoints (Anthropic, OpenAI, Google) are strictly stateless HTTP APIs. While frontier providers offer prefix caching (such as Anthropic Prompt Caching with a 5-minute TTL) to discount repeated prefixes, any cache eviction, dynamic context injection, or mid-session file edits trigger full cache re-computation.

To provide continuity in a multi-turn conversation, every request to the API must transmit the entire conversation history from Turn 0 to the current turn N:

Prompt Tokens at Turn k = System_Tokens + Sum(User_i + Assistant_i + Tool_i)

If each turn adds an average increment of Delta tokens (file reads, compiler outputs, agent reasoning), the cumulative tokens billed across an N-turn session follows the arithmetic sum:

Total Tokens Ingested = Sum(k * Delta) = (N * (N + 1) / 2) * Delta ≈ 0.5 * Delta * N²

This is quadratic complexity O(N²):

Turns (N)    Linear Perception (N * Δ)    Actual Billed Ingestion (½ N² * Δ)
  5 turns              25,000 tokens               75,000 tokens
 10 turns              50,000 tokens              275,000 tokens
 25 turns             125,000 tokens            1,625,000 tokens
 50 turns             250,000 tokens            6,375,000 tokens (!)

By turn 50, you are not paying for 250,000 tokens. You have been billed for over 6.3 million tokens because the earlier tool outputs and file reads were re-transmitted and processed on every single step!


2. Anatomy of “Zombie Bloat” in Agent Transcripts

Why do real-world transcripts accumulate so many tokens? When inspecting session logs (such as .jsonl files in Claude Code or Antigravity), three categories of Zombie Bloat dominate:

1. Dead Tool Outputs (Superceded Results)

On Turn 3, the agent executed cat package.json (400 tokens). On Turn 12, the agent updated package.json with a new dependency. The output from Turn 3 is now dead information, yet it continues to be re-transmitted on Turns 4 through 50.

2. High-Entropy Media & Base64 Traps

An agent uses a browser tool to capture a screenshot for UI validation. The browser subagent returns a 180 KB Base64 string (~55,000 tokens). Even though the agent looked at the screenshot on Turn 8, that raw string sits inside the history, adding 55,000 billed input tokens to every subsequent turn.

3. Verbose Compiler and Lint Traces

When a build fails, tools like tsc or cargo build can dump 400 lines of cascading errors. The agent fixes the missing semicolon on the next turn, but the 400 lines of obsolete error traces remain permanently embedded in the transcript.


3. The Surgical Pruning Solution

You cannot simply truncate the conversation by deleting the top half of the history, because that destroys the foundational context (the original user goal and project boundaries).

Instead, production systems employ Surgical In-Place Pruning:

[ Raw Session Transcript (.jsonl) ]
                 │
                 ▼
     [ Transcript Auditor Engine ]
  Identifies zombie tool results & dead media
                 │
                 ▼
  [ Replace Dead Results with Trivial Stubs ]
  {"tool_result": "<pruned: 48,000 token image - validated in step 8>"}
                 │
                 ▼
   [ Slashed Transcript (49% to 75% Reduction) ]

Real-World Case Study: Pruning a 2.6M Token Session

In our benchmarking lab, we audited an autonomous session transcript (ea29aa7b-0d9a-4150-88e7-ff381c8fbcc8) that had grown to 11,280,000 raw cumulative tokens with severe context saturation.

By applying deterministic transcript surgery:

  • We located 273 dead tool results (terminal output loops and superseded diff chunks).
  • We stripped out 4 uncompressed base64 image captures.
  • Immediate Result: Token footprint slashed by 5,522,655 tokens (49% reduction).
  • Execution Cost: Dropped from an ongoing $38/session trajectory down to $14/session.
  • Accuracy: Needle-in-a-haystack retrieval accuracy improved from 64% back to 96%.

4. Architectural Rules to Tame Quadratic Growth

  1. Implement Automated Tool Result Expiration: After 5 turns, if a tool result has not been referenced, replace its detailed payload with a one-line summary stub.
  2. Offload Media Immediately: Never let a subagent return a base64 string directly into the main conversation. Save media to /artifacts/ and return a markdown image path.
  3. Trigger State Handover at Turn 20: When a session reaches 20 turns, execute a structured State Handover: summarize active progress into a checkpoint artifact, wipe the transient tool history, and re-anchor the agent with a clean slate.

Treating token consumption as an active memory lifecycle rather than a passive log file is the only way to scale agentic systems without blowing enterprise budgets.

🏛️ Systems Engineering Pillar (Layer 5)Foundational Knowledge

The 7 Layers of AI Systems Engineering: From Foundation Models to Shared Meaning

Loop Engineering (Layer 5) must actively manage history re-feeding to keep enterprise multi-turn sessions viable.

Newsletter Edition #1

Subscribe to The AgentJunky AI Brief

Get practical thinking on agentic AI, enterprise architecture, RAG, and production governance delivered straight to your LinkedIn feed.

Subscribe on LinkedIn ↗

Discuss an AI Opportunity

Need an architectural review, help transitioning an agent prototype to production, or designing governed tool gateways? Let's connect.