Compact a response
responses.compact(ResponseCompactParams**kwargs) -> CompactedResponse
POST/responses/compact
Compact a conversation. Returns a compacted response object.
Learn when and how to compact long-running conversations in the conversation state guide. For ZDR-compatible compaction details, see Compaction (advanced).
Parameters
instructions: Optional[str]
A system (or developer) message inserted into the model’s context.
When used along with previous_response_id, the instructions from a previous response will not be carried over to the next response. This makes it simple to swap out system (or developer) messages in new responses.
previous_response_id: Optional[str]
The unique ID of the previous response to the model. Use this to create multi-turn conversations. Learn more about conversation state. Cannot be used in conjunction with conversation.
Compact a response
from openai import OpenAI
client = OpenAI()
compacted_response = client.responses.compact(
model="gpt-5.1-codex-max",
input=[
{
"role": "user",
"content": "Create a simple landing page for a dog petting cafe.",
},
# All items returned from previous requests are included here, like reasoning, message, function call, etc.
{
"id": "msg_001",
"type": "message",
"status": "completed",
"content": [
{
"type": "output_text",
"annotations": [],
"logprobs": [],
"text": "Below is a single file, ready-to-use landing page for a dog petting café:...",
},
],
"role": "assistant",
},
]
)
# Pass the compacted_response.output as input to the next request
print(compacted_response)
{
"id": "resp_001",
"object": "response.compaction",
"created_at": 1764967971,
"output": [
{
"id": "msg_000",
"type": "message",
"status": "completed",
"content": [
{
"type": "input_text",
"text": "Create a simple landing page for a dog petting cafe."
}
],
"role": "user"
},
{
"id": "cmp_001",
"type": "compaction",
"encrypted_content": "gAAAAABpM0Yj-...="
}
],
"usage": {
"input_tokens": 139,
"input_tokens_details": {
"cached_tokens": 0,
"cache_write_tokens": 0
},
"output_tokens": 438,
"output_tokens_details": {
"reasoning_tokens": 64
},
"total_tokens": 577
}
}
Returns Examples
{
"id": "resp_001",
"object": "response.compaction",
"created_at": 1764967971,
"output": [
{
"id": "msg_000",
"type": "message",
"status": "completed",
"content": [
{
"type": "input_text",
"text": "Create a simple landing page for a dog petting cafe."
}
],
"role": "user"
},
{
"id": "cmp_001",
"type": "compaction",
"encrypted_content": "gAAAAABpM0Yj-...="
}
],
"usage": {
"input_tokens": 139,
"input_tokens_details": {
"cached_tokens": 0,
"cache_write_tokens": 0
},
"output_tokens": 438,
"output_tokens_details": {
"reasoning_tokens": 64
},
"total_tokens": 577
}
}