Compression, images and switches
Needs v0.18.0 or later.
Your agent pays for every byte a tool returns. A 20,000-line build log or a 10 MB JSON reply sits in context for the rest of the session. Compression keeps a short preview in context and the whole thing on disk.
No model is involved. The same input gives the same preview.
Turn it on
Section titled “Turn it on”It is off until you ask. One command:
ctx-optimize config input-compress onThat switches on tool-result and image compression and installs the two hooks below for every host it finds, Claude Code and Devin CLI. It prints what it did per host (installed, already installed, host not found). Run it again any time: nothing is duplicated. --project turns it on for this repo only. Copilot CLI and opencode are tracked in issue #48. The hooks merge into the host’s config and leave your own hooks alone; uninstall removes them.
By hand instead (the manual path): ctx-optimize install --claude --compress or --devin --compress installs only the hooks, leaves the switch off, and prints the line to run.
| hook | does |
|---|---|
PostToolUse, matcher * |
replaces a big tool result with a preview |
PreToolUse, Read |
shrinks an image before the agent reads it |
Devin runs the hooks in the Claude settings file as well as its own. The entries in Devin’s config carry --host devin. With both installs done, Devin still runs each hook once: the Claude entry steps aside when it sees Devin is the host and Devin’s own entry exists.
Input: what the agent sees
Section titled “Input: what the agent sees”The trigger is size, not the tool. A result over 25 lines or 8 KB is saved byte for byte and replaced. Shorter results pass through whole.
1 | 12 | 23 | 3...1499 | 14991500 | 15001501 | 1501...2998 | 29982999 | 29993000 | 3000exit status: 0
Original: ~/ctxoptimize/artifacts/2e57c67a8bbe706a.txtRead specific lines: ctx-optimize read-lines ~/ctxoptimize/artifacts/2e57c67a8bbe706a.txt --from 4 --to 1498The preview holds up to 100 lines, each with its original line number: three from the start, middle and end; every error with 3 lines before and 4 after; warnings; verdict lines; then the rarest remaining lines. One JSON document on at most three lines gets a key outline instead, plus a pretty-printed copy so the middle is reachable.
A result is replaced only when the preview is smaller than it. Your own ctx-optimize commands, images and failed saves pass through untouched.
Get the full text back
Section titled “Get the full text back”ctx-optimize read-lines ~/ctxoptimize/artifacts/2e57c67a8bbe706a.txt --from 1000 --to 10021000 | 10001001 | 10011002 | 1002Exact original lines, never compressed again. The preview names the command; the agent runs it.
Saved outputs live in <store>/artifacts, owner-only, never in your repo. They are removed after input-compress keep-days (7) and capped by input-compress max-store-mb (1024, oldest first). ctx-optimize status shows the count, bytes and folder:
saved outputs: 1 files, 0.0 MB in ~/ctxoptimize/artifactsTune with ctx-optimize config input-compress max-lines 60 (add --project for one repo; keep-days and max-store-mb are machine-wide). Also lines, bytes, keep-days, max-store-mb. ctx-optimize config input-compress lists every setting with its default and range.
What it covers
Section titled “What it covers”| Claude Code | Devin CLI | |
|---|---|---|
| Bash / exec | large successful output | large output that finished with an exit code |
| Read, Grep, Glob, WebFetch | replaced in the tool’s own shape | every tool, by size |
| MCP tools | yes | yes |
| Unknown shapes (WebSearch, Task) | pass through | edit/write are small and pass through |
Claude Code does not offer the model Grep, Glob or WebFetch by default (search goes through Bash), so those rows matter only when you enable those tools. WebFetch already returns a small summary and passes through.
Measured on Devin, seq 1 20000: 43,154 bytes became 576, and read-lines reached line 10,000. Measured on Claude with the real hook: Grep 201,785 to 1,279 bytes, Read of 1,500 lines 67,305 to 1,036. Estimated tokens are bytes divided by 4, not a tokenizer count.
Numbers
Section titled “Numbers”ctx-optimize usage --compress # all sessionsctx-optimize usage --compress --session ID --jsonBytes shown, bytes saved, estimated tokens, recoveries, why results passed through, top commands. Numbers only; no content is stored. The dashboard shows the same in a panel.
Images
Section titled “Images”When the agent reads a PNG or JPEG whose long edge is over input-compress image-max-edge (1024), the hook points the read at a smaller copy in <store>/artifacts/images/. Your file is never touched. Nothing is auto-approved.
A 1920x1080 image costs about 2,691 tokens; at 1024x576 about 777. On Devin the saving is smaller: it already caps at 1344 px.
By hand: ctx-optimize image shrink shot.png [--edge 800] prints the path to read.
Limits:
- Claude
@file.pngand pasted images reach the model before any hook runs. Ask the agent to read the file instead. - GIF and WebP, missing or corrupt files, files over 64 MB or 40 megapixels pass through.
- Copies count against
input-compress max-store-mb.
Switches
Section titled “Switches”ctx-optimize config input-compress on # text and image; installs the hooksctx-optimize config input-compress off # both off; the hooks stay and pass throughctx-optimize config input-compress text on # tool results onlyctx-optimize config input-compress image on # images onlyctx-optimize config input-compress on --project # this repo onlyctx-optimize config input-compress on --global # the machine (same as no flag)ctx-optimize config input-compress # both switches, every setting, sources, usagectx-optimize config # every key, effective value and sourceEverything is off until you turn it on, so a release changes nothing for you by itself. A project value wins over the global one. A read with no flag shows the effective value and where it came from (default, global or project); with --project or --global it shows only what that file holds. Both flags together is an error. Plain config never writes anything.
off uninstalls nothing. The hooks stay and pass everything through, recording the reason disabled; ctx-optimize uninstall removes the files. compress, image shrink, read-lines and usage work when you type them. status prints a switches: line while compression is off, with the command to turn it on. Claude hooks are written to $CLAUDE_CONFIG_DIR/settings.json when that variable is set, otherwise ~/.claude/settings.json. If your hooks setting is NONE, on writes no hook files and prints the install command to run yourself.
See the numbers: ctx-optimize usage --compress and ctx-optimize status.
Real-host check, 2026-11-29: Claude Code 2.1.332 (haiku) and Devin 3000.15.14 (swe-2-flash-medium): the dark default, one command on, text only, image only, global against project, a repo’s own thresholds, a flip mid-session, a custom CLAUDE_CONFIG_DIR, and a fresh host where config input-compress on installs the hooks and off passes through. 13 of 13 cells passed on each host.
Limits
Section titled “Limits”- Claude
Readover about 25k tokens fails before the hook runs. - Devin’s overflow copy can be partial (8.36 of 10.0 MB in one test); the preview says “Saved by Devin, may be partial.”
- Devin’s re-send on resume or compaction was not verified.
- A failing Claude Bash command is delivered by the host in full; Claude does not let a hook replace it.
- Other hosts are not verified. Copilot CLI and opencode: issue #48. See limits.