Why pay for 50,000 tokens of agent history when only 3 facts are still true?

Because transcripts pile up obsolete tool outputs, superseded decisions, and dead turns. ContextGC deterministically garbage-collects your prompt in <3ms — slashing tokens by up to 66% and preserving active state without calling another model.

TERMINAL pipeline_trace.json — airline_reservation_turn_6
39.6% smaller
What the agent was sent uncompiled transcript
:: system: You are an airline support agent.
> user: I need to change the cabin on reservation
> 0U4NPP. I am in economy class.
! assistant: Confirming reservation 0U4NPP.
! <contextgc-state>{"assert":{"active_reservation":
! "0U4NPP","cabin_class":"economy"}}</contextgc-state>
> user: Upgrade me to business class.
! assistant: Upgrading you now.
! <contextgc-state>{"assert":{"cabin_class":
! "business"}}</contextgc-state>
> user: Actually, keep it in economy, I changed
> my mind.
assistant: Reverting to economy.
> user: Thanks.
▌
What the model receives −39.6% tokens
:: [ACTIVE_AGENT_STATE]
- active_reservation = "0U4NPP" (turn 2 [declared])
- cabin_class = "economy" (turn 6 [declared])
:: system: You are an airline support agent.
assistant: Confirming reservation 0U4NPP.
> user: Upgrade me to business class.
> user: Actually, keep it in economy, I changed
> my mind.
assistant: Reverting to economy.
> user: Thanks.
▌

The business upgrade is gone because the user reversed it, and the register states the value that survived. Nothing was inferred and nothing was invented — the agent said which key it had changed, as a side-effect of a turn it was already making.

The Problem

Why autonomous agents break down after 30 turns

As multi-turn dialogues grow, language models struggle with state conflict, payload bloat, attention loss, and provider cache eviction.

01 • State Conflict

Contradiction & Ghost State

High Severity

A user updates their delivery address or flight date 15 turns in. Later, when executing an action, the LLM hallucinates between both values in context or silently executes against the obsolete state.

Transcript Drift Trace State Collision
# Turn 3: User sets initial invariant
> user: Book flight to "SFO" on Oct 12.
# Turn 18: User updates their constraint
> user: Actually change that to "JFK" instead.
# Turn 28: LLM sees both turns, resurrects stale state
! error: Agent executed action with destination="SFO"
02 • Token Inflation

Tool Payload Bloat & Token Bleed

Token Inflation

Bash command runs, SQL tables, and vector lookups dump hundreds of lines of raw tabular noise that no future turn ever reads again. Token bills explode, latency spikes, and attention evaporates.

Payload Growth Trace +340% Overhead
# Turn 8: Bash tool outputs verbose tabular trace
> tool_call: bash_run("git status && ls -la")
# Payload: 8,420 tokens dumped into context window
> tool_output: [8,402 irrelevant bytes retained]
# Turns 9-40: 99.8% of bytes never read again
! cost: $0.84 wasted per turn on dead payload
03 • Attention Loss

Lost-in-the-Middle Decay

Attention Loss

Critical user constraints and business invariants placed in the middle of a 40+ turn transcript suffer from U-shaped attention degradation. The LLM quietly ignores constraints without throwing an error.

Invariant Attenuation Trace Constraint Lost
# Turn 12: Business safety invariant declared
> system: "Always require supervisor approval before charging."
# Turn 35: Invariant enters middle-context blind spot
> attention: Attention weight falls to 0.12 (-78%)
# Turn 42: LLM bypasses invariant silently
! violation: Agent charged card directly without approval
04 • KV Eviction

Prompt Cache Thrashing

0% KV Hit Rate

Naive pruning or sliding-window truncation mutates early message boundaries on every turn. This breaks byte-identical prefixes, invalidating provider KV caches and driving TTFT to multi-second delays.

Prefix Mutation Trace Cache Eviction
# Turn 15: Sliding window prunes Turn 1-2 headers
> prefix: Byte-exact prefix stream mutated
# Provider prompt cache cannot match altered prefix
> kv_cache: CACHE MISS (Re-encoding 32k tokens)
> latency: TTFT spikes from 85ms to 2,420ms
The Architecture

The 4-stage context compilation pipeline

Each step is a pure, total mathematical function over the transcript. Zero model in the loop, zero stochastic drift, and identical input always yields identical output.

PIPELINE ACTIVE ContextGC · DAG Orchestrator
Capabilities

Engineered for safety, speed, and determinism

Everything required to operate production AI agents reliably across hundreds of turns.

DAG Engine 01

Deterministic State DAG

Mathematical tracking of entity lifecycles. When a key is re-asserted, the prior turn is retired; when a key is voided, it is cleanly struck from active state.

Safety Gate 02

Safety-First Compactor

Compresses multi-row tool payloads by up to 80% while guaranteeing that any row carrying allergen, PII, security, or expiry signals is strictly preserved.

Cache Align 03

KV-Cache Prefix Preserver

In cache_friendly mode, unchanged prefix messages are emitted byte-identical, retaining provider KV cache hits across model generations.

Dual Path I/O 04

Dual Read & Write Path

Extracts implicit facts using measured regex schemas on the read path, or receives direct declarations from the agent via <contextgc-state> blocks.

Evidence Gate 05

Lexical Evidence Gate

A turn is never retired if it remains the sole supporting evidence for an active fact. Orphaned assertions are mathematically prevented.

Pure Python 06

Zero-Dependency Core

Written in 100% pure standard-library Python (3.9–3.13). Installs instantly, needs no PyTorch or heavy NLP dependencies, and compiles in <3ms.

Empirical Evidence

Measured on 180 real agent trajectories

Not a benchmark of the compiler grading itself. Three public corpora, real model output, and a hand-labelled sample for the precision figure. Every number below ships with the n it came from.

Empirical Evidence · Paired Measures

Token Reduction vs. Compiler Precision

Grouped horizontal comparison across 3 distinct production agent domains on a unified 0–100% scale.

Token reduction
Compiler precision
MEASUREMENT BENCHMARK RATE (0 — 100%) VALUE
Coding 40 transcripts
SWE-agent bug-fixing, 20 repos · 161 turns retired · 668 payloads compacted

Token reduction

66.5%

Precision

100% n=36, CI 90–100%

Airline support 60 conversations
APIGen-MT-5k, reservation tool use · 9 turns retired · 138 payloads compacted

Token reduction

59.0%

Precision

100% n=72, CI 95–100%

Retail support 80 conversations
APIGen-MT-5k, order and exchange tool use · 2 turns retired · 275 payloads compacted

Token reduction

62.5%

Precision

100% n=47, CI 92–100%

0% 25% 50% 75% 100%

Reduction transfers across all three; supersession does not. Coding re-asserts a key 77 times across 40 transcripts, retail twice across 80 — so the two support corpora gain most of their reduction from compacting tool output, not from retiring turns. Reported separately because averaging them would hide it.

Specifications

Tech stack & integration architecture

Designed from first principles to be embedded in any Python agent runtime without friction.

Quick Start Integration
jaygopal@macbook:~/contextgc — bash
jaygopal:~$ pip install contextgc
✔ Preflight checks passed: pure Python 3.9+, 0 dependencies.
✔ Successfully installed contextgc-0.1.0
jaygopal:~$ python3 -m contextgc.compile --schema travel --mode cache_friendly transcript.json
✔ Loaded transcript: 14 turns (1,842 tokens)
✔ Invariants verified: refunds over 500 checked
✔ Compiled context: 7 turns (1,112 tokens, −39.6% reduction)
✔ Latency: 2.14ms | 0 LLM calls | 0 stochastic drift
jaygopal:~$ python3 run_agent.py
Reduced context by 39.6% in 2.14ms