# Caveman 5: a small model decides what your agent reads *trim-2 keeps the lines a coding agent's ask needs, drops the rest, and keeps every original on your disk.* Julius Brussee · 29 November 2026 · Release --- One of the test fixtures in Caveman 5 is a log. It's 404 lines of `kubectl logs` output: four hundred lines saying a checkout request was served in a few milliseconds, one line saying `ERROR payment client: ledger-db timeout after 30s (order 1271)`, the stack frame under it, and `exit status 1`. Give that to a coding agent along with the question *"Why did the checkout payment fail? Find the ledger error."* and, left alone, the agent sends all 10,061 tokens to the model. Then, because agents resend their whole history on every call, it sends them again on the next turn, and on the turn after that. Here is what trim-2, the model new in Caveman 5, keeps of it: ```text $ kubectl logs deploy/checkout --since=1h … 269 lines … 2026-10-11T03:04:29Z INFO checkout request 1269 served in 24ms 2026-10-11T03:04:30Z INFO checkout request 1270 served in 25ms 2026-10-11T03:04:31Z INFO checkout request 1271 served in 26ms 2026-10-11T03:04:31Z ERROR payment client: ledger-db timeout after 30s (order 1271) at LedgerClient.charge (ledger/client.go:88) 2026-10-11T03:04:32Z INFO checkout request 1272 served in 10ms 2026-10-11T03:04:33Z INFO checkout request 1273 served in 11ms 2026-10-11T03:04:34Z INFO checkout request 1274 served in 12ms … 126 lines … ``` Nine lines and 216 tokens. The error and its stack frame are two of them; the rest let the model read the error as an event in a timeline, with the command that produced the log and the requests on either side of the failure. Every surviving line is byte for byte what the log said, each marker states exactly how many lines it stands for, and the other 395 lines sit in a recovery store on your machine, one tool call away. And on this log, trim-2 loses. Caveman's log compressor, which shipped long before 5.0, gets the same log down to 169 tokens. It keeps the error and the stack frame too. It wins because 400 of the 404 lines are one template with the numbers changed, and folding repetition doesn't require knowing the question. In 5.0 the proxy runs both and sends whichever result is smaller, so an agent reading this log gets the older compressor's 169 tokens. (Every token count in this post is Caveman's local `o200k_base` count, labelled `inferred`. Your provider's bill counts its own way.) We lead with a losing example because it explains what trim-2 is for. It isn't a replacement for the compressors Caveman already had. It covers the blocks they can't touch. ## Format doesn't know the question Caveman's Engine detects whether a block is JSON, CSV, a log, a diff, or an HTML page, and runs a compressor written for that format. Those compressors are good at one thing, repetition: a long JSON array of same-shaped records comes back as a few examples plus an exact account of what was elided, and a log of one repeated template comes back as the log above did. That's how the [wrap benchmark](../WRAP-BENCHMARK.md) got its 33.2% cut in whole-session input tokens. A format compressor can't know what you asked, though. It sees a log, not that your question was about the ledger. One of our own rules also holds it back on purpose: every compressor that drops units keeps at least one example of each distinct kind of content, because an agent that can't tell what went missing from a list it needed burns more on recovery calls than the cut saved. That rule is right. It also means that when little repeats, as in a build log where every line differs or grep hits spread across forty files, the format compressor keeps nearly all of it or passes the block through untouched. Before 5.0, the agent then read every line. Picking the lines that answer a question needs the question. ## What trim-1 taught us trim-2's predecessor, [trim-1](https://github.com/caveman-ai/trim-1), scored each line on 67 features with a single forest of 712 trees. It worked well on long documents with a clear question. On coding-agent tool output, its own README told people to expect small savings and to stay at the conservative settings. Agent output is short and dense, and much of what matters in it is invisible to word overlap: a closing brace can hold an Edit anchor together, a stack frame often shares no words with the question, a diff line exists only to show what changed, and a log line repeated three hundred times matters only the one time it says something else. trim-2 was built for that kind of text. ## How trim-2 reads a line trim-2 keeps trim-1's 67 features and adds 67 more, all aimed at what agents read. Their names undersell them, so here are four: - A second BM25 channel over identifier parts, so `LedgerClient.charge` matches a question about the ledger client even though no whole word lines up. - A sole-witness signal. A line that is the only place a query term appears gets credit for being the one copy. - Line kinds that only show up in agent output: diff additions and deletions, the line-number gutters read tools add, ANSI control codes, lockfile and `node_modules/` noise. - Distances from each line to the nearest error, the nearest diff change, and the best-matching line. That's stage one: 701 trees scoring each line from its own row of 134 numbers. A line's value also depends on how the lines around it scored, which stage one can't see. So trim-2 computes 40 more features from stage one's scores across the whole context (where a line ranks, how strong its block and section are, whether it stands out inside a run of near-identical log lines, how much probability ranks above it) and runs a second forest of 641 trees over all 174 columns. One of those features reaches past the question's words: it takes rare terms from stage one's top few lines and credits other lines that share them. Suppose the error line names a function your question never mentioned. A line elsewhere that calls the same function now gets pulled up too. The trees evaluate exactly. Split thresholds are stored so that 32-bit comparisons decide the same way the trained 64-bit ones do, and the final sigmoid uses a vendored copy of Go's portable `exp`, so the answer doesn't depend on which CPU computes it. A parity test checks both stages against the training library's own scores on 256 rows, bit for bit. Determinism matters here for a reason that has nothing to do with tidiness, and we'll come back to it. ## A budget, not a threshold trim-1 kept every line scoring above a share of the best line. trim-2 solves for the selection instead. Each kept line earns its keep probability and pays for its tokens. Each gap costs a marker, priced at 5.5 tokens, and neighbors kept together earn a small bonus. A two-state Viterbi pass finds the exact best selection in one sweep over the lines. The marker price does quiet work. One test hands the optimizer a six-line Go function with its lone closing brace scored at 0.01. A threshold would drop the brace and print `… 1 line …` in its place, spending more tokens on the marker than the brace ever cost. The optimizer keeps the brace. ## Code is not a log Agents read logs to understand them and read code to change it. An Edit or `apply_patch` call has to quote the lines it replaces exactly, so a cut through the middle of a function can break the agent's next action even when the model understood the code fine. On source code trim-2 adds rails: enclosing function and `if` headers stay, one line of context stays on each side of anything it keeps, and short holes get filled so anchors stay contiguous. Small source files aren't cut at all. The 5.0 default, `smart_max`, follows from that split. Source code and diffs get the careful `balanced` point; logs, search results, prose, and dumps get `max`, the hardest cut. Shape detection reads only the bytes in front of it, so the same block always gets the same treatment. In logs and diffs, any line that looks like `error:`, `panic:`, `Traceback`, or `--- FAIL:` is pinned whatever the model thinks of it. ## When it says no A lot of trim-2's value is in declining. If your latest message is "continue", "ok", or "go ahead", or asks for the whole file, every match, or a line-by-line review, trim-2 hands the block back untouched. It does the same when the question is too vague to score against, when the block is small, and when the cut would save less than 5%. A line selector working from "continue" is guessing, and a guess that drops the line the agent needed costs a recovery call plus another turn of history. The proxy adds one more guard. To score well, it feeds trim-2 the agent's latest tool call (its name, the file path, the grep pattern, the command) along with your words. The refusal check runs on your words alone, and it runs first. Otherwise a file path could make "continue" look like a specific question. ## Nothing is gone Every line trim-2 drops goes into Caveman Context Recovery first, a SQLite store on your machine. If storing fails, nothing gets cut. The agent sees a handle and can call `caveman_retrieve` for the exact original, or for just the part that matches a query. Prompt caching sets the other rule, and it's why determinism matters. Providers charge far less for the part of a request they've already seen, so changing bytes inside a cached prefix can cost more than any cut saves. trim-2 therefore only touches new blocks, its output is a pure function of the block and the question, and the proxy memoises its first decision for each block and re-sends that exact decision on every later turn. Changing `trim_level` changes the cache key too, so one level's cuts never replay under another. ## The rest of 5.0 trim-2 leads the release, but it's one layer of several. In `compress` mode, 5.0 turns on `savings_profile: max` by default, which bundles levers that used to be opt-in: - **Aged epoch compaction.** Old tool results get replaced with short verbatim excerpts and a recovery handle, but only when the arithmetic works. Rewriting history costs a cache write, so the proxy prices it per provider (Anthropic writes cache at 1.25x and reads at 0.10x; Gemini writes at 1.0x and reads at 0.25x) and compacts for free right after a cache has expired anyway. - **Cross-turn delta.** A run of lines the agent already has, verbatim, in an earlier tool result becomes a one-line reference. - **A stable prompt head.** The tool catalog gets shrunk, sorted into one order, and MCP tools are deferred. All of it is deterministic, so the start of the prompt never moves. - **Provider levers.** On Anthropic API keys, `clear_thinking` and a split cache lifetime (an hour for tools and system prompt, five minutes for the growing conversation). Everywhere it's supported, lower effort on mechanical "continue" turns, and low thinking on Gemini's clean tool-loop turns. Structural compression now covers Bedrock (Converse and Anthropic-wire Invoke) as well as Anthropic, OpenAI, Azure, Gemini, and Vertex. trim-2 doesn't run on Bedrock or Vertex yet, because the proxy doesn't read your question out of those requests. The Responses API over WebSocket gets content compression, trim-2 included, but not the catalog shrink or the provider levers. On the CLI side, support for all ten agents moved behind one typed adapter interface with a shared contract test. The [release notes](../releases/v5.0.0.md) have the full list, and [Compress mode in 5.0](../technical/proxy-and-providers.md#compress-mode-in-50) documents every lever and its off switch. ## What we haven't measured This section is longer than I'd like. There is no committed quality benchmark for trim-2 yet. The tests pin what it does: the golden log, Edit-span contiguity, the `smart_max` dispatch, the refusals, bit-exact parity with training. None of them measure whether an agent answers as well with trim-2 as without it. The log at the top of this post is synthetic, with one planted error, and on it trim-2 loses to an older compressor. It shows the mechanism and says nothing about your sessions. The 33.2% session number in the README comes from August, on an engine without trim-2, and we haven't re-run it on 5.0. Until we do, it stays the only session-level number we quote. When we re-run it, the losing rows stay in. One 5.0 path ships switched off: native agent hooks have an inline trim-2 path, but nothing turns it on yet. The model's training pipeline and data aren't in the repository either. What ships is the model file, the feature code, and the parity fixture. Speed we did measure. On one core of an Apple M5 Max, trim-2 takes about 4.7 ms on a mixed 8 KB context and about 96 ms on 200 KB. The command to reproduce that is in the [trim-2 docs](../technical/trim-2.md#speed), and the log figures at the top of this post, the Engine's 169 included, are pinned by a test that fails if either side moves them. ## Try it ```bash npx @caveman-ai/cli@latest ``` One command on Mac, Linux, or Windows, with Node.js 22.13 or newer. Agents you launch through Caveman run in `compress` mode, so trim-2 is on at `smart_max`. To choose another point, or turn it off: ```yaml # ~/.caveman/caveman.yaml trim_level: balanced # off, conservative, balanced, smart_max, aggressive, max ``` `CAVEMAN_TRIM_LEVEL=off` does the same from the environment. And if you only want the model, it's a Go package with no dependencies outside the standard library: ```go import "github.com/JuliusBrussee/caveman/engine/trim2" res := trim2.Compress(toolOutput, question, trim2.Options{Point: "smart_max"}) ``` The full write-up of the model, its features, operating points, refusals, and limits: [trim-2: the line-selection model](../technical/trim-2.md). Agent read less. Answer still there.