Context Optimize

Context Optimize trims tokens before a request reaches your provider. Compare its off, safe and aggressive modes, and see what safe mode changes first.

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

ModeDescription
offDefault. No optimization applied.
safeDeterministic transforms, no model calls. Not strictly lossless: long conversations are trimmed.
aggressiveSafe 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

Via CLI flag
nadirclaw serve --optimize safe
Via .env
NADIRCLAW_OPTIMIZE=safe

Test on a file

Preview optimization results on a saved payload without starting the server:

Test optimize
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

Safe mode is not lossless: past 40 user turns it trims the middle of the conversation, and JSON formatting can change. Use off for exact-copy tasks. Test it on your own traffic before you turn it on.

Pages on this site

Machine-readable summaries of this site: llms.txt and llms-full.txt.