Compact API
Compress chat history and code context at 33,000 tok/s with byte-identical output
Overview
Compact compresses chat history and code context at 33,000 tok/s by removing irrelevant lines. Every surviving line is byte-for-byte identical to the original input. 100K tokens compresses in under 2 seconds. Passquery to tell the model what matters for the next LLM call. Without it, the model auto-detects from the last user message.
Usage Examples
keepContext Tags
Wrap sections you never want compressed in<keepContext> / </keepContext> tags. Tagged content survives compression verbatim regardless of the compression ratio.
kept_line_ranges showing which lines were force-preserved.
Compatible Endpoints
Compact also works through OpenAI-compatible endpoints withmodel: "morph-compactor":
Authorizations
Morph API key, passed as Authorization: Bearer sk-.... Create keys at https://www.morphllm.com/dashboard/api-keys.
Body
Compact request with text or messages to compress
Text or conversation to compress, plus the knobs controlling how aggressively lines are pruned.
Text to compact. One of input or messages is required.
"def hello():\n return 1\n\ndef unused():\n pass\n\ndef world():\n return 2"
Conversation messages to compact. Takes priority over input.
Focus query for relevance-based pruning. Lines relevant to this query are kept.
"hello function"
Fraction of input to keep. 0.3 = aggressive, 0.7 = light.
0.5
Keep last N messages uncompressed.
0
When true, system messages are also compressed. By default they are preserved verbatim.
false
Include compacted_line_ranges in response.
true
Include (filtered N lines) text markers. When false, gaps become empty lines.
true
Model ID.
"morph-compactor"
Response
Compact response with compressed output and metadata
Compacted output, per-message line ranges, and usage statistics.
Unique identifier for the compact request
"cmpr-7373faf8af65"
Object type, always compact
"compact"
Model used
"morph-compactor"
All compacted messages joined into a single string
"def hello():\n return 1\n(filtered 3 lines)\ndef world():\n return 2"
Per-message compaction results
Usage statistics — token counts and timing for a single compaction.