Responses
Compact a conversation
Compacts a conversation into a response object suitable for long-running and zero-data-retention scenarios.
Kérelem törzse
model
string
kötelező
The model deployment to use for the compaction of this response.
input
string | OpenAI.InputItem[] | null
prompt_cache_retention
enum
prompt_cache_options
OpenAI.PromptCacheOptionsParam | null
Options for prompt caching. Supported for gpt-5.6 and later models. By default, OpenAI automatically chooses one implicit cache breakpoint. You can add explicit breakpoints to content blocks with prompt_cache_breakpoint. Each request can write up to four breakpoints. For cache matching, OpenAI considers up to the latest 80 breakpoints in the conversation, without a content-block lookback limit. Set mode to explicit to disable the implicit breakpoint. The ttl defaults to 30m, which is currently the only supported value. See the prompt caching guide for current details.
ttl
enum
mode
enum
service_tier
enum
Válaszok
The request has succeeded.
Törzs
id
string
kötelező
The unique identifier for the compacted response.
object
enum
kötelező
The object type. Always response.compaction.
output
OpenAI.ItemField[]
kötelező
The compacted list of output items.
created_at
integer
kötelező
Unix timestamp (in seconds) when the compacted conversation was created.
usage
OpenAI.ResponseUsage
kötelező
Represents token usage details including input tokens, output tokens, a breakdown of output tokens, and the total tokens used.
input_tokens_details
OpenAI.ResponseUsageInputTokensDetails
kötelező
output_tokens_details
OpenAI.ResponseUsageOutputTokensDetails
kötelező
Kérés
curl -X POST https://api.openai.com/v1/responses/compact \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-5.1-codex-max",
"input": [
{
"role": "user",
"content": "Create a simple landing page for a dog petting café."
},
{
"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"
}
]
}'
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);
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ.get("OPENAI_API_KEY"), # This is the default and can be omitted
)
compacted_response = client.responses.compact(
model="gpt-5.4",
)
print(compacted_response.id)
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env['OPENAI_API_KEY'], // This is the default and can be omitted
});
const compactedResponse = await client.responses.compact({ model: 'gpt-5.4' });
console.log(compactedResponse.id);
package main
import (
"context"
"fmt"
"github.com/openai/openai-go"
"github.com/openai/openai-go/option"
"github.com/openai/openai-go/responses"
)
func main() {
client := openai.NewClient(
option.WithAPIKey("My API Key"),
)
compactedResponse, err := client.Responses.Compact(context.TODO(), responses.ResponseCompactParams{
Model: responses.ResponseCompactParamsModelGPT5_4,
})
if err != nil {
panic(err.Error())
}
fmt.Printf("%+v
", compactedResponse.ID)
}
package com.openai.example;
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.responses.CompactedResponse;
import com.openai.models.responses.ResponseCompactParams;
public final class Main {
private Main() {}
public static void main(String[] args) {
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
ResponseCompactParams params = ResponseCompactParams.builder()
.model(ResponseCompactParams.Model.GPT_5_4)
.build();
CompactedResponse compactedResponse = client.responses().compact(params);
}
}
require "openai"
openai = OpenAI::Client.new(api_key: "My API Key")
compacted_response = openai.responses.compact(model: :"gpt-5.4")
puts(compacted_response)
Válasz
{
"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
},
"output_tokens": 438,
"output_tokens_details": {
"reasoning_tokens": 64
},
"total_tokens": 577
}
}