You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/running_agents.md
+65Lines changed: 65 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -121,6 +121,71 @@ Sessions automatically:
121
121
122
122
See the [Sessions documentation](sessions.md) for more details.
123
123
124
+
125
+
### Server-managed conversations
126
+
127
+
You can also let the OpenAI conversation state feature manage conversation state on the server side, instead of handling it locally with `to_input_list()` or `Sessions`. This allows you to preserve conversation history without manually resending all past messages. See the [OpenAI Conversation state guide](https://platform.openai.com/docs/guides/conversation-state?api-mode=responses) for more details.
128
+
129
+
OpenAI provides two ways to track state across turns:
130
+
131
+
#### 1. Using `conversation_id`
132
+
133
+
You first create a conversation using the OpenAI Conversations API and then reuse its ID for every subsequent call:
134
+
135
+
```python
136
+
from agents import Agent, Runner
137
+
from openai import AsyncOpenAI
138
+
139
+
client = AsyncOpenAI()
140
+
141
+
asyncdefmain():
142
+
# Create a server-managed conversation
143
+
conversation =await client.conversations.create()
144
+
conv_id = conversation.id
145
+
146
+
agent = Agent(name="Assistant", instructions="Reply very concisely.")
147
+
148
+
# First turn
149
+
result1 =await Runner.run(agent, "What city is the Golden Gate Bridge in?", conversation_id=conv_id)
150
+
print(result1.final_output)
151
+
# San Francisco
152
+
153
+
# Second turn reuses the same conversation_id
154
+
result2 =await Runner.run(
155
+
agent,
156
+
"What state is it in?",
157
+
conversation_id=conv_id,
158
+
)
159
+
print(result2.final_output)
160
+
# California
161
+
```
162
+
163
+
#### 2. Using `previous_response_id`
164
+
165
+
Another option is **response chaining**, where each turn links explicitly to the response ID from the previous turn.
166
+
167
+
```python
168
+
from agents import Agent, Runner
169
+
170
+
asyncdefmain():
171
+
agent = Agent(name="Assistant", instructions="Reply very concisely.")
172
+
173
+
# First turn
174
+
result1 =await Runner.run(agent, "What city is the Golden Gate Bridge in?")
175
+
print(result1.final_output)
176
+
# San Francisco
177
+
178
+
# Second turn, chained to the previous response
179
+
result2 =await Runner.run(
180
+
agent,
181
+
"What state is it in?",
182
+
previous_response_id=result1.last_response_id,
183
+
)
184
+
print(result2.final_output)
185
+
# California
186
+
```
187
+
188
+
124
189
## Long running agents & human-in-the-loop
125
190
126
191
You can use the Agents SDK [Temporal](https://temporal.io/) integration to run durable, long-running workflows, including human-in-the-loop tasks. View a demo of Temporal and the Agents SDK working in action to complete long-running tasks [in this video](https://www.youtube.com/watch?v=fFBZqzT4DD8), and [view docs here](https://github.com/temporalio/sdk-python/tree/main/temporalio/contrib/openai_agents).
0 commit comments