Run a deferred Grok chat completion
Run a deferred Grok chat completion
Create a Chat Completions job with "deferred": true, keep the returned request_id, then poll GET /v1/chat/deferred-completion/{request_id}. The result is available exactly once within 24 hours, then discarded. REST and the xAI SDK support this today.
Submit, then poll
RESPONSE=$(curl -s https://api.x.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $XAI_API_KEY" \
-d '{
"messages": [
{"role": "system", "content": "You are Zaphod Beeblebrox."},
{"role": "user", "content": "126/3=?"}
],
"model": "grok-4.6",
"deferred": true
}')
REQUEST_ID=$(echo "$RESPONSE" | jq -r '.request_id')
echo "Request ID: $REQUEST_ID"
sleep 10
curl -s "https://api.x.ai/v1/chat/deferred-completion/$REQUEST_ID" \
-H "Authorization: Bearer $XAI_API_KEY"
202 Accepted with an empty body means still running. 200 returns a normal chat completion payload (including message.reasoning_content when the model produced a thinking trace).
xAI SDK
import os
from datetime import timedelta
from xai_sdk import Client
from xai_sdk.chat import user, system
client = Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(
model="grok-4.6",
messages=[system("You are Zaphod Beeblebrox.")],
)
chat.append(user("126/3=?"))
response = chat.defer(
timeout=timedelta(minutes=10),
interval=timedelta(seconds=10),
)
print(response.content)
Pitfalls
- Retrieve the result once. A second successful GET after a 200 will not return the same payload; treat the first 200 as the only copy.
- Deferred rate limits match your chat completions limits. Check the xAI Console for your quota.
- Poll with backoff. Tight loops burn quota without speeding the job.
- Console API credits are separate from SuperGrok's weekly pool on grok.com.