Context Optimize reduces token counts before requests hit your LLM provider. Safe mode is deterministic and never paraphrases, but it is not strictly lossless, so check what it changes below before you turn it on.
Modes
| Mode | Description |
|---|---|
off | Default. No optimization applied. |
safe | Deterministic transforms, no model calls. Not strictly lossless: long conversations are trimmed. |
aggressive | Safe plus semantic deduplication of near-duplicate messages. Larger savings, and it can drop wording the model needed. |
What safe mode does
- System prompt deduplication. Removes system prompt text that is repeated word for word in a later message.
- Tool schema deduplication. Replaces a repeated, identical tool or function schema with a short reference to the first copy.
- JSON minification. Re-serializes JSON in message text without formatting whitespace. JSON inside
```code fences is left alone. - Whitespace normalization. Reduces unnecessary whitespace in prose. Inspect the output with your installed version: spaces and line breaks can carry meaning in code, Markdown and exact-copy tasks.
- Chat history trimming. Once a conversation passes 40 user turns, keeps the system messages, the first turn, and the latest 39 turns, and replaces the middle with a one-line placeholder. Safe mode applies this history limit.
Token savings
Measure your own payload before and after optimization. Token reduction depends on the content, mode and tokenizer. It does not establish lower provider cost or preserved answer quality.
Compare complete tasks using your existing checks. Include cache reads, cache writes, retries and any repair work in the cost and time comparison.
Enable
nadirclaw serve --optimize safe
NADIRCLAW_OPTIMIZE=safe
Test on a file
Preview optimization results on a saved payload without starting the server:
nadirclaw optimize payload.json # Illustrative output; your result will differ: # Original tokens: 4,218 # Optimized tokens: 1,803 # Saved: 2,415 (57.3%) # Mode: safe
Warning
off for exact-copy tasks. Test it on your own traffic before you turn it on.