API / chain-responses-with-previous-id

API

Chain Responses with previous_response_id

Chain Responses with previous_response_id

Pass previous_response_id on POST /v1/responses with only the new turn in input. The server rehydrates the stored prior conversation so you skip resending full history. On the xAI SDK, pass previous_response_id=response.id into client.chat.create. Vercel AI SDK uses providerOptions.xai.previousResponseId. The prior response must still be stored within the 30-day retention window.

Chat Completions multi-turn works by resending the full message list on each call. The Responses API adds previous_response_id (and store) for stateful continuation.

OpenAI SDK

response = client.responses.create(
    model="grok-4.6",
    input=[
        {"role": "system", "content": "You are Grok, an AI agent built to answer helpful questions."},
        {"role": "user", "content": "How big is the universe?"},
    ],
)
print(response.id)

second_response = client.responses.create(
    model="grok-4.6",
    previous_response_id=response.id,
    input=[
        {"role": "user", "content": "How do stars form?"},
    ],
)
print(second_response.id)

xAI SDK

chat = client.chat.create(model="grok-4.6", store_messages=True)
chat.append(system("You are Grok, an AI agent built to answer helpful questions."))
chat.append(user("How big is the universe?"))
response = chat.sample()

chat = client.chat.create(
    model="grok-4.6",
    previous_response_id=response.id,
    store_messages=True,
)
chat.append(user("How do stars form?"))
second_response = chat.sample()

curl

curl https://api.x.ai/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $XAI_API_KEY" \
  -m 3600 \
  -d '{
    "model": "grok-4.6",
    "previous_response_id": "The previous response ID",
    "input": [
        {
            "role": "user",
            "content": "How do stars form?"
        }
    ]
}'

Vercel AI SDK

const result = await generateText({
  model: xai.responses('grok-4.6'),
  system: "You are Grok, an AI agent built to answer helpful questions.",
  prompt: "How big is the universe?",
});
const responseId = result.response.id;

const { text: secondResponse } = await generateText({
  model: xai.responses('grok-4.6'),
  prompt: "How do stars form?",
  providerOptions: {
    xai: {
      previousResponseId: responseId,
    },
  },
});

Under store: false or ZDR, keep local history or pass encrypted thinking / prior response.output in input instead of relying on a stored id. After 30 days, rebuild the same way.

Pitfalls

  • Needs a stored prior response (default store-on) within 30 days. Deleted or expired ids will not chain.
  • With store: false or ZDR over HTTP without a live connection cache, continuation can fail with previous_response_not_found.
  • WebSocket sessions keep an in-memory cache for up to 25 minutes; a failed turn can evict that previous_response_id from the connection cache.
  • Console API credits are separate from SuperGrok's weekly pool on grok.com.