Skip to content

Agents and LLMsHarness and toolingreference

Context, prompt cache, and compaction in a coding-agent harness

Every request a coding-agent harness sends to its model provider is the system prompt, then the tool definitions, then the conversation so far. Providers charge by the token and reuse work across requests through a prefix cache that survives only until the first byte that changed. This page covers what each request carries, what breaks the cache, how stale tool output is reclaimed without breaking it, and how compaction works once the window is full.

The measurements are from one harness - pi, extended with a stack of context and cache extensions, running against openrouter/deepseek/deepseek-v4-pro and a local engine. The cache-break and compaction-trial numbers are measured; the pruning and spilling thresholds are the design values in the extension sources. Where a claim is documented rather than measured, the evidence table at the end says so.

  • A fresh session sends a 193.6 KB system prompt and 114.4 KB of tool definitions (139 tools) before the conversation starts.
  • The system prompt is position 0 of the request, so any change to it is a full cache miss. It should stay byte-identical across a session.
  • Six extensions return a whole systemPrompt from before_agent_start. One measured instance, a one-shot semantic-inject block, changed the prompt at offset 40,224 bytes and cost two misses per session until it moved into a message.
  • Pruning used to mutate an earlier message almost every turn, which re-prefills the whole prompt on a local engine (~35 s at 150k tokens). Hysteresis fixes it: prune once a batch crosses the threshold, down to a low watermark, and never un-prune.
  • Oversized fresh output is spilled to disk: a 51,200-byte preview plus a locator replaces the full text when a tool result lands.
  • Compaction has two paths. pi’s default re-serialises the history under a new system prompt and has none of prefix replay’s failure modes. Prefix replay re-sends the last real request with one summarise instruction appended.
  • On the local engine prefix replay was slower than the default, because the engine reused its cache only when a request extended its whole previous sequence (prompt plus generated reply). Since the 2026-11-01 engine update it also reuses a shared prefix up to the last checkpoint before the divergence, and replay is now 5-9 s faster than the default when the session’s cache is warm; after a model swap the cache is cold and replay loses by 13-40 s. Prefix compaction is off by default for every provider.

The /context inspector splits every provider request into three parts and reports their sizes plus where the request stopped matching the previous one. The split is a system prompt (sections delimited by markdown headings and XML (Extensible Markup Language) blocks), a tool set, and the message history.

one provider requestsystem prompt193.6 KB (position 0)provider prefix cachereuses up to first difffirst bytetool definitions114.4 KB, 139 toolsmessages(conversation)

A fresh TUI (terminal user interface) session, two prompts in, measured: system prompt 193.6 KB, 139 tools at 114.4 KB. Both are sent before the first byte of the conversation and are the same on every request, so a cache can serve them. The message history grows from zero; the prompt and the tools do not.

The inspector derives the sections by splitting the system prompt at top-level # / ## headings and at XML block openers, so the report shows which section moved, not just that something moved. It fingerprints each section, each tool definition and each message, and compares against the previous request. It reports three kinds of difference: the system prompt changed (and which sections), the tool set changed, or an earlier message was rewritten by pruning or compaction, which is a miss from that message onward.

Why the system prompt is the cache’s first byte

Section titled “Why the system prompt is the cache’s first byte”

A provider prefix cache (Anthropic, DeepSeek, OpenRouter pass-through, and a local engine’s KV (key-value) reuse) serves bytes from the start of the request up to the first byte that differs from the previous request. The system prompt is the start of the request, so it is the first thing that has to stay identical.

pi records the initial prompt and tool set in the transcript’s first system message, then appends prompt and tool changes before the next model request instead of editing earlier bytes.1 The extension docs say to change prompt sections, selected tools or guidelines so pi can append a transcript delta; returning systemPrompt (or setting forceSystemPrompt) replaces the whole prompt for that run.1

The harness config repo has six extensions that return a full systemPrompt from before_agent_start:

ExtensionInjects
memorythe memory block
session ledgerthe recent-sessions block (and the semantic one-shot, below)
style togglethe output-style rules
local-model rulesrules for the local engine
veans-prime.tsVikunja workflow priming
tool routingthe tool-routing prepend

Each is injected at the front of the prompt, so a change to its string is a cache miss. Four are stable within a session. The memory block changes when a memory is saved mid-session, and the semantic one-shot appeared on one prompt and vanished on the next.

A semantic retrieval block - prior sessions matched by embedding against the user’s first substantive prompt - was injected into the system prompt once per session: it appeared on the prompt that first qualified and was absent from the next, so the prompt changed twice per session.

/context showed it: the system prompt changed on prompt 2, with the block inserted about 200 lines in, so the cached prefix ended there. The block landed at offset 40,224 bytes into the prompt - a full miss on the prompt it fired, and a second full miss on the prompt where it vanished.

The fix moved the block out of the system prompt and into a message. A message appended to the conversation and kept in history does not touch the cached prefix. Verified in a TUI session after the change: 3 requests, 0 system-prompt changes, where the same prompts before the change produced 1 change at offset 40,224.

The memory block was the next cause. Over a day of real use after the semantic fix (2026-10-20/21), the per-request log recorded 747 interactive requests, 15 with a changed system prompt: 13 in the memory block (a memory saved by this session or another one rebuilt the block on the next turn) and 2 in the tool-routing rules (the rules file itself was edited). The block is now built once per session; a memory saved mid-session stays reachable through the memory tool and reaches the next session’s prompt.

The prompt holds scaffolding that is identical on every request (tool-routing rules, the documentation preamble, the local-model rules), and anything that appears once and disappears goes in a message.

Pruning: hysteresis over per-turn mutation

Section titled “Pruning: hysteresis over per-turn mutation”

Tool results (reads, bash output, search results) are the largest and fastest-ageing part of the context; the extension header puts them at typically 80% or more of the bloat in a long session. The harness reclaims them by walking the message stream backwards and replacing old tool-result content with a short marker, keeping conversation flow and decisions intact. The open question is when to run that replacement.

The first version pruned down to a fixed protect window every time new output pushed one more old result past it. That moved the boundary almost every turn, and the newest-result exemption sometimes shrank the pruned set - 11 results collapsed to 4 - un-pruning content that had already been collapsed. On a local engine every edit to an earlier message is a full re-prefill, about 35 s at 150k tokens, so a per-turn prune was a per-turn re-prefill.

Hysteresis fixes it (design note dated 2026-10-10 in the extension header). Pruning fires only when the bytes beyond the protect window reach a trigger floor; when it fires it prunes down to a low watermark, not to the window edge, so the boundary jumps once per batch and then stays still until the next trigger worth of output accumulates. The pruned set is monotone - a result that was pruned never comes back. Below the trigger floor, the per-request layer decides on a clone of the messages, so sub-floor turns leave the prefix byte-identical.

Three constants set when the prune fires and how far down it goes:

ConstantValueMeaning
Protect window160,000 bytes~40,000 tokens of recent tool output preserved verbatim
Trigger floor80,000 bytesdo not prune unless bytes beyond the window would reclaim at least this
Low watermark80,000 bytes (window / 2)on fire, prune down to here so the boundary stays still

Each is overridable (PI_PRUNE_PROTECT_BYTES, PI_PRUNE_MIN_BYTES, PI_PRUNE_LOW_BYTES). The newest non-pruned result is always kept verbatim; protected tools (skill, memory, todowrite, question, session-search, bookmark) are never pruned; image-bearing results stay out of the text budget, because a single image read is ~290 KB of base64 and would blow the 160 KB window on its own. Since 2026-10-30 they have their own budget instead (PI_PRUNE_IMAGE_BYTES, default 16 MiB of base64): the newest image is always kept, older image results are pruned once the total passes the budget. Before that, images were exempt outright and a long session kept accumulating them until the provider rejected the whole request (OpenRouter caps downloaded image content at 30 MB).

Two layers make the identical decision. The persisted layer runs at turn_end and writes context_edit entries to the session file, so a prune decision survives restarts and compaction without recomputation. The per-request layer runs on the context event as a within-turn safety net for a long agentic turn that accumulates many large outputs between turn ends, and short-circuits on already-pruned markers.

A replay of recorded sessions on 2026-11-20 found that the per-request layer never applied (it returned the original message array, which the runner reads as unchanged) and that a turn-end prune on Anthropic re-bills the whole conversation. The per-request layer was removed and turn-end pruning now goes through a cache gate; both are measured in Tool-output pruning and the prompt cache: a replay of long agent sessions.

A package-catalog survey had two workers rank “pruning mutates old messages every turn and breaks the prefix cache” as their top finding and propose porting another package’s commit-epoch design. The hysteresis described above is already that design.

Pruning reclaims old results. Spilling bounds a fresh oversized result the moment it lands. The spill extension watches tool_result and, for an all-text result over 51,200 bytes, writes the full text to disk and replaces the inline content with a bounded preview plus a locator.

It targets the extension tools without provider-side caps - webfetch (5 MB), codesearch and context7_query_docs (50k tokens), the subagent and dossier tools. Built-in read and bash self-cap at 50 KB / 2,000 lines, so they rarely trigger it. read is skipped deliberately: spilling a read result would force a read - spill - read-again loop. Persistence is best-effort - a failed write leaves the original inline, and a spill failure never turns a success into an error.

Pruning handles old results and spilling handles new ones. Pruning is batch-driven and cache-aware; spilling is per-result and instant, and its replacement is small.

tool result landstext over51,200 bytes?spill: full text to disk,preview + locator inlineyeskeep inlinenoturn_endbytes beyond the160,000-byte window>= 80,000?prune oldest resultsdown to the low watermarkyesprefix unchangedno
  1. A tool result lands. If it is all text and over 51,200 bytes, the full text goes to disk and a preview plus locator stays inline.
  2. At turn_end, if the bytes beyond the 160,000-byte protect window total at least 80,000, the oldest results are pruned down to the low watermark.
  3. Otherwise nothing earlier in the conversation changes, and the prefix stays identical.

When the window is full, the harness compacts: it summarises the older turns and replaces them with the summary. There are two ways to run that summary.

pi’s default triggers when context tokens cross contextWindow - reserveTokens (default 16,384), keeps the most recent keepRecentTokens (default 20,000) and summarises the rest.2 It serialises the history to text - [User]: ..., [Assistant]: ..., [Tool result]: ... - with tool results truncated to 2,000 characters, and sends that serialised transcript under a new system prompt. Summarisation requests disable prompt-cache writes, because a one-off summary prompt is unlikely to be reused.2

Prefix replay is the alternative. On session_before_compact, it re-sends the last real provider request byte-for-byte with one summarise instruction appended.3 The provider’s prefix cache serves the history, and the summariser sees the full untruncated context instead of the 2,000-character tool results. Any failure falls back to pi’s default.

Aspectpi defaultPrefix replay
What it sendsre-serialised transcript as text, new system promptthe last real request byte-for-byte + one instruction
Tool results in the summarytruncated to 2,000 charsfull, untruncated
Cache behaviourcold; cache writes disabledprefix cache serves the history
Failure moderefuses when the branch fits in keepRecentTokens; summarisation request can failcontext-length 400, low cache hit on routed providers
Providersalllocal engine only by default

The trigger is shared: the auto-compact extension fires at 0.85 of the context window (crossing detection, not a level, so a successful compaction does not immediately re-trigger). Small windows are skipped - pi’s compaction refuses to run when the whole branch fits inside keepRecentTokens, so firing at 0.85 of an 8,192-token window produces a failed compaction rather than a summary. A companion extension resumes a headless run that dead-stops after a clean-turn compaction, because in pi -p mode there is no user to type /continue.

Two trial rounds pinned openrouter/deepseek/deepseek-v4-pro, with the tool-output prune off, driving pi’s RPC (remote procedure call) compact command. Arm A is pi’s default compaction; arm B is prefix replay (first the upstream package, then the harness’s own port). Wall times include OpenRouter routing variance. The raw rows are results.jsonl and results-ours.jsonl.

The upstream package as arm B:

SessiontokensBeforeA wallA input tokA costB
synthetic (6 big file reads)141,73719.1s4,157$0.00926.4s (19s summary + 7s re-warm), 141,696 cached
real 1147,71317.9s49,454$0.028failed 3/3
real 2181,82625.2s66,626$0.036failed 3/3
real 3221,45423.3s100,127$0.052failed 3/3

One out-of-driver debug run of real 1 under arm B succeeded: 12 s summary plus 3 s re-warm, with 147,456 prompt tokens served from cache. The scripted failures (9/9 in the final run, 3/3 in the previous one) were OpenRouter 400s - the endpoint’s 16,384-token context limit rejected a request whose size matched pi’s default summarisation prompt, with a max-output-1 request that is the package’s warm-up shape.

The harness’s own port, on the same three sessions, succeeded 3/3 where the package failed:

SessionA wall / costA cachedB wall / costB cached
real 1 (147k)24.1s / $0.01249,40850.1s / $0.07210,368
real 2 (181k)28.8s / $0.01466,56032.7s / $0.0868,192
real 3 (222k)25.8s / $0.053058.7s / $0.1092,176

The port worked, but OpenRouter served only 2,000-10,000 of the replayed 137k-223k-token prefix from cache - requests are not pinned to the upstream endpoint that holds the session’s cache, so a replay lands on a different backend with a cold cache. Arm A’s cache hits are flattered: they come from identical summarisation requests sent by earlier trial runs, not from the session’s own prefix; in normal use arm A’s request is cold. Computed from the table, on OpenRouter prefix replay cost 2.1-6.1x pi’s default and took 1.1-2.3x as long.

The package’s published results are from a local engine: 132 compactions on Qwen3.8-27B under vLLM went from a median 229 s to 86 s, with the first turn after compaction 69 s to 1.8 s with a warm-up, and a DeepSeek API (application programming interface) run serving 56,192 of 56,216 prompt tokens from cache. Those are the package’s own numbers, fetched from its README, not this harness’s rig.

One incidental cost: a seed run resumed a copied session on its original model before the model was pinned, a $1.08 cache write.

The trial’s llmc sessions (qwen38-ninfer-uncensored on the local ninfer engine) gave the real result:

SessionA (pi default)B (prefix replay)
real 1 (146k)18.7 s, 49,970 in, 0 cached58.3 s, 146,610 in, 0 cached
real 2failed both arms: “Nothing to compact (session too small)” after the seed turn
real 3 (89k)17.8 s, 12,559 in, 0 cached30.8 s, 88,973 in, 0 cached

Prefix replay was slower on the local engine, not faster. Direct probes explain it. The engine keeps one checkpoint: the KV of its last sequence, which is the last prompt plus the reply it generated. A new request reuses that checkpoint only when it extends the whole sequence:

Request after a 22,890-token turn with reasoningTimeCached
next turn, previous reply re-sent without its reasoning2.4 s0
next turn, previous reply re-sent with reasoning_content0.1 s23,054 of 23,069

An identical prompt repeated also hits (6.8 s -> 0.1 s at 45,285 tokens), because the checkpoint then covers it plus one generated token. A replay is the previous prompt plus a summarise instruction; it skips the reply, so it shares the checkpoint’s prefix but does not extend it, and gets nothing.

Normal multi-turn use does extend the sequence: pi re-sends each reply with its reasoning under the field it was streamed in. In one long-running session, measured over the same engine: turn 1 (a file read and a reply) 17.2 s cold; turn 2 1.1 s with 89,397 tokens cached; turn 3 0.4 s with 89,751 cached. Misses come from anything that changes or drops earlier bytes, or that replaces the checkpoint. Two of those causes are now fixed:

CauseBeforeFixAfter
another client uses the engine in between (a background worker, a pre-push review, an image description)next main turn 89,772 in, 0 cached, 14.9 sone extra device state slot per preset (device_state_slots = 1, about 110 MiB of VRAM (video random-access memory))89.7-89.8k cached, 0.4-0.5 s, after a small, a 2.8k and two back-to-back side requests
a new process resumes the session (per-request reminder messages were re-inserted from in-memory state)resumed turns 0 cachedthe reminder positions are saved as session entries and restored on resume89,402 and 89,919 cached

Two extra checkpoints are not possible on this box: a host-RAM (random-access memory) tier fails with a pinned-memory out-of-memory error under WSL2 (Windows Subsystem for Linux 2) even at 4 GiB (WSL2 pins about 1 GiB of host memory in total). Pruning and compaction still miss by design.

The engine pin moved to a newer NInfer build. KV and state now share one device pool, and the engine writes a prefix checkpoint every 8,192 tokens during prefill, so a request reuses everything up to the last checkpoint before it diverges instead of needing to extend the whole previous sequence. The device_state_slots setting no longer exists. Measured on a six-turn replay over source files (about 45k tokens by turn 6), greedy, same artifact on both engines:

Turn 4, after a 21k-token unrelated side requestPrevious engineNew engine
client re-sends each reply with its reasoning27,039 of 34,191 cached, 0.94 s27,197 of 34,349 cached, 0.92 s
client drops the reasoning0 of 34,005 cached, 3.8 s26,546 of 33,968 cached, 1.06 s

The engine also reports the reused count in usage.prompt_tokens_details.cached_tokens now (the previous engine always reported 0), and keeps the checkpoints of a cancelled prefill: a rerun of an aborted 134,781-token prompt reused 106,496 tokens.

The compaction trial was re-run on this engine with the same driver, the same three source sessions and the same preset, lock held:

Sessionpi defaultPrefix replay
real 1 (152k)20.4 s, 49,970 in, 0 cached14.8 s, 152,213 in, 151,910 cached
real 2 (92k)24.2 s, 10,153 in, 62 cached15.6 s, 92,766 in, 85,328 cached
real 3 (95k)24.8 s, 12,958 in, 62 cached19.4 s, 95,695 in, 95,392 cached

Replay now reuses the checkpoint of the turn just before it and wins all three. The win needs that checkpoint to exist: a model swap or proxy restart empties the engine, and the previous engine’s rows above are what a cold replay costs (30.8-58.3 s against 17.8-18.7 s). The average warm win is 6.5 s and the average cold loss 26.3 s, so replay comes out ahead only if fewer than about one compaction in five runs cold. On a box that swaps models often that is not a safe bet, so prefix compaction stays off and is an opt-in per provider. Summary quality was not compared: the default summarises a truncated transcript, replay the full history.

Decision: prefix-compaction.ts is off by default for every provider. It can be opted in on an engine with block-level prefix caching, one that reuses any shared prefix, as vLLM and llama.cpp can.

compaction firesengine reuses anyshared prefix?prefix replaycache serves the historyyes (opt-invia provider list)pi defaultcold serialised summaryno (default)llmc, openrouter,deepseek
  1. The engine reuses any shared prefix (block-level prefix caching): enable prefix replay via PI_PREFIX_COMPACTION_PROVIDERS. No provider in the harness’s current set meets this.
  2. Otherwise: pi’s default cold serialised summary. This covers OpenRouter, deepseek, and the local engine (llmc), all of which the trial measured.

Pruning stays on regardless: it reclaims context before compaction is needed, and with hysteresis it does not itself break the cache.

ClaimSourceMeasured vs design-only
System prompt 193.6 KB, 139 tools 114.4 KB in a fresh session/context inspector, first measurement commitmeasured
Semantic one-shot changed the prompt at offset 40,224, two misses per session/context, before/after the fixmeasured
After the fix, 3 requests with 0 system-prompt changesTUI session, fix commit messagemeasured
Prune hysteresis design, ~35 s re-prefill at 150k, monotone pruned setextension header, dated 2026-10-10design-only
Spill threshold 51,200 bytes, read skipped, best-effortextension headerdesign-only
Compaction defaults: reserve 16,384, keep 20,000, tool results 2,000 chars, cache writes disabledinstalled pi docs docs/compaction.mddocumented
Prompt sections vs systemPrompt replacement ruleinstalled pi docs docs/extensions.mddocumented
Prefix-cache trial tables (walls, costs, cache-hit counts)run.py, results.jsonl / results-ours.jsonlmeasured
Package vLLM 229s -> 86s, DeepSeek cache 56,192/56,216package README, fetchedreported by package, not this rig
llmc compaction trial: default (18.7 s) vs replay (58.3 s) at 146k, engine reuses one checkpoint that a request must extend (trial README, direct probes)README.md, llmc sectionmeasured
Side-request and resume fixes: 0 -> 89.7-89.9k cached on the next turnprobe scripts, fix commit messagesmeasured
New engine (2026-11-01) prefix reuse across divergence and side requests, cached_tokens reportedllmc bench/ab/ replay, results bench/results/ab-30/measured
Compaction trial on the 2026-11-01 engine: replay 14.8-19.4 s vs default 20.4-24.8 s warm, break-even one cold in fivetrial README, results-llmc-1882.jsonlmeasured
Memory block: 13 of 747 interactive requests changed the system promptper-request context log, 2026-10-20/21measured
  1. pi repository, https://github.com/earendil-works/pi ↩ ↩2

  2. pi compaction source, https://github.com/earendil-works/pi/blob/main/packages/coding-agent/src/core/compaction/compaction.ts ↩ ↩2

  3. pi-prefix-cache-compaction, https://pi.dev/packages/pi-prefix-cache-compaction ↩