Chain Conversations
On this page
Conversations Without Managing History
A major advantage of the Responses API is chaining conversations via previous_response_id. Instead of resending the entire history with every request, you simply pass the ID of the previous response. The xAI server handles reconstructing the full context.
First Request
Send your initial prompt. Retrieve the response ID (resp-xxx).
Subsequent Requests
Pass previous_response_id with your new message. The server reconstructs the context.
Full Conversation
Chain as many messages as needed. Each response points to the previous one.
Complete Python Example
from openai import OpenAI
client = OpenAI(
api_key="xai-...",
base_url="https://api.x.ai/v1"
)
# Turn 1: Introduction
response1 = client.responses.create(
model="grok-4.20-0309-reasoning",
instructions="You are a patient and clear math teacher.",
input="Hello, I would like to understand derivatives."
)
print(response1.output[0].content[0].text)
# Turn 2: The model remembers the context
response2 = client.responses.create(
model="grok-4.20-0309-reasoning",
input="Can you give me a concrete example?",
previous_response_id=response1.id
)
print(response2.output[0].content[0].text)
# Turn 3: Deep dive
response3 = client.responses.create(
model="grok-4.20-0309-reasoning",
input="And how do I calculate the derivative of x² + 3x?",
previous_response_id=response2.id
)
print(response3.output[0].content[0].text)
Notice that each request after the first contains only the new message and the reference to the previous response. The xAI server automatically reconstructs the entire history.
curl Equivalent
# Turn 1
curl https://api.x.ai/v1/responses \
-H "Authorization: Bearer $XAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-4.20-0309-reasoning",
"input": "Hello, explain the basics of Python to me."
}'
# Retrieve the ID from the response: "id": "resp-abc123"
# Turn 2
curl https://api.x.ai/v1/responses \
-H "Authorization: Bearer $XAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-4.20-0309-reasoning",
"input": "How do I write a for loop?",
"previous_response_id": "resp-abc123"
}'
Chaining and Instructions
The instructions (system prompt) defined in the first request persist throughout the chained conversation. You don’t need to repeat them.
If you modify instructions in a chained request, the new instructions replace the previous ones for that request and subsequent ones.
Chaining Limits
- The referenced response must exist on the server (either
store: trueor a recent request) - Chaining accumulates history tokens—each request is charged for the entire conversation
- There is no limit to the number of turns, but the model’s context window applies
Key Takeaways
previous_response_idchains conversations without resending history- The server automatically reconstructs the full context
- System instructions persist from turn to turn
- Each turn is charged for the entire accumulated history
- The referenced response must have been stored on the server