SpyBara
Go Premium

Documentation 2026-11-06 23:00 UTC to 2026-11-07 22:59 UTC

6 files changed +108 −6. View all changes and history on the product overview
2026
Fri 13 23:58 Thu 12 22:57 Wed 11 23:57 Tue 10 23:02 Mon 9 23:02 Sun 8 22:59 Sat 7 22:59 Fri 6 23:00 Thu 5 23:00 Wed 4 22:58 Tue 3 22:58 Mon 2 23:58 Sun 1 23:00
Details

8size while preserving state needed for subsequent turns.8size while preserving state needed for subsequent turns.

9 9 

10Compaction helps you balance quality, cost, and latency as conversations grow.10Compaction helps you balance quality, cost, and latency as conversations grow.

11For guidance on preserving reasoning when changing conversation history, see

12[Preserved reasoning](https://developers.openai.com/api/docs/guides/preserved-reasoning).

11 13 

12## Server-side compaction14## Server-side compaction

13 15 

Details

175 175 

176For 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).176For 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).

177 177 

178When replaying reasoning items, preserve the history that produced them. See [Preserved reasoning](https://developers.openai.com/api/docs/guides/preserved-reasoning) for details on how edits to conversation history affect reasoning.

179 

178In 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.180In 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.

179 181 

180 182 

Details

473 473 

474Note that for reasoning models like GPT-5 or o4-mini, any reasoning items474Note that for reasoning models like GPT-5 or o4-mini, any reasoning items

475 returned in model responses with tool calls must also be passed back with tool475 returned in model responses with tool calls must also be passed back with tool

476 call outputs.476 call outputs. For guidance on preserving the history that produced those

477 items, see [Preserved reasoning](https://developers.openai.com/api/docs/guides/preserved-reasoning).

477 478 

478## Defining functions479## Defining functions

479 480 

Details

1# Preserved reasoning

2 

3> For the complete documentation index, see [llms.txt](/llms.txt). Markdown versions of documentation pages are available by appending `.md` to the page URL.

4 

5When you continue a conversation with the Responses API, a model can reuse

6reasoning items from an earlier response. To preserve that reasoning when you

7manage conversation history, keep the history that produced it intact and append

8new messages and tool results at the end. The selected model and

9[`reasoning.context` setting](https://developers.openai.com/api/docs/guides/reasoning#preserve-reasoning-across-calls)

10determine which available reasoning items the model can use.

11 

12## How history changes affect reasoning

13 

14For newer frontier models, the API checks the conversation history before each

15reasoning item you send back. This history is the item's _prefix_. If you edit,

16shorten, or insert a message in that prefix, the API drops the affected

17reasoning. The request can continue with the conversation you provide, and the

18model can generate new reasoning.

19 

20For example, an agent calls a ledger tool and uses the returned rows to

21investigate a discrepancy. On the next request, your app replaces those rows

22with a subtotal. The model can still reuse reasoning it used to choose the tool,

23but the API drops reasoning that depended on the original rows, even if you send the

24reasoning item back unchanged.

25 

26## Append new instructions

27 

28When you manage history yourself, keep the previous input and the response's

29output items in their original order. Add new instructions at the point where

30they take effect.

31 

32This example uses the Python SDK in stateless mode with a model that supports

33`reasoning.context` set to `all_turns`. Stateless responses

34include encrypted reasoning content by default. See

35[Preserve reasoning without stored responses](https://developers.openai.com/api/docs/guides/reasoning#preserve-reasoning-without-stored-responses).

36 

37Append instructions to a conversation

38 

39```python

40from openai import OpenAI

41 

42client = OpenAI()

43model = "gpt-6.1-sol"

44history = [{"role": "user", "content": "Compare invoice A ($80) and B ($95)."}]

45 

46first = client.responses.create(

47 model=model,

48 reasoning={"context": "all_turns"},

49 input=history,

50 store=False,

51)

52 

53next_input = [

54 *history,

55 *(item.to_dict() for item in first.output),

56 {

57 "role": "developer",

58 "content": "For the reconciliation report, use a Markdown table.",

59 },

60 {"role": "user", "content": "Add invoice C ($110) and show the total."},

61]

62report = client.responses.create(

63 model=model,

64 reasoning={"context": "all_turns"},

65 input=next_input,

66 store=False,

67)

68print(report.output_text)

69```

70 

71 

72If you use stored responses, you can continue from the prior response with

73`previous_response_id` and send only the new input items. See

74[Conversation state](https://developers.openai.com/api/docs/guides/conversation-state).

75 

76## Migrate away from prefix edits

77 

78Compare two consecutive requests in a workflow that reuses reasoning. Find the

79first place your app changes an item it already sent, then choose the migration

80that matches the edit.

81 

82| If your app edits… | Migrate to… |

83| ---------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

84| Older turns during compaction | Use OpenAI compaction and continue from the returned context. If you create a portable text summary, let the model reason from it. Replacing the prefix can invalidate reasoning you replay after the summary. If the history includes `configuration_update` items, use an explicit `compaction_trigger`, then append a fresh update before the next user message. Restate every setting you changed, including the format if applicable. Automatic compaction, automatic truncation, and standalone `/responses/compact` don't support histories containing these updates. |

85| Developer instructions | Append a developer message when the instruction takes effect. If the response is already running, use mid-turn steering where supported. |

86| The tool list when switching modes | Keep known tool definitions stable. Restrict the next request with `tool_choice` of type `allowed_tools`. To introduce new tools, append an `additional_tools` input item with role `developer` and the definitions in `tools`. Replay it in its original position. |

87| A tool result the model has already seen | Filter the result before sending `function_call_output` the first time. If the data changes, append a correction or return it from a new tool call. |

88 

89For more on managing long conversations, see

90[Compaction](https://developers.openai.com/api/docs/guides/compaction).

Details

940 940 

941### Keeping reasoning items in context941### Keeping reasoning items in context

942 942 

943When doing [function calling](https://developers.openai.com/api/docs/guides/function-calling) with a reasoning model in the [Responses API](https://developers.openai.com/api/reference/resources/responses), we highly recommend you pass back any reasoning items returned with the last function call (in addition to the output of your function). If the model calls multiple functions consecutively, you should pass back all reasoning items, function call items, and function call output items, since the last `user` message. This allows the model to continue its reasoning process to produce better results in the most token-efficient manner.943When doing [function calling](https://developers.openai.com/api/docs/guides/function-calling) with a reasoning model in the [Responses API](https://developers.openai.com/api/reference/resources/responses), pass back the reasoning items returned with the function calls along with the tool outputs. This lets the model continue reasoning after a tool call.

944 944 

945The simplest way to do this is to pass in all reasoning items from a previous response into the next one. Our systems will smartly ignore any reasoning items that aren't relevant to your functions, and only retain those in context that are relevant. You can pass reasoning items from previous responses either using the `previous_response_id` parameter, or by manually passing in all the [output](https://developers.openai.com/api/reference/resources/responses#responses/object-output) items from a past response into the [input](https://developers.openai.com/api/reference/resources/responses/methods/create#responses-create-input) of a new one.945You can carry reasoning items forward using `previous_response_id`. If you manage conversation history yourself, include the original input and all [output](https://developers.openai.com/api/reference/resources/responses#responses/object-output) items from each response in the [input](https://developers.openai.com/api/reference/resources/responses/methods/create#responses-create-input) of the next request. Keep those items in their original order and append new items.

946 946 

947For advanced use cases where you might be truncating and optimizing parts of the context window before passing them on to the next response, just ensure all items between the last user message and your function call output are passed into the next response untouched. This will ensure that the model has all the context it needs.947For newer frontier models, changes to the history preceding a reasoning item can cause the API to drop that reasoning. See [Preserved reasoning](https://developers.openai.com/api/docs/guides/preserved-reasoning) for guidance on changing instructions and tool outputs, and managing long conversations.

948 

949Check out [this guide](https://developers.openai.com/api/docs/guides/conversation-state) to learn more about manual context management.

950 948 

951## Change reasoning mid-conversation949## Change reasoning mid-conversation

952 950 

Details

20 20 

21Older versions of the Python SDK also installed a legacy `openai` command. If you already had that package installed and the command you see does not match this guide, your shell may still be resolving the older binary. Fresh CLI installs are not affected.21Older versions of the Python SDK also installed a legacy `openai` command. If you already had that package installed and the command you see does not match this guide, your shell may still be resolving the older binary. Fresh CLI installs are not affected.

22 22 

23## Explore commands

24 

25Run `openai explore` in your terminal to browse commands before you set up an API key.

26Type a task such as `count tokens offline` to find `openai tokenizer count`.

27Use the arrow keys to highlight a match. Press <kbd>Enter</kbd> to select it or <kbd>Escape</kbd> to cancel.

28 

29Explore shows the selected command without running it. With shell integration, you can edit the selection at your prompt.

30Run `openai explore --help` for supported shells and keyboard shortcuts.

31 

23## Authentication32## Authentication

24 33 

25The CLI reads your API key from `OPENAI_API_KEY`:34The CLI reads your API key from `OPENAI_API_KEY`: