Skip to main content
POST
Compact context

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. Pass query 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.
The response includes kept_line_ranges showing which lines were force-preserved.

Compatible Endpoints

Compact also works through OpenAI-compatible endpoints with model: "morph-compactor": See the full Compact documentation for SDK reference, best practices, and advanced usage.

Authorizations

Authorization
string
header
required

Morph API key, passed as Authorization: Bearer sk-.... Create keys at https://www.morphllm.com/dashboard/api-keys.

Body

application/json

Compact request with text or messages to compress

Text or conversation to compress, plus the knobs controlling how aggressively lines are pruned.

input
string
default:def hello(): return 1 def unused(): pass def world(): return 2

Text to compact. One of input or messages is required.

Example:

"def hello():\n return 1\n\ndef unused():\n pass\n\ndef world():\n return 2"

messages
object[]

Conversation messages to compact. Takes priority over input.

Example:
query
string
default:hello function

Focus query for relevance-based pruning. Lines relevant to this query are kept.

Example:

"hello function"

compression_ratio
number
default:0.5

Fraction of input to keep. 0.3 = aggressive, 0.7 = light.

Example:

0.5

preserve_recent
integer
default:2

Keep last N messages uncompressed.

Example:

0

compress_system_messages
boolean
default:false

When true, system messages are also compressed. By default they are preserved verbatim.

Example:

false

include_line_ranges
boolean
default:true

Include compacted_line_ranges in response.

Example:

true

include_markers
boolean
default:true

Include (filtered N lines) text markers. When false, gaps become empty lines.

Example:

true

model
string
default:morph-compactor

Model ID.

Example:

"morph-compactor"

Response

Compact response with compressed output and metadata

Compacted output, per-message line ranges, and usage statistics.

id
string
required

Unique identifier for the compact request

Example:

"cmpr-7373faf8af65"

object
string
required

Object type, always compact

Example:

"compact"

model
string
required

Model used

Example:

"morph-compactor"

output
string
required

All compacted messages joined into a single string

Example:

"def hello():\n return 1\n(filtered 3 lines)\ndef world():\n return 2"

messages
object[]
required

Per-message compaction results

Example:
usage
object
required

Usage statistics — token counts and timing for a single compaction.

Example: