POST /v1/optimize

POST /v1/optimize compacts a message list and reports the tokens removed, returning the same message schema so it drops straight into your provider call.

POSThttps://api.getnadir.com/v1/optimize

Compact a message list and report the tokens removed. Returns the same message schema you sent, so the output drops straight into your provider call. No LLM call.

Authentication

Required. Send your key as X-API-Key.

Request body

messagesobject[]required

Messages to compact. At least 1.

modestring

safe, aggressive, kompress, or select. Defaults to safe. See below.

max_turnsinteger

Maximum conversation turns kept when trimming. Between 1 and 500, defaults to 40.

modelstring

Target model id, used for accurate token counting.

tierstring

The routed tier this request is headed for, for example from /v1/bucket. Read by select mode only. Every other mode ignores it.

Modes

safe

Deterministic, no model calls. Dedups repeated system prompts and tool schemas, minifies JSON, and normalizes plain prose while preserving code, string spacing and Markdown line breaks. Not lossless: JSON formatting can change and long histories may be trimmed. Use off for exact-copy tasks.

aggressive

Adds semantic deduplication of repeated content.

kompress

Compresses eligible tool results only after an explicit protected-prefix boundary. Without that boundary it preserves input; it does not fall back to aggressive mode or trim history.

select

Adds query-conditioned removal of whole retrieved passages. Nothing is rewritten: a passage survives verbatim or is dropped.

Example

Request
curl https://api.getnadir.com/v1/optimize \
  -H "Content-Type: application/json" \
  -H "X-API-Key: $NADIR_API_KEY" \
  -d '{
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "..."}
    ],
    "mode": "safe",
    "model": "claude-sonnet-4-6"
  }'

Response

messagesobject[]required

Compacted messages, same schema as the input.

original_tokensintegerrequired

Token count before optimization.

optimized_tokensintegerrequired

Token count after optimization.

tokens_savedintegerrequired

original_tokens minus optimized_tokens.

savings_pctnumberrequired

tokens_saved / original_tokens * 100.

modestringrequired

Mode actually applied, which can differ from the mode requested when a mode degrades.

optimizations_appliedstring[]required

Named transforms that fired on this message list.

latency_msintegerrequired

Optimization latency in milliseconds.

Info

Field names and types on this page are taken from the deployed schema at api.getnadir.com/openapi.json. The values in the example response are illustrative, not a recorded call.

Warning

In safe and aggressive modes the output is prefix-stable: re-optimizing a conversation that has grown by a turn returns the previously optimized messages byte for byte, so provider prompt caches keyed on that prefix keep hitting. The exception is history trimming, which moves the prefix. When it fires, optimizations_applied includes chat_history_trim. Check for it before assuming a cache hit.

Pages on this site

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