SPB Git

Repository Public

conversation-state.md 28.1 KB 958 lines markdown

1# Conversation state23> For the complete documentation index, see [llms.txt](/llms.txt). Markdown versions of documentation pages are available by appending `.md` to the page URL.45OpenAI provides a few ways to manage conversation state, which is important for preserving information across multiple messages or turns in a conversation.678  When troubleshooting cases where GPT-5.5 treats an intermediate update as9    the final answer, verify your integration preserves the assistant message10    `phase` field correctly. See [Phase11    parameter](https://developers.openai.com/api/docs/guides/reasoning#phase-parameter) for details.121314## Manually manage conversation state1516While each text generation request is independent and stateless, you can still implement **multi-turn conversations** by providing additional messages as parameters to your text generation request. Consider a knock-knock joke:17181920  Manually construct a past conversation2122```javascript23import OpenAI from "openai";2425const openai = new OpenAI();2627const response = await openai.responses.create({28  model: "gpt-6-astra",29  input: [30    { role: "user", content: "knock knock." },31    { role: "assistant", content: "Who's there?" },32    { role: "user", content: "Orange." },33  ],34});3536console.log(response.output_text);37```3839```python40from openai import OpenAI4142client = OpenAI()4344response = client.responses.create(45    model="gpt-6-astra",46    input=[47        {"role": "user", "content": "knock knock."},48        {"role": "assistant", "content": "Who's there?"},49        {"role": "user", "content": "Orange."},50    ],51)5253print(response.output_text)54```5556```go57package main5859import (60	"context"61	"fmt"6263	"github.com/openai/openai-go/v3"64	"github.com/openai/openai-go/v3/responses"65)6667func main() {68	client := openai.NewClient()6970	response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{71		Model: "gpt-6-astra",72		Input: responses.ResponseNewParamsInputUnion{73			OfInputItemList: responses.ResponseInputParam{74				responses.ResponseInputItemParamOfMessage("Knock knock.", responses.EasyInputMessageRoleUser),75				responses.ResponseInputItemParamOfMessage("Who's there?", responses.EasyInputMessageRoleAssistant),76				responses.ResponseInputItemParamOfMessage("Orange.", responses.EasyInputMessageRoleUser),77			},78		},79	})80	if err != nil {81		panic(err)82	}8384	fmt.Println(response.OutputText())85}86```8788```java89import com.openai.client.OpenAIClient;90import com.openai.client.okhttp.OpenAIOkHttpClient;91import com.openai.models.responses.EasyInputMessage;92import com.openai.models.responses.ResponseCreateParams;93import com.openai.models.responses.ResponseInputItem;94import java.util.List;9596ResponseCreateParams params =97    ResponseCreateParams.builder()98        .model("gpt-6-astra")99        .inputOfResponse(100            List.of(101                ResponseInputItem.ofEasyInputMessage(102                    EasyInputMessage.builder()103                        .role(EasyInputMessage.Role.USER)104                        .content("Knock knock.")105                        .build()),106                ResponseInputItem.ofEasyInputMessage(107                    EasyInputMessage.builder()108                        .role(EasyInputMessage.Role.ASSISTANT)109                        .content("Who's there?")110                        .build()),111                ResponseInputItem.ofEasyInputMessage(112                    EasyInputMessage.builder()113                        .role(EasyInputMessage.Role.USER)114                        .content("Orange.")115                        .build())))116        .build();117118client.responses().create(params).output().stream()119    .flatMap(item -> item.message().stream())120    .flatMap(message -> message.content().stream())121    .flatMap(content -> content.outputText().stream())122    .forEach(text -> System.out.println(text.text()));123```124125```csharp126using OpenAI.Responses;127#pragma warning disable OPENAI001128129string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;130ResponsesClient client = new(key);131132ResponseResult response = await client.CreateResponseAsync(133    "gpt-6-astra",134    [135        ResponseItem.CreateUserMessageItem("Knock knock."),136        ResponseItem.CreateAssistantMessageItem("Who's there?"),137        ResponseItem.CreateUserMessageItem("Orange."),138    ]139);140141Console.WriteLine(response.GetOutputText());142```143144```ruby145require "openai"146147client = OpenAI::Client.new148149response = client.responses.create(150  model: "gpt-6-astra",151  input: [152    {153      role: :user,154      content: "Knock knock."155    },156    {157      role: :assistant,158      content: "Who's there?"159    },160    {161      role: :user,162      content: "Orange."163    }164  ]165)166167puts(response.output_text)168```169170171172By using alternating `user` and `assistant` messages, you capture the previous state of a conversation in one request to the model.173174To manually share context across generated responses, include the model's previous response output as input, and append that input to your next request.175176For stateless reasoning-model requests, preserve every item in the response's `output` array. The Responses API returns encrypted reasoning items by default. Replaying the complete output keeps reasoning items and assistant `phase` values intact. Models that support persisted reasoning can use `reasoning.context: "all_turns"` to render the available reasoning from earlier turns into the next sample. See [preserve reasoning across calls](https://developers.openai.com/api/docs/guides/reasoning#preserve-reasoning-across-calls).177178In the following example, we ask the model to tell a joke, followed by a request for another joke. Appending previous responses to new requests in this way helps ensure conversations feel natural and retain the context of previous interactions.179180181182183  Manually manage conversation state with the Responses API.184185```javascript186import OpenAI from "openai";187import { toResponseInputItems } from "openai/lib/responses/ResponseInputItems";188189const openai = new OpenAI();190191let history = [192  {193    role: "user",194    content: "tell me a joke",195  },196];197198const response = await openai.responses.create({199  model: "gpt-6-astra",200  input: history,201  store: false,202});203204console.log(response.output_text);205206// Add replayable output items, including reasoning items, to the history207history.push(...toResponseInputItems(response.output));208209history.push({210  role: "user",211  content: "tell me another",212});213214const secondResponse = await openai.responses.create({215  model: "gpt-6-astra",216  input: history,217  store: false,218});219220console.log(secondResponse.output_text);221```222223```python224from openai import OpenAI225226client = OpenAI()227228history = [{"role": "user", "content": "tell me a joke"}]229230response = client.responses.create(231    model="gpt-6-astra",232    input=history,233    store=False,234)235236print(response.output_text)237238# Add all response output items, including encrypted reasoning items, to the conversation239history += response.output240241history.append({"role": "user", "content": "tell me another"})242243second_response = client.responses.create(244    model="gpt-6-astra",245    input=history,246    store=False,247)248249print(second_response.output_text)250```251252```go253package main254255import (256	"context"257	"encoding/json"258	"fmt"259260	"github.com/openai/openai-go/v3"261	"github.com/openai/openai-go/v3/responses"262)263264func main() {265	client := openai.NewClient()266	history := responses.ResponseInputParam{267		responses.ResponseInputItemParamOfMessage("tell me a joke", responses.EasyInputMessageRoleUser),268	}269	first, err := client.Responses.New(context.Background(), responses.ResponseNewParams{270		Model: "gpt-6-astra",271		Input: responses.ResponseNewParamsInputUnion{OfInputItemList: history},272		Store: openai.Bool(false),273	})274	if err != nil {275		panic(err)276	}277	fmt.Println(first.OutputText())278279	history = append(history, outputAsInput(first.Output)...)280	history = append(history, responses.ResponseInputItemParamOfMessage("tell me another", responses.EasyInputMessageRoleUser))281	second, err := client.Responses.New(context.Background(), responses.ResponseNewParams{282		Model: "gpt-6-astra",283		Input: responses.ResponseNewParamsInputUnion{OfInputItemList: history},284		Store: openai.Bool(false),285	})286	if err != nil {287		panic(err)288	}289	fmt.Println(second.OutputText())290}291292func outputAsInput(output []responses.ResponseOutputItemUnion) []responses.ResponseInputItemUnionParam {293	input := make([]responses.ResponseInputItemUnionParam, 0, len(output))294	for _, item := range output {295		var converted responses.ResponseInputItemUnion296		if err := json.Unmarshal([]byte(item.RawJSON()), &converted); err != nil {297			panic(err)298		}299		input = append(input, converted.ToParam())300	}301	return input302}303```304305```java306import com.openai.client.OpenAIClient;307import com.openai.client.okhttp.OpenAIOkHttpClient;308import com.openai.core.JsonValue;309import com.openai.models.responses.EasyInputMessage;310import com.openai.models.responses.ResponseCreateParams;311import com.openai.models.responses.ResponseInputItem;312import java.util.ArrayList;313314var history = new ArrayList<ResponseInputItem>();315history.add(316    ResponseInputItem.ofEasyInputMessage(317        EasyInputMessage.builder()318            .role(EasyInputMessage.Role.USER)319            .content("Tell me a joke.")320            .build()));321322var first =323    client324        .responses()325        .create(326            ResponseCreateParams.builder()327                .model("gpt-6-astra")328                .inputOfResponse(history)329                .store(false)330                .build());331first.output().stream()332    .flatMap(item -> item.message().stream())333    .flatMap(message -> message.content().stream())334    .flatMap(content -> content.outputText().stream())335    .forEach(text -> System.out.println(text.text()));336first.output().stream()337    .map(item -> JsonValue.from(item).convert(ResponseInputItem.class))338    .forEach(history::add);339history.add(340    ResponseInputItem.ofEasyInputMessage(341        EasyInputMessage.builder()342            .role(EasyInputMessage.Role.USER)343            .content("Tell me another.")344            .build()));345346client347    .responses()348    .create(349        ResponseCreateParams.builder()350            .model("gpt-6-astra")351            .inputOfResponse(history)352            .store(false)353            .build())354    .output()355    .stream()356    .flatMap(item -> item.message().stream())357    .flatMap(message -> message.content().stream())358    .flatMap(content -> content.outputText().stream())359    .forEach(text -> System.out.println(text.text()));360```361362```csharp363using OpenAI.Responses;364#pragma warning disable OPENAI001365366string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;367ResponsesClient client = new(key);368369List<ResponseItem> history =370[371    ResponseItem.CreateUserMessageItem("Tell me a joke."),372];373374CreateResponseOptions options = new("gpt-6-astra", history)375{376    StoredOutputEnabled = false,377    IncludedProperties =378    {379        IncludedResponseProperty.ReasoningEncryptedContent,380    },381};382ResponseResult first = await client.CreateResponseAsync(options);383Console.WriteLine(first.GetOutputText());384385history.AddRange(first.OutputItems);386history.Add(ResponseItem.CreateUserMessageItem("Tell me another."));387388options = new("gpt-6-astra", history)389{390    StoredOutputEnabled = false,391    IncludedProperties =392    {393        IncludedResponseProperty.ReasoningEncryptedContent,394    },395};396ResponseResult second = await client.CreateResponseAsync(options);397Console.WriteLine(second.GetOutputText());398```399400```ruby401require "openai"402403client = OpenAI::Client.new404history = [405  {406    role: :user,407    content: "Tell me a joke."408  }409]410411first = client.responses.create(412  model: "gpt-6-astra",413  input: history,414  store: false415)416puts(first.output_text)417418history.concat(first.output)419history << {420  role: :user,421  content: "Tell me another."422}423424second = client.responses.create(425  model: "gpt-6-astra",426  input: history,427  store: false428)429puts(second.output_text)430```431432433434## OpenAI APIs for conversation state435436Our APIs make it easier to manage conversation state automatically, so you don't have to pass inputs manually with each turn of a conversation.437438439440441442### Using the Conversations API443444The [Conversations API](https://developers.openai.com/api/reference/resources/conversations/methods/create) works with the [Responses API](https://developers.openai.com/api/reference/resources/responses/methods/create) to persist conversation state as a long-running object with its own durable identifier. After creating a conversation object, you can keep using it across sessions, devices, or jobs.445446Conversations store items, which can be messages, tool calls, tool outputs, and other data.447448  Create a conversation449450```javascript451const conversation = await client.conversations.create();452```453454```python455conversation = openai.conversations.create()456```457458```go459conversation, err := client.Conversations.New(context.Background(), conversations.ConversationNewParams{})460if err != nil {461	panic(err)462}463```464465```java466import com.openai.client.OpenAIClient;467import com.openai.client.okhttp.OpenAIOkHttpClient;468469var conversation = client.conversations().create();470471System.out.println(conversation.id());472```473474```ruby475conversation = client.conversations.create476```477478479In a multi-turn interaction, you can pass the `conversation` into subsequent responses to persist state and share context across subsequent responses, rather than having to chain multiple response items together.480481  Manage conversation state with Conversations and Responses APIs482483```javascript484const response = await client.responses.create({485  model: "gpt-6-astra",486  input: [{ role: "user", content: "What are the five Ds of dodgeball?" }],487  conversation: conversation.id,488});489490console.log(response.output_text);491```492493```python494response = openai.responses.create(495    model="gpt-6-astra",496    input=[{"role": "user", "content": "What are the 5 Ds of dodgeball?"}],497    conversation=conversation.id,498)499```500501```go502response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{503	Model: "gpt-6-astra",504	Conversation: responses.ResponseNewParamsConversationUnion{505		OfString: openai.String(conversation.ID),506	},507	Input: responses.ResponseNewParamsInputUnion{508		OfString: openai.String("What are the five Ds of dodgeball?"),509	},510})511if err != nil {512	panic(err)513}514fmt.Println(response.OutputText())515```516517```java518import com.openai.client.OpenAIClient;519import com.openai.client.okhttp.OpenAIOkHttpClient;520import com.openai.models.responses.ResponseCreateParams;521522var conversation = client.conversations().create();523524var response =525    client526        .responses()527        .create(528            ResponseCreateParams.builder()529                .model("gpt-6-astra")530                .conversation(conversation.id())531                .input("What are the five Ds of dodgeball?")532                .build());533534response.output().stream()535    .flatMap(item -> item.message().stream())536    .flatMap(message -> message.content().stream())537    .flatMap(content -> content.outputText().stream())538    .forEach(text -> System.out.println(text.text()));539```540541```ruby542response = client.responses.create(543  model: "gpt-6-astra",544  conversation: conversation.id,545  input: "What are the five Ds of dodgeball?"546)547548puts(response.output_text)549```550551552### Passing context from the previous response553554Another way to manage conversation state is to share context across generated responses with the `previous_response_id` parameter. This parameter lets you chain responses and create a threaded conversation.555556  Chain responses across turns by passing the previous response ID557558```javascript559import OpenAI from "openai";560561const openai = new OpenAI();562563const response = await openai.responses.create({564  model: "gpt-6-astra",565  input: "tell me a joke",566  store: true,567});568569console.log(response.output_text);570571const secondResponse = await openai.responses.create({572  model: "gpt-6-astra",573  previous_response_id: response.id,574  input: [{ role: "user", content: "explain why this is funny." }],575  store: true,576});577578console.log(secondResponse.output_text);579```580581```python582from openai import OpenAI583584client = OpenAI()585586response = client.responses.create(587    model="gpt-6-astra",588    input="tell me a joke",589)590print(response.output_text)591592second_response = client.responses.create(593    model="gpt-6-astra",594    previous_response_id=response.id,595    input=[{"role": "user", "content": "explain why this is funny."}],596)597print(second_response.output_text)598```599600```go601package main602603import (604	"context"605	"fmt"606607	"github.com/openai/openai-go/v3"608	"github.com/openai/openai-go/v3/responses"609)610611func main() {612	client := openai.NewClient()613614	first, err := client.Responses.New(context.Background(), responses.ResponseNewParams{615		Model: "gpt-6-astra",616		Input: responses.ResponseNewParamsInputUnion{617			OfString: openai.String("Tell me a joke."),618		},619	})620	if err != nil {621		panic(err)622	}623	fmt.Println(first.OutputText())624625	second, err := client.Responses.New(context.Background(), responses.ResponseNewParams{626		Model:              "gpt-6-astra",627		PreviousResponseID: openai.String(first.ID),628		Input: responses.ResponseNewParamsInputUnion{629			OfString: openai.String("Explain why this is funny."),630		},631	})632	if err != nil {633		panic(err)634	}635	fmt.Println(second.OutputText())636}637```638639```java640import com.openai.client.OpenAIClient;641import com.openai.client.okhttp.OpenAIOkHttpClient;642import com.openai.models.responses.ResponseCreateParams;643644var first =645    client646        .responses()647        .create(648            ResponseCreateParams.builder().model("gpt-6-astra").input("Tell me a joke.").build());649650first.output().stream()651    .flatMap(item -> item.message().stream())652    .flatMap(message -> message.content().stream())653    .flatMap(content -> content.outputText().stream())654    .forEach(text -> System.out.println(text.text()));655656var second =657    client658        .responses()659        .create(660            ResponseCreateParams.builder()661                .model("gpt-6-astra")662                .input("Explain why this is funny.")663                .previousResponseId(first.id())664                .build());665second.output().stream()666    .flatMap(item -> item.message().stream())667    .flatMap(message -> message.content().stream())668    .flatMap(content -> content.outputText().stream())669    .forEach(text -> System.out.println(text.text()));670```671672```csharp673using OpenAI.Responses;674#pragma warning disable OPENAI001675676string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;677ResponsesClient client = new(key);678679ResponseResult first = await client.CreateResponseAsync(680    "gpt-6-astra",681    "Tell me a joke."682);683Console.WriteLine(first.GetOutputText());684685ResponseResult second = await client.CreateResponseAsync(686    "gpt-6-astra",687    "Explain why this is funny.",688    previousResponseId: first.Id689);690Console.WriteLine(second.GetOutputText());691```692693```ruby694require "openai"695696client = OpenAI::Client.new697698first = client.responses.create(699  model: "gpt-6-astra",700  input: "Tell me a joke."701)702puts(first.output_text)703704second = client.responses.create(705  model: "gpt-6-astra",706  previous_response_id: first.id,707  input: "Explain why this is funny."708)709puts(second.output_text)710```711712713In the following example, we ask the model to tell a joke. Separately, we ask the model to explain why it's funny, and the model has all necessary context to deliver a good response.714715716  Manually manage conversation state with the Responses API717718```javascript719import OpenAI from "openai";720721const openai = new OpenAI();722723const response = await openai.responses.create({724  model: "gpt-6-astra",725  input: "tell me a joke",726  store: true,727});728729console.log(response.output_text);730731const secondResponse = await openai.responses.create({732  model: "gpt-6-astra",733  previous_response_id: response.id,734  input: [{ role: "user", content: "explain why this is funny." }],735  store: true,736});737738console.log(secondResponse.output_text);739```740741```python742from openai import OpenAI743744client = OpenAI()745746response = client.responses.create(747    model="gpt-6-astra",748    input="tell me a joke",749)750print(response.output_text)751752second_response = client.responses.create(753    model="gpt-6-astra",754    previous_response_id=response.id,755    input=[{"role": "user", "content": "explain why this is funny."}],756)757print(second_response.output_text)758```759760```go761package main762763import (764	"context"765	"fmt"766767	"github.com/openai/openai-go/v3"768	"github.com/openai/openai-go/v3/responses"769)770771func main() {772	client := openai.NewClient()773774	first, err := client.Responses.New(context.Background(), responses.ResponseNewParams{775		Model: "gpt-6-astra",776		Input: responses.ResponseNewParamsInputUnion{777			OfString: openai.String("Tell me a joke."),778		},779	})780	if err != nil {781		panic(err)782	}783	fmt.Println(first.OutputText())784785	second, err := client.Responses.New(context.Background(), responses.ResponseNewParams{786		Model:              "gpt-6-astra",787		PreviousResponseID: openai.String(first.ID),788		Input: responses.ResponseNewParamsInputUnion{789			OfString: openai.String("Explain why this is funny."),790		},791	})792	if err != nil {793		panic(err)794	}795	fmt.Println(second.OutputText())796}797```798799```java800import com.openai.client.OpenAIClient;801import com.openai.client.okhttp.OpenAIOkHttpClient;802import com.openai.models.responses.ResponseCreateParams;803804var first =805    client806        .responses()807        .create(808            ResponseCreateParams.builder().model("gpt-6-astra").input("Tell me a joke.").build());809810first.output().stream()811    .flatMap(item -> item.message().stream())812    .flatMap(message -> message.content().stream())813    .flatMap(content -> content.outputText().stream())814    .forEach(text -> System.out.println(text.text()));815816var second =817    client818        .responses()819        .create(820            ResponseCreateParams.builder()821                .model("gpt-6-astra")822                .input("Explain why this is funny.")823                .previousResponseId(first.id())824                .build());825second.output().stream()826    .flatMap(item -> item.message().stream())827    .flatMap(message -> message.content().stream())828    .flatMap(content -> content.outputText().stream())829    .forEach(text -> System.out.println(text.text()));830```831832```csharp833using OpenAI.Responses;834#pragma warning disable OPENAI001835836string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;837ResponsesClient client = new(key);838839ResponseResult first = await client.CreateResponseAsync(840    "gpt-6-astra",841    "Tell me a joke."842);843Console.WriteLine(first.GetOutputText());844845ResponseResult second = await client.CreateResponseAsync(846    "gpt-6-astra",847    "Explain why this is funny.",848    previousResponseId: first.Id849);850Console.WriteLine(second.GetOutputText());851```852853```ruby854require "openai"855856client = OpenAI::Client.new857858first = client.responses.create(859  model: "gpt-6-astra",860  input: "Tell me a joke."861)862puts(first.output_text)863864second = client.responses.create(865  model: "gpt-6-astra",866  previous_response_id: first.id,867  input: "Explain why this is funny."868)869puts(second.output_text)870```871872873#### `previous_response_id` in WebSocket mode874875If you are using [the Responses API WebSocket mode](https://developers.openai.com/api/docs/guides/websocket-mode), continuation uses the same `previous_response_id` semantics as HTTP mode, but over a persistent socket with repeated `response.create` events.876877The connection-local cache keeps recent previous responses in memory for low-latency continuation. When you use `stream_id`, each lane can retain its latest response; `previous_response_id` still controls lineage, so a new lane can fork from a response on another lane while that response remains available. If an uncached ID cannot be resolved, send a new turn with `previous_response_id` set to `null` and pass full input context.878879880881  882883##### Data retention for model responses884885886      Response objects are saved for 30 days by default. They can be viewed in the dashboard 887      [logs](https://platform.openai.com/logs?api=responses) page or 888      [retrieved](https://developers.openai.com/api/reference/resources/responses/methods/retrieve) via the API. 889      You can disable this behavior by setting `store` to `false`890      when creating a Response.891892      Conversation objects and items in them are not subject to the 30 day TTL. Any response attached to a conversation will have its items persisted with no 30 day TTL.893894      OpenAI does not use data sent via API to train our models without your explicit consent—[learn more](https://developers.openai.com/api/docs/guides/your-data).895  896897898899900901902Even when using `previous_response_id`, all previous input tokens for responses in the chain are billed as input tokens in the API.903904905906## Managing the context window907908Understanding context windows will help you successfully create threaded conversations and manage state across model interactions.909910The **context window** is the maximum number of tokens that can be used in a single request. This max tokens number includes input, output, and reasoning tokens. To learn your model's context window, see [model details](https://developers.openai.com/api/docs/models).911912### Managing context for text generation913914As your inputs become more complex, or you include more turns in a conversation, you'll need to consider both **output token** and **context window** limits. Model inputs and outputs are metered in [**tokens**](https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them), which are parsed from inputs to analyze their content and intent and assembled to render logical outputs. Models have limits on token usage during the lifecycle of a text generation request.915916- **Output tokens** are the tokens generated by a model in response to a prompt. Each model has different [limits for output tokens](https://developers.openai.com/api/docs/models). For example, `gpt-4o-2024-08-06` can generate a maximum of 16,384 output tokens.917- A **context window** describes the total tokens that can be used for both input and output tokens (and for some models, [reasoning tokens](https://developers.openai.com/api/docs/guides/reasoning)). Compare the [context window limits](https://developers.openai.com/api/docs/models) of our models. For example, `gpt-4o-2024-08-06` has a total context window of 128k tokens.918919If you create a large prompt—often by including extra context, data, or examples for the model—you run the risk of exceeding the allocated context window for a model, which might result in truncated outputs.920921Use the [tokenizer tool](https://platform.openai.com/tokenizer), built with the [tiktoken library](https://github.com/openai/tiktoken), to see how many tokens are in a particular string of text.922923924925For example, when making an API request to the [Responses API](https://developers.openai.com/api/reference/resources/responses) with a reasoning enabled model, like the [o1 model](https://developers.openai.com/api/docs/guides/reasoning), the following token counts will apply toward the context window total:926927- Input tokens (inputs you include in the `input` array for the [Responses API](https://developers.openai.com/api/reference/resources/responses))928- Output tokens (tokens generated in response to your prompt) 929- Reasoning tokens (used by the model to plan a response)930931932Tokens generated in excess of the context window limit may be truncated in API responses.933934![context window visualization](https://cdn.openai.com/API/docs/images/context-window.png)935936You can estimate the number of tokens your messages will use with the [tokenizer tool](https://platform.openai.com/tokenizer).937938<a id="compaction-advanced"></a>939940### Compaction941942Detailed compaction guidance now lives in943[Compaction](https://developers.openai.com/api/docs/guides/compaction).944945- For `/responses` with `context_management` and `compact_threshold`, see946  [Server-side compaction](https://developers.openai.com/api/docs/guides/compaction#server-side-compaction).947- For explicit compaction control, see948  [Standalone compact endpoint](https://developers.openai.com/api/docs/guides/compaction#standalone-compact-endpoint)949  and the [`/responses/compact` API reference](https://developers.openai.com/api/reference/resources/responses/methods/compact).950951## Next steps952953For more specific examples and use cases, visit the [OpenAI Cookbook](https://developers.openai.com/cookbook), or learn more about using the APIs to extend model capabilities:954955-   [Receive JSON responses with Structured Outputs](https://developers.openai.com/api/docs/guides/structured-outputs)956-   [Extend the models with function calling](https://developers.openai.com/api/docs/guides/function-calling)957-   [Enable streaming for real-time responses](https://developers.openai.com/api/docs/guides/streaming-responses)958-   [Build a computer-using agent](https://developers.openai.com/api/docs/guides/tools-computer-use)
Files main
Menu

Theme