Skip to main content

Module session_elide

Module session_elide 

Source
Expand description

Long-session elision for Anthropic Messages requests.

In a long agent session most of every request is old tool output the model has already acted on. Once the client’s messages serialize to more than THRESHOLD_BYTES, every tool_result older than the live window whose text is longer than MIN_ELIDE_BYTES leaves as a one-line stub. The original goes to the content-addressed tee store ({state}/tee/, files 0600 in a private state directory) and the stub names that file, so the model gets the full text back with cat or its file read — Claude Code in governor mode has no MCP tools, so ctx_expand alone would not do.

§Why this rewrites inside the cached prefix

Claude Code puts a cache marker on the last message, so the history pruner (which never touches the client-cached prefix, #448) never fires there. Elision deliberately does. It is bounded:

  • the window is the cache-aware staircase of history_prune::prune_boundary: it advances only in whole strides of 16 messages and keeps at least 8, so the cached prefix breaks once per stride, not every turn;
  • the stub is a pure function of the block’s text (#498), so a block elided once leaves with the same bytes on every later turn;
  • the size test reads the client’s own messages, which only grow, so a session that crossed the threshold stays across it;
  • it runs on every request of such a session, the person’s prompt turns included: a prompt turn then sends the same elided prefix as the tool turn before it and reads the cache, instead of sending the history whole and writing it again.

Measured on Claude Code with Haiku 5.5, long sessions: cost x0.74 (95 % CI 0.53–0.94) against native, 12/12 tasks solved on both arms. The one cache write per stride is cheaper than reading the full history on every turn of that stride.

The determinism guard and the prefix replay know this exception: a forwarded prefix that first differs at a newly elided message is the sanctioned stride break (newly_elided, message_is_elided).

Under protection (a policy pack, governed or sovereign mode) the tee store holds nothing originless: a block leaves shortened only with a recovery copy bound to its lineage (compress::shorten_with_copy), otherwise it stays whole. Content never reaches a log.

Constants§

MIN_ELIDE_BYTES
Tool output at or below this size stays as it is: the stub would save little and the model often still quotes short results.
STUB_OPEN
How every elision stub opens; also what message_is_elided recognises.
THRESHOLD_BYTES
Serialized size of the client’s messages above which old tool output is elided (measured arm: 180 000 characters).

Functions§

elide
Elide old tool output in messages when they exceed threshold bytes. Returns the index of the last message that carries a stub, or None when nothing was elided.
message_is_elided
Whether message carries a tool result that left as an elision stub.
newly_elided
Whether the elision that reached message last is new against a forwarded request (prefix_bytes): that request covered message last and sent it without a stub. Elision only advances in whole strides, so the last elided message decides; a prefix replay must not restore the older bytes then.