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.
:: 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.▌
:: [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.
As multi-turn dialogues grow, language models struggle with state conflict, payload bloat, attention loss, and provider cache eviction.
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.
# 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"
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.
# 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
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.
# 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
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.
# 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
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.
Everything required to operate production AI agents reliably across hundreds of turns.
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.
Compresses multi-row tool payloads by up to 80% while guaranteeing that any row carrying allergen, PII, security, or expiry signals is strictly preserved.
In cache_friendly mode, unchanged prefix messages are emitted byte-identical,
retaining provider KV cache hits across model generations.
Extracts implicit facts using measured regex schemas on the read path, or receives direct
declarations from the agent via <contextgc-state> blocks.
A turn is never retired if it remains the sole supporting evidence for an active fact. Orphaned assertions are mathematically prevented.
Written in 100% pure standard-library Python (3.9–3.13). Installs instantly, needs no PyTorch or heavy NLP dependencies, and compiles in <3ms.
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.
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.
Designed from first principles to be embedded in any Python agent runtime without friction.