Use context compaction
Use context compaction
POST https://api.x.ai/v1/responses/compact shrinks a long conversation into one compaction item with opaque encrypted_content. Pass that item as the head of the next /v1/responses input, then append new user turns after it (never before).
Compact, then continue
# Step 1 - compact
curl -s https://api.x.ai/v1/responses/compact \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $XAI_API_KEY" \
-d '{
"model": "grok-4.6",
"input": [
{"role": "system", "content": "You are a concise and knowledgeable science tutor."},
{"role": "user", "content": "What is the Higgs boson and why is it important?"},
{"role": "assistant", "content": "The Higgs boson is an elementary particle..."},
{"role": "user", "content": "How does the Higgs mechanism actually work?"},
{"role": "assistant", "content": "The Higgs mechanism works through spontaneous symmetry breaking..."}
]
}'
# Step 2 - continue with the compaction item at the head of input
curl -s https://api.x.ai/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $XAI_API_KEY" \
-d '{
"model": "grok-4.6",
"input": [
{
"type": "compaction",
"id": "cmp_abc123",
"encrypted_content": "<paste encrypted_content from step 1>"
},
{"role": "user", "content": "Based on our earlier conversation, what gives particles their mass?"}
]
}'
OpenAI SDK
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["XAI_API_KEY"],
base_url="https://api.x.ai/v1",
)
compacted = client.responses.compact(
model="grok-4.6",
input=[
{"role": "system", "content": "You are a concise and knowledgeable science tutor."},
{"role": "user", "content": "What is the Higgs boson and why is it important?"},
{"role": "assistant", "content": "The Higgs boson is an elementary particle..."},
{"role": "user", "content": "How does the Higgs mechanism actually work?"},
{"role": "assistant", "content": "The Higgs mechanism works through spontaneous symmetry breaking..."},
],
)
print(f"Compaction ID: {compacted.id}")
print(f"Dropped messages: {compacted.usage.dropped_message_count}")
followup = client.responses.create(
model="grok-4.6",
input=[
*compacted.output,
{"role": "user", "content": "Based on our earlier conversation, what gives particles their mass?"},
],
)
print(followup.output_text)
xAI SDK
import os
from xai_sdk import Client
from xai_sdk.chat import system, user
client = Client(api_key=os.environ["XAI_API_KEY"])
# use_encrypted_content=True so prior reasoning survives compaction
chat = client.chat.create(model="grok-4.6", use_encrypted_content=True)
chat.append(system("You are a concise and knowledgeable science tutor."))
chat.append(user("What is the Higgs boson and why is it important?"))
chat.append(chat.sample())
chat.append(user("How does the Higgs mechanism actually work?"))
chat.append(chat.sample())
compact = client.chat.compact_context(model="grok-4.6", messages=chat.messages)
print(f"Compaction ID: {compact.id}")
print(f"Dropped messages: {compact.dropped_message_count}")
chat.append(compact)
chat.append(user("Based on our earlier conversation, what gives particles their mass?"))
print(chat.sample().content)
For long agent loops, chat.compact() runs compaction in place and replaces the live message list with the compaction item so later chat.sample() calls ride the compacted prefix.
Response shape
| Field | Meaning |
|---|---|
id |
Stable id (cmp_...), also on the inner compaction item |
object |
Always response.compaction |
output |
One compaction item; pass it verbatim into the next request |
usage.dropped_message_count |
How many input messages were folded in |
Pitfalls
- The conversation must already fit context. Compaction shrinks history; it does not rescue an over-limit request.
- At most one compaction per call. Re-compacting later (including an already-compacted thread) is fine.
- Compaction itself uses tokens (
usage.input_tokens/usage.output_tokens). - Append new turns after the compaction item, never before. Do not prune or reorder
output. - Console API credits are separate from SuperGrok's weekly pool on grok.com.