GET and DELETE: Managing Responses
On this page
Beyond Creation
The Responses API isn’t limited to creating responses. Two complementary endpoints let you retrieve and delete stored responses. These operations are essential for building robust applications with history management.
GET /v1/responses/{id}
Retrieve a Response
curl https://api.x.ai/v1/responses/resp-abc123def456 \
-H "Authorization: Bearer $XAI_API_KEY"
The API returns the exact same JSON object as during creation:
{
"id": "resp-abc123def456",
"object": "response",
"created_at": 1712000000,
"status": "completed",
"model": "grok-4.20-0309-reasoning",
"output": [...],
"usage": {...}
}
Practical Use Cases
Conversation Resumption: Your application stores the last response_id in the user session. When reconnecting, retrieve the response to display the previous context.
# User returns after a break
last_response_id = get_from_session(user_id)
previous = client.responses.retrieve(last_response_id)
# Display the last exchange
print(f"Last response: {previous.output[0].content[0].text[:200]}...")
# Continue the conversation
new_response = client.responses.create(
model="grok-4.20-0309-reasoning",
input="Where were we?",
previous_response_id=last_response_id
)
Cost Audit: Iterate through your stored responses to analyze consumption by user or feature.
response = client.responses.retrieve("resp-abc123")
cost_usd = response.usage.cost_in_nano_usd / 1e9
input_tokens = response.usage.input_tokens
output_tokens = response.usage.output_tokens
print(f"Cost: ${cost_usd:.6f} | In: {input_tokens} | Out: {output_tokens}")
Error Handling
If the response doesn’t exist (deleted or expired), the API returns a 404 error:
{
"error": {
"message": "Response not found",
"type": "not_found_error"
}
}
Handle this case in your code:
try:
response = client.responses.retrieve("resp-expired123")
except Exception as e:
print(f"Response not found: {e}")
# Start a new conversation
DELETE /v1/responses/{id}
Delete a Response
curl -X DELETE https://api.x.ai/v1/responses/resp-abc123def456 \
-H "Authorization: Bearer $XAI_API_KEY"
Deletion returns a 200 status on success. The operation is irreversible.
Impact on Chained Conversations
When you delete a response referenced by others via previous_response_id, that response’s context disappears from the chain. Subsequent responses aren’t deleted, but they lose access to the prior context.
resp-1 → resp-2 → resp-3 → resp-4
↑ deleted
If you delete resp-2, resp-3 and resp-4 still exist, but the context from resp-1 and resp-2 is no longer accessible for new chained requests.
Automated Cleanup
For production applications, implement periodic cleanup:
import datetime
def cleanup_old_responses(response_ids, max_age_days=7):
for rid in response_ids:
try:
resp = client.responses.retrieve(rid)
age = datetime.datetime.now().timestamp() - resp.created_at
if age > max_age_days * 86400:
client.responses.delete(rid)
print(f"Deleted: {rid}")
except Exception:
pass # Already deleted or expired
Best Practices
- Store IDs: keep
response_ids in your database along with conversation metadata - Clean up regularly: don’t wait for the automatic 30-day expiration for sensitive data
- Handle 404s: a response can disappear between requests (expiration, manual deletion)
- Document your chains: for debugging, trace the
previous_response_idlinks between your responses
Key Takeaways
GET /v1/responses/{id}retrieves a response with all its content and metadataDELETE /v1/responses/{id}permanently deletes a response—irreversible operation- Deleting an intermediate response breaks the conversation chain context
- Handle 404 errors: responses expire automatically after 30 days
- Keep
response_ids in your own database for traceability