Context lifecycle in local thinking models — measurement and findings

Measures what fills a context window over agent sessions and what a client’s auto-compaction keeps versus drops. Runs entirely on captures from the tap.py script in 01-token-audit/.

The scripts are client-agnostic. The findings below are from one specific setup: compaction behaviour changes depending on the client and version. A different client, or a later version of the same one, can behave differently. That is what the scripts are for: run them on your own captures and get your own measurements.

Test setup for the original runs

Run order

  1. Leave the tap running in front of the inference server during normal use. Days, not minutes: compactions only happen in long sessions.

  2. Decompose the captures into per-request rows:

    python3 decompose.py output.csv ~/captures/reqs-*.raw
  3. Summarise:

    python3 summarise.py output.csv

What the scripts do

decompose.py extracts every chat-completions request from the raw captures (any JSON object containing a messages key), splits each into character counts per role (system / user / assistant / tool / tool schemas), and classifies every request:

Session IDs are unique across files.

summarise.py prints four sets of data: per-session composition and compaction counts; overall composition shares, also per model; a compaction table showing what each event dropped by role; and post-compaction tool regrowth (how many tool characters reappeared within the next three states, the measured cost of dropping reconstructible content).

Character counts, not tokens: composition is reported as shares, and characters track tokens closely enough for that (~4 chars/token). Exact tokenisation would need a live server and adds nothing here.

Findings from the original runs

These numbers describe the setup above. n = 13 compactions on one machine.

What generalises vs what is client-specific

Generalises (mechanism, not policy): - Prefix caching only survives compaction if the compacted request keeps the prompt front identical. Any client that rewrites the system prompt during compaction resets the cache to zero. - Tool results are the correct thing to drop because they are reconstructible: the agent can re-read a file, but cannot re-derive a lost user instruction or decision. - Selective retention (keep recent tool results, drop stale ones) beats both extremes. This held for pi; whether your client does it is what the compaction and regrowth tables show.

Client-specific (verify on yours): - What the compaction actually drops, whether the system prompt survives, how failure is handled, and when compaction triggers.

Requirements

python3, standard library only. Input: raw capture files from the 01-token-audit/ tap.