Correction, 24 September 2026: an earlier version of this post said every transform is lossless with zero semantic degradation, and that Context Optimize runs in safe mode by default. Neither was true. Safe mode is deterministic, but conversations past 40 user turns are trimmed, whitespace normalization flattened indentation outside code fences until 24 September 2026 (it now keeps it), and the layer stays off until you turn it on. The benchmark below is unchanged.
The problem
Agentic coding sessions are expensive because of bloated context. Every turn in a conversation accumulates:
- Repeated tool schemas (the same function definitions sent every time)
- Pretty-printed JSON (indented with whitespace that costs tokens)
- Duplicate system prompts across turns
- Old conversation history that's no longer relevant
With Claude Opus at $5/1M input tokens, this adds up fast.
What Context Optimize does
Safe mode is deterministic: no model calls, and nothing is paraphrased. It is not strictly lossless, though: history trimming drops the middle of long conversations.
| Transform | What it does |
|---|---|
| JSON minification | Removes whitespace, newlines from JSON values |
| Tool schema dedup | Replaces repeated tool definitions with references |
| System prompt dedup | Removes duplicated instructions across turns |
| Whitespace normalization | Collapses runs of spaces and extra blank lines outside code fences, keeping indentation |
| Chat history trimming | Past 40 user turns, keeps system + first + last 39 turns |
Benchmark results (Claude Opus)
| Scenario | Before (tokens) | After | Saved | $/1K requests |
|---|---|---|---|---|
| Agentic coding (8 turns, 5 tools) | 3,657 | 1,573 | 57.0% | $10.42 |
| RAG pipeline (6 chunks) | 544 | 386 | 29.0% | $0.79 |
| API response analysis (nested JSON) | 1,634 | 616 | 62.3% | $5.09 |
| Long debug session (50 turns) | 3,856 | 1,414 | 63.3% | $12.21 |
| OpenAPI spec context (5 endpoints) | 2,649 | 762 | 71.2% | $9.44 |
| Total | 12,340 | 4,751 | 61.5% | $37.95 |
The biggest wins come from agentic sessions with repeated tool schemas and long debug sessions with JSON logs.
How to enable it
Context Optimize is off by default. Turn on safe mode in your API key config or globally:
layers:
optimize: safe # deterministic transforms, no model calls
The aggressive mode (semantic dedup, embedding-based redundancy removal) is available on the Pro plan.