Model Capabilities

Generate Text

The Responses API is the preferred way of interacting with our models via API. It allows optional stateful interactions with our models, where previous input prompts, reasoning content, and model responses are saved and stored on SpaceXAI's servers. You can continue the interaction by appending new prompt 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.

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. If 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.

For Python, we also offer our xAI SDK 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.


Prerequisites

Create an API key on the xAI Console API Keys Page. Set your API key in your environment:

Bash

export XAI_API_KEY="your_api_key"

Creating a new model response

Start by creating a response:

import { xai } from '@ai-sdk/xai';
import { generateText } from 'ai';

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

console.log(text);

// The response ID can be used to continue the conversation
console.log(response.id);

Disable storing previous request/response on server

If you do not want to store your previous request/response on the server, you can set store: false on the request.

import os
import httpx
from openai import OpenAI

client = OpenAI(
    api_key="<YOUR_XAI_API_KEY_HERE>",
    base_url="https://api.x.ai/v1",
    timeout=httpx.Timeout(3600.0), # Override default timeout with longer timeout for reasoning models
)

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

print(response)

Returning encrypted thinking content

If 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.

Make sure to use a reasoning model when working with encrypted thinking content.

grok-4.7 always returns reasoning.encrypted_content on the Responses API, whether or not include lists it. See Encrypted reasoning content.

Modify the steps to create a chat client (xAI SDK) or change the request body as following:

import { xai } from '@ai-sdk/xai';
import { generateText } from 'ai';

// Encrypted reasoning content is included automatically by the AI SDK
// as long as `store: false` is not set. No extra configuration is needed.
const { text, reasoning } = await generateText({
  model: xai.responses('grok-4.7'),
  system: "You are Grok, an AI agent built to answer helpful questions.",
  prompt: "How big is the universe?",
});

console.log(text);
console.log(reasoning); // Contains encrypted reasoning content

See Adding encrypted thinking content on how to use the returned encrypted thinking content when making a new request.


Chaining the conversation

We now have the id of the first response. With Chat Completions API, we typically send a stateless new request with all the previous messages.

With Responses API, we can send the id of the previous response, and the new messages to append to it.

import { xai } from '@ai-sdk/xai';
import { generateText } from 'ai';

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

console.log(result.text);

// Get the response ID from the response object
const responseId = result.response.id;

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

console.log(secondResponse);

Adding encrypted thinking content

After returning the encrypted thinking content, you can also add it to a new response's input.

Make sure to use a reasoning model when working with encrypted thinking content.

import { xai } from '@ai-sdk/xai';
import { generateText } from 'ai';

// First request. Encrypted reasoning content is included automatically
// by the AI SDK as long as `store: false` is not set.
const result = await generateText({
  model: xai.responses('grok-4.7'),
  system: "You are Grok, an AI agent built to answer helpful questions.",
  prompt: "How big is the universe?",
});

console.log(result.text);

// Continue the conversation using previousResponseId
// The encrypted content is automatically included when using previousResponseId
const { text: secondResponse } = await generateText({
  model: xai.responses('grok-4.7'),
  prompt: "How do stars form?",
  providerOptions: {
    xai: {
      previousResponseId: result.response.id,
    },
  },
});

console.log(secondResponse);

Retrieving a previous model response

If you have a previous response's ID, you can retrieve the content of the response.

// Note: The Vercel AI SDK does not provide a method to retrieve previous responses.
// Use the OpenAI SDK as shown above for this functionality.

import OpenAI from "openai";

const client = new OpenAI({
    apiKey: "<api key>",
    baseURL: "https://api.x.ai/v1",
    timeout: 360000,
});

const response = await client.responses.retrieve("<The previous response's id>");

console.log(response);

Delete a model response

If you no longer want to store the previous model response, you can delete it.

// Note: The Vercel AI SDK does not provide a method to delete previous responses.
// Use the OpenAI SDK as shown above for this functionality.

import OpenAI from "openai";

const client = new OpenAI({
    apiKey: "<api key>",
    baseURL: "https://api.x.ai/v1",
    timeout: 360000,
});

const response = await client.responses.delete("<The previous response's id>");

console.log(response);

Last updated: September 29, 2026