Compact a response
client.responses.compact(ResponseCompactParams { model, input, instructions, 5 more } body, RequestOptionsoptions?): CompactedResponse { id, created_at, object, 2 more }
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).
Compact a response
import OpenAI from "openai";
const openai = new OpenAI();
// Compact the previous response if you are running out of tokens
const compactedResponse = await openai.responses.compact({
model: "gpt-5.1-codex-max",
input: [
{
role: "user",
content: "Create a simple landing page for a dog petting café.",
},
// All items returned from previous requests are included here, like reasoning, message, function call, etc.
{
id: "msg_030d085c0b53e67e0069332e3a72d4819c96c6f2c4adc15d33",
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 compactedResponse.output as input to the next request
console.log(compactedResponse);
{
"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
}
}