Repository Public
1<!-- source: https://docs.x.ai/developers/model-capabilities/text/generate-text (from https://docs.x.ai/llms-full.txt, retrieved 2026-09-19T05:28:55Z) -->2#### Model Capabilities34# Generate Text56The Responses API is the preferred way of interacting with our models via API. It allows optional **stateful interactions** with our models,7where **previous input prompts, reasoning content, and model responses are saved and stored on xAI's servers**. You can continue the interaction by appending new8prompt messages instead of resending the full conversation. This behavior is on by default. If you would like to store your request/response locally, please see [Disable storing previous request/response on server](#disable-storing-previous-requestresponse-on-server).910**The responses will be stored for 30 days, after which they will be removed. This means you can use the response ID to retrieve or continue a conversation within 30 days of sending the request.**11If you want to continue a conversation after 30 days, please store your responses history and the encrypted thinking content locally, and pass them in a new request body.1213For Python, we also offer our [xAI SDK](https://github.com/xai-org/xai-sdk-python) which covers all of our features and uses gRPC for optimal performance. It's fine to mix both. The xAI SDK allows you to interact with all our products such as Collections, Voice API, API key management, and more, while the Responses API is more suited for chatbots and usage in RESTful APIs.1415## Prerequisites1617Create an API key on the [xAI Console API Keys Page](https://console.x.ai/team/default/api-keys?utm_source=docs\&utm_medium=referral\&utm_campaign=developers-model-capabilities-text-generate-text\&utm_content=api-keys). Set your API key in your environment:1819```bash20export XAI_API_KEY="your_api_key"21```2223## Creating a new model response2425Start by creating a response:2627```python customLanguage="pythonXAI"28import os29from xai_sdk import Client30from xai_sdk.chat import user, system3132client = Client(33 api_key=os.getenv("XAI_API_KEY"),34 management_api_key=os.getenv("XAI_MANAGEMENT_API_KEY"),35 timeout=3600,36)3738chat = client.chat.create(model="grok-4.6")39chat.append(system("You are Grok, an AI agent built to answer helpful questions."))40chat.append(user("How big is the universe?"))41response = chat.sample()4243print(response)4445# The response ID that can be used to continue the conversation later4647print(response.id)48```4950```python customLanguage="pythonOpenAISDK"51import os52import httpx53from openai import OpenAI5455client = OpenAI(56 api_key="<YOUR_XAI_API_KEY_HERE>",57 base_url="https://api.x.ai/v1",58 timeout=httpx.Timeout(3600.0), # Override default timeout with longer timeout for reasoning models59)6061response = client.responses.create(62 model="grok-4.6",63 input=[64 {"role": "system", "content": "You are Grok, an AI agent built to answer helpful questions."},65 {"role": "user", "content": "How big is the universe?"},66 ],67)6869print(response)7071# The response ID that can be used to continue the conversation later7273print(response.id)74```7576```javascript customLanguage="javascriptOpenAISDK"77import OpenAI from "openai";7879const client = new OpenAI({80 apiKey: "<api key>",81 baseURL: "https://api.x.ai/v1",82 timeout: 360000, // Override default timeout with longer timeout for reasoning models83});8485const response = await client.responses.create({86 model: "grok-4.6",87 input: [88 {89 role: "system",90 content: "You are Grok, an AI agent built to answer helpful questions."91 },92 {93 role: "user",94 content: "How big is the universe?"95 },96 ],97});9899console.log(response);100101// The response ID that can be used to recall the conversation later102console.log(response.id);103```104105```javascript customLanguage="javascriptAISDK"106import { xai } from '@ai-sdk/xai';107import { generateText } from 'ai';108109const { text, response } = await generateText({110 model: xai.responses('grok-4.6'),111 system: "You are Grok, an AI agent built to answer helpful questions.",112 prompt: "How big is the universe?",113});114115console.log(text);116117// The response ID can be used to continue the conversation118console.log(response.id);119```120121```bash122curl https://api.x.ai/v1/responses \123 -H "Content-Type: application/json" \124 -H "Authorization: Bearer $XAI_API_KEY" \125 -m 3600 \126 -d '{127 "model": "grok-4.6",128 "input": [129 {130 "role": "system",131 "content": "You are Grok, an AI agent built to answer helpful questions."132 },133 {134 "role": "user",135 "content": "How big is the universe?"136 }137 ]138}'139```140141### Disable storing previous request/response on server142143If you do not want to store your previous request/response on the server, you can set `store: false` on the request.144145```python customLanguage="pythonXAI"146import os147from xai_sdk import Client148from xai_sdk.chat import user, system149150client = Client(151 api_key=os.getenv("XAI_API_KEY"),152 management_api_key=os.getenv("XAI_MANAGEMENT_API_KEY"),153 timeout=3600,154)155156chat = client.chat.create(model="grok-4.6", store_messages=False)157chat.append(system("You are Grok, an AI agent built to answer helpful questions."))158chat.append(user("How big is the universe?"))159response = chat.sample()160161print(response)162```163164```python customLanguage="pythonOpenAISDK"165import os166import httpx167from openai import OpenAI168169client = OpenAI(170 api_key="<YOUR_XAI_API_KEY_HERE>",171 base_url="https://api.x.ai/v1",172 timeout=httpx.Timeout(3600.0), # Override default timeout with longer timeout for reasoning models173)174175response = client.responses.create(176 model="grok-4.6",177 input=[178 {"role": "system", "content": "You are Grok, an AI agent built to answer helpful questions."},179 {"role": "user", "content": "How big is the universe?"},180 ],181 store=False182)183184print(response)185```186187```javascript customLanguage="javascriptOpenAISDK"188import OpenAI from "openai";189190const client = new OpenAI({191 apiKey: "<api key>",192 baseURL: "https://api.x.ai/v1",193 timeout: 360000, // Override default timeout with longer timeout for reasoning models194});195196const response = await client.responses.create({197 model: "grok-4.6",198 input: [199 {200 role: "system",201 content: "You are Grok, an AI agent built to answer helpful questions."202 },203 {204 role: "user",205 content: "How big is the universe?"206 },207 ],208 store: false209});210211console.log(response);212```213214```bash215curl https://api.x.ai/v1/responses \216 -H "Content-Type: application/json" \217 -H "Authorization: Bearer $XAI_API_KEY" \218 -m 3600 \219 -d '{220 "model": "grok-4.6",221 "input": [222 {223 "role": "system",224 "content": "You are Grok, an AI agent built to answer helpful questions."225 },226 {227 "role": "user",228 "content": "How big is the universe?"229 }230 ],231 "store": false232}'233```234235### Returning encrypted thinking content236237If you want to return the encrypted thinking traces, you need to specify `use_encrypted_content=True` in xAI SDK or gRPC request message, or `include: ["reasoning.encrypted_content"]` in the request body.238239> [!NOTE]240>241> Make sure to use a reasoning model when working with encrypted thinking content.242243Modify the steps to create a chat client (xAI SDK) or change the request body as following:244245```python customLanguage="pythonXAI"246chat = client.chat.create(model="grok-4.6",247 use_encrypted_content=True)248```249250```python customLanguage="pythonOpenAISDK"251response = client.responses.create(252 model="grok-4.6",253 input=[254 {"role": "system", "content": "You are Grok, an AI agent built to answer helpful questions."},255 {"role": "user", "content": "How big is the universe?"},256 ],257 include=["reasoning.encrypted_content"]258)259```260261```javascript customLanguage="javascriptWithoutSDK"262const response = await client.responses.create({263 model: "grok-4.6",264 input: [265 {"role": "system", "content": "You are Grok, an AI agent built to answer helpful questions."},266 {"role": "user", "content": "How big is the universe?"},267 ],268 include: ["reasoning.encrypted_content"],269});270271```272273```javascript customLanguage="javascriptAISDK"274import { xai } from '@ai-sdk/xai';275import { generateText } from 'ai';276277// Encrypted reasoning content is included automatically by the AI SDK278// as long as `store: false` is not set. No extra configuration is needed.279const { text, reasoning } = await generateText({280 model: xai.responses('grok-4.6'),281 system: "You are Grok, an AI agent built to answer helpful questions.",282 prompt: "How big is the universe?",283});284285console.log(text);286console.log(reasoning); // Contains encrypted reasoning content287```288289```bash290curl https://api.x.ai/v1/responses \291 -H "Content-Type: application/json" \292 -H "Authorization: Bearer $XAI_API_KEY" \293 -m 3600 \294 -d '{295 "model": "grok-4.6",296 "input": [297 {298 "role": "system",299 "content": "You are Grok, an AI agent built to answer helpful questions."300 },301 {302 "role": "user",303 "content": "How big is the universe?"304 }305 ],306 "include": ["reasoning.encrypted_content"]307}'308```309310See [Adding encrypted thinking content](#adding-encrypted-thinking-content) on how to use the returned encrypted thinking content when making a new request.311312## Chaining the conversation313314We now have the `id` of the first response. With Chat Completions API, we typically send a stateless new request with all the previous messages.315316With Responses API, we can send the `id` of the previous response, and the new messages to append to it.317318```python customLanguage="pythonXAI"319import os320from xai_sdk import Client321from xai_sdk.chat import user, system322323client = Client(324 api_key=os.getenv("XAI_API_KEY"),325 management_api_key=os.getenv("XAI_MANAGEMENT_API_KEY"),326 timeout=3600,327)328329chat = client.chat.create(model="grok-4.6", store_messages=True)330chat.append(system("You are Grok, an AI agent built to answer helpful questions."))331chat.append(user("How big is the universe?"))332response = chat.sample()333334print(response)335336# The response ID that can be used to continue the conversation later337338print(response.id)339340# New steps341342chat = client.chat.create(343 model="grok-4.6",344 previous_response_id=response.id,345 store_messages=True,346)347chat.append(user("How do stars form?"))348second_response = chat.sample()349350print(second_response)351352# The response ID that can be used to continue the conversation later353354print(second_response.id)355```356357```python customLanguage="pythonOpenAISDK"358# Previous steps359import os360import httpx361from openai import OpenAI362363client = OpenAI(364 api_key="<YOUR_XAI_API_KEY_HERE>",365 base_url="https://api.x.ai/v1",366 timeout=httpx.Timeout(3600.0), # Override default timeout with longer timeout for reasoning models367)368369response = client.responses.create(370 model="grok-4.6",371 input=[372 {"role": "system", "content": "You are Grok, an AI agent built to answer helpful questions."},373 {"role": "user", "content": "How big is the universe?"},374 ],375)376377print(response)378379# The response ID that can be used to continue the conversation later380381print(response.id)382383# New steps384385second_response = client.responses.create(386 model="grok-4.6",387 previous_response_id=response.id,388 input=[389 {"role": "user", "content": "How do stars form?"},390 ],391)392393print(second_response)394395# The response ID that can be used to continue the conversation later396397print(second_response.id)398```399400```javascript customLanguage="javascriptWithoutSDK"401// Previous steps402import OpenAI from "openai";403404const client = new OpenAI({405 apiKey: "<api key>",406 baseURL: "https://api.x.ai/v1",407 timeout: 360000, // Override default timeout with longer timeout for reasoning models408});409410const response = await client.responses.create({411 model: "grok-4.6",412 input: [413 {414 role: "system",415 content: "You are Grok, an AI agent built to answer helpful questions."416 },417 {418 role: "user",419 content: "How big is the universe?"420 },421 ],422});423424console.log(response);425426// The response ID that can be used to recall the conversation later427console.log(response.id);428429const secondResponse = await client.responses.create({430 model: "grok-4.6",431 previous_response_id: response.id,432 input: [433 {"role": "user", "content": "How do stars form?"},434 ],435});436437console.log(secondResponse);438439// The response ID that can be used to recall the conversation later440console.log(secondResponse.id);441```442443```javascript customLanguage="javascriptAISDK"444import { xai } from '@ai-sdk/xai';445import { generateText } from 'ai';446447// First request448const result = await generateText({449 model: xai.responses('grok-4.6'),450 system: "You are Grok, an AI agent built to answer helpful questions.",451 prompt: "How big is the universe?",452});453454console.log(result.text);455456// Get the response ID from the response object457const responseId = result.response.id;458459// Continue the conversation using previousResponseId460const { text: secondResponse } = await generateText({461 model: xai.responses('grok-4.6'),462 prompt: "How do stars form?",463 providerOptions: {464 xai: {465 previousResponseId: responseId,466 },467 },468});469470console.log(secondResponse);471```472473```bash474curl https://api.x.ai/v1/responses \475 -H "Content-Type: application/json" \476 -H "Authorization: Bearer $XAI_API_KEY" \477 -m 3600 \478 -d '{479 "model": "grok-4.6",480 "previous_response_id": "The previous response ID",481 "input": [482 {483 "role": "user",484 "content": "How do stars form?"485 }486 ]487}'488```489490### Adding encrypted thinking content491492After returning the encrypted thinking content, you can also add it to a new response's input.493494> [!NOTE]495>496> Make sure to use a reasoning model when working with encrypted thinking content.497498```python customLanguage="pythonXAI"499import os500from xai_sdk import Client501from xai_sdk.chat import user, system502503client = Client(504 api_key=os.getenv("XAI_API_KEY"),505 management_api_key=os.getenv("XAI_MANAGEMENT_API_KEY"),506 timeout=3600,507)508509chat = client.chat.create(model="grok-4.6", store_messages=True, use_encrypted_content=True)510chat.append(system("You are Grok, an AI agent built to answer helpful questions."))511chat.append(user("How big is the universe?"))512response = chat.sample()513514print(response)515516# The response ID that can be used to continue the conversation later517518print(response.id)519520# New steps521522chat.append(response) ## Append the response and the SDK will automatically add the outputs from response to message history523524chat.append(user("How do stars form?"))525second_response = chat.sample()526527print(second_response)528529# The response ID that can be used to continue the conversation later530531print(second_response.id)532```533534```python customLanguage="pythonOpenAISDK"535# Previous steps536import os537import httpx538from openai import OpenAI539540client = OpenAI(541 api_key="<YOUR_XAI_API_KEY_HERE>",542 base_url="https://api.x.ai/v1",543 timeout=httpx.Timeout(3600.0), # Override default timeout with longer timeout for reasoning models544)545546response = client.responses.create(547 model="grok-4.6",548 input=[549 {"role": "system", "content": "You are Grok, an AI agent built to answer helpful questions."},550 {"role": "user", "content": "How big is the universe?"},551 ],552 include=["reasoning.encrypted_content"]553)554555print(response)556557# The response ID that can be used to continue the conversation later558559print(response.id)560561# New steps562563second_response = client.responses.create(564 model="grok-4.6",565 input=[566 *response.output, # Use response.output instead of the stored response567 {"role": "user", "content": "How do stars form?"},568 ],569)570571print(second_response)572573# The response ID that can be used to continue the conversation later574575print(second_response.id)576```577578```javascript customLanguage="javascriptWithoutSDK"579// Previous steps580import OpenAI from "openai";581582const client = new OpenAI({583 apiKey: "<api key>",584 baseURL: "https://api.x.ai/v1",585 timeout: 360000, // Override default timeout with longer timeout for reasoning models586});587588const response = await client.responses.create({589 model: "grok-4.6",590 input: [591 {592 role: "system",593 content: "You are Grok, an AI agent built to answer helpful questions."594 },595 {596 role: "user",597 content: "How big is the universe?"598 },599 ],600 include: ["reasoning.encrypted_content"],601});602603console.log(response);604605// The response ID that can be used to recall the conversation later606console.log(response.id);607608const secondResponse = await client.responses.create({609 model: "grok-4.6",610 input: [611 ...response.output, // Use response.output instead of the stored response612 {"role": "user", "content": "How do stars form?"},613 ],614});615616console.log(secondResponse);617618// The response ID that can be used to recall the conversation later619console.log(secondResponse.id);620```621622```javascript customLanguage="javascriptAISDK"623import { xai } from '@ai-sdk/xai';624import { generateText } from 'ai';625626// First request. Encrypted reasoning content is included automatically627// by the AI SDK as long as `store: false` is not set.628const result = await generateText({629 model: xai.responses('grok-4.6'),630 system: "You are Grok, an AI agent built to answer helpful questions.",631 prompt: "How big is the universe?",632});633634console.log(result.text);635636// Continue the conversation using previousResponseId637// The encrypted content is automatically included when using previousResponseId638const { text: secondResponse } = await generateText({639 model: xai.responses('grok-4.6'),640 prompt: "How do stars form?",641 providerOptions: {642 xai: {643 previousResponseId: result.response.id,644 },645 },646});647648console.log(secondResponse);649```650651```bash652curl https://api.x.ai/v1/responses \653 -H "Content-Type: application/json" \654 -H "Authorization: Bearer $XAI_API_KEY" \655 -m 3600 \656 -d '{657 "model": "grok-4.6",658 "input": [659 {660 "role": "system",661 "content": "You are Grok, an AI agent built to answer helpful questions."662 },663 {664 "role": "user",665 "content": "How big is the universe?"666 },667 {668 "id": "rs_51abe1aa-599b-80b6-57c8-dddc6263362f_us-east-1",669 "summary": [],670 "type": "reasoning",671 "status": "completed",672 "encrypted_content": "bvV88j99ILvgfHRTHCUSJtw+ISji6txJzPdZNbcSVuDk4OMG2Z9r5wOBBwjd3u3Hhm9XtpCWJO1YgTOlpgbn+g7DZX+pOagYYrCFUpQ19XkWz6Je8bHG9JcSDoGDqNgRbDbAUO8at6RCyqgPupJj5ArBDC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gPYiszc7R8Y5dF6QbW2mlKnXzVy6qRMHNeQqGhCEncyT5nPNSdK5QlUwLokAIg"673 },674 {675 "content": [676 {677 "type": "output_text",678 "text": "42\n\nThis is, of course, the iconic answer from Douglas Adams'\'' *The Hitchhiker'\''s Guide to the Galaxy*, where a supercomputer named Deep Thought spends 7.5 million years computing the \"Answer to the Ultimate Question of Life, the Universe, and Everything\"—only to reveal it'\''s 42. (The real challenge, it turns out, is figuring out what the actual *question* was.)\n\nIf you'\''re asking in a more literal or philosophical sense, the universe doesn'\''t have a single tidy answer—it'\''s full of mysteries like quantum mechanics, dark matter, and why cats knock things off tables. But 42? That'\''s as good a starting point as any. What'\''s your take on it?",679 "logprobs": null,680 "annotations": []681 }682 ],683 "id": "msg_c2f68a9b-87cd-4f85-a9e9-b6047213a3ce_us-east-1",684 "role": "assistant",685 "type": "message",686 "status": "completed"687 },688 {689 "role": "user",690 "content": "How do stars form?"691 }692 ],693 "include": [694 "reasoning.encrypted_content"695 ]696}'697```698699## Retrieving a previous model response700701If you have a previous response's ID, you can retrieve the content of the response.702703```python customLanguage="pythonXAI"704import os705from xai_sdk import Client706from xai_sdk.chat import user, system707708client = Client(709 api_key=os.getenv("XAI_API_KEY"),710 management_api_key=os.getenv("XAI_MANAGEMENT_API_KEY"),711 timeout=3600,712)713714response = client.chat.get_stored_completion("<The previous response's id>")715716print(response)717```718719```python customLanguage="pythonOpenAISDK"720import os721import httpx722from openai import OpenAI723724client = OpenAI(725 api_key="<YOUR_XAI_API_KEY_HERE>",726 base_url="https://api.x.ai/v1",727 timeout=httpx.Timeout(3600.0), # Override default timeout with longer timeout for reasoning models728)729730response = client.responses.retrieve("<The previous response's id>")731732print(response)733```734735```javascript customLanguage="javascriptOpenAISDK"736import OpenAI from "openai";737738const client = new OpenAI({739 apiKey: "<api key>",740 baseURL: "https://api.x.ai/v1",741 timeout: 360000, // Override default timeout with longer timeout for reasoning models742});743744const response = await client.responses.retrieve("<The previous response's id>");745746console.log(response);747```748749```javascript customLanguage="javascriptAISDK"750// Note: The Vercel AI SDK does not provide a method to retrieve previous responses.751// Use the OpenAI SDK as shown above for this functionality.752753import OpenAI from "openai";754755const client = new OpenAI({756 apiKey: "<api key>",757 baseURL: "https://api.x.ai/v1",758 timeout: 360000,759});760761const response = await client.responses.retrieve("<The previous response's id>");762763console.log(response);764```765766```bash767curl https://api.x.ai/v1/responses/{response_id} \768 -H "Content-Type: application/json" \769 -H "Authorization: Bearer $XAI_API_KEY" \770 -m 3600771```772773## Delete a model response774775If you no longer want to store the previous model response, you can delete it.776777```python customLanguage="pythonXAI"778import os779from xai_sdk import Client780from xai_sdk.chat import user, system781782client = Client(783 api_key=os.getenv("XAI_API_KEY"),784 management_api_key=os.getenv("XAI_MANAGEMENT_API_KEY"),785 timeout=3600,786)787788response = client.chat.delete_stored_completion("<The previous response's id>")789print(response)790```791792```python customLanguage="pythonOpenAISDK"793import os794import httpx795from openai import OpenAI796797client = OpenAI(798 api_key="<YOUR_XAI_API_KEY_HERE>",799 base_url="https://api.x.ai/v1",800 timeout=httpx.Timeout(3600.0), # Override default timeout with longer timeout for reasoning models801)802803response = client.responses.delete("<The previous response's id>")804805print(response)806```807808```javascript customLanguage="javascriptOpenAISDK"809import OpenAI from "openai";810811const client = new OpenAI({812 apiKey: "<api key>",813 baseURL: "https://api.x.ai/v1",814 timeout: 360000, // Override default timeout with longer timeout for reasoning models815});816817const response = await client.responses.delete("<The previous response's id>");818819console.log(response);820```821822```javascript customLanguage="javascriptAISDK"823// Note: The Vercel AI SDK does not provide a method to delete previous responses.824// Use the OpenAI SDK as shown above for this functionality.825826import OpenAI from "openai";827828const client = new OpenAI({829 apiKey: "<api key>",830 baseURL: "https://api.x.ai/v1",831 timeout: 360000,832});833834const response = await client.responses.delete("<The previous response's id>");835836console.log(response);837```838839```bash840curl -X DELETE https://api.x.ai/v1/responses/{response_id} \841 -H "Content-Type: application/json" \842 -H "Authorization: Bearer $XAI_API_KEY" \843 -m 3600844```845