xAI and Grok API integration
xAI models and SDK capabilities change often, so verify endpoints, model identifiers, parameters, and feature support against current documentation before writing code. The local resources/auto/ directory holds the documentation index (resources/auto/llms.txt), one file per page it lists (named after the page path, such as resources/auto/developers-models.md for the API guides, build-* for Grok Build, and cookbook-* for the cookbook), and the REST API schema (resources/auto/openapi.json.md).
API endpoints and SDK setup
Default to the Responses API (/v1/responses) or the native xai-sdk (Python) for all new integrations. Server-side agentic tools (web_search, x_search, code execution, remote MCP), stateful conversations (previous_response_id), and encrypted reasoning (include: ["reasoning.encrypted_content"]) require the Responses API or xai-sdk.
Chat Completions (/v1/chat/completions, client.chat.completions.create(...)) is a legacy endpoint (function calling only, no server-side search tools or encrypted reasoning). Use it only when maintaining existing integrations; consult resources/auto/developers-model-capabilities-text-comparison.md and resources/auto/developers-model-capabilities-legacy-chat-completions.md when migrating.
- Base URL:
https://api.x.ai/v1 - Environment variable:
XAI_API_KEY - Supported SDKs:
- OpenAI SDK (TypeScript / JavaScript & Python):
openaipointed athttps://api.x.ai/v1, callingclient.responses.create(...). - xAI native SDK (Python):
xai-sdk(pip install xai-sdk,from xai_sdk import Client). - Vercel AI SDK (TypeScript / JavaScript):
@ai-sdk/xai(npm install ai @ai-sdk/xai) withxai.responses("<model-id>")(avoid legacyxai("<model-id>")).
- OpenAI SDK (TypeScript / JavaScript & Python):
OpenAI-compatible Responses API (TypeScript / JavaScript)
import OpenAI from "openai"
const client = new OpenAI({
apiKey: process.env.XAI_API_KEY,
baseURL: "https://api.x.ai/v1"
})
const response = await client.responses.create({
model: "<model-id>",
input: "Summarize the key changes in this diff."
})
console.log(response.output_text)OpenAI-compatible Responses API (Python)
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ.get("XAI_API_KEY"),
base_url="https://api.x.ai/v1",
)
response = client.responses.create(
model="<model-id>",
input="Summarize the key changes in this diff.",
)
print(response.output_text)Native xai-sdk (Python)
import os
from xai_sdk import Client
from xai_sdk.chat import user
client = Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(model="<model-id>")
chat.append(user("Summarize the key changes in this diff."))
print(chat.sample().content)Vercel AI SDK (@ai-sdk/xai, TypeScript / JavaScript)
import {xai} from "@ai-sdk/xai"
import {generateText} from "ai"
const {text} = await generateText({
model: xai.responses("<model-id>"),
prompt: "Summarize the key changes in this diff."
})
console.log(text)
Replace <model-id> with a current identifier verified in resources/auto/developers-models.md.
Documentation lookup
When configuring an xAI API integration, follow this lookup order:
- Read the guide page in
resources/auto/(for exampledevelopers-quickstart.md,developers-models.md,developers-model-capabilities-text-comparison.md,developers-tools-function-calling.md,developers-tools-web-search.md,developers-tools-x-search.md,developers-model-capabilities-text-structured-outputs.md, ordevelopers-model-capabilities-images-understanding.md). - Verify exact endpoint paths, parameter schemas, and request payloads in
resources/auto/openapi.json.md. - Take model identifiers from the current models page (
resources/auto/developers-models.md, orhttps://docs.x.ai/developers/modelswhen it may be stale), not from memory or examples, and confirm the selected model supports the intended feature (vision input, server-side search tools, function calling, reasoning). - Load
XAI_API_KEYfrom environment variables or a secret manager. - When a page is missing locally or a newly announced feature is absent, check the upstream index at
https://docs.x.ai/llms.txt. To fetch a page online, request its.mdURL without anAccept: text/markdownheader; that combination returns 404.
Core integration patterns
- Model selection: Verify current model strings and pricing and rate-limit tiers on the models, pricing, and rate-limits pages listed in the index.
- Server-side tools and function calling: Pass server-side tools (
{"type": "web_search"},{"type": "x_search"}in Responses API;web_search()/x_search()inxai-sdk;xai.tools.webSearch()in@ai-sdk/xai) alongside custom function tools as needed. - Reasoning and streaming: Use Responses API (
include: ["reasoning.encrypted_content"],stream: true) orxai-sdk(chat.stream()) for reasoning models, and configure generous HTTP timeouts for long reasoning turns. - Structured outputs: Use JSON schema response formats (
resources/auto/developers-model-capabilities-text-structured-outputs.md) when precise downstream parsing is required. - Vision and multimodal: Pass image URLs or base64 data to vision-supported Grok models using Responses API input blocks or
xai-sdkhelpers.
Completion criteria
An xAI API integration task is complete when:
- Target model identifiers, endpoints, and parameters have been validated against the current docs pages and
resources/auto/openapi.json.md. - New integrations use
/v1/responses(client.responses.create(...)orxai.responses(...)) orxai-sdk(using legacy/v1/chat/completionsonly when explicitly maintaining legacy code), targethttps://api.x.ai/v1, and loadXAI_API_KEYfrom the environment or a secret manager. - Error handling covers common API response codes (e.g., rate limits, context/token limits, invalid key) and sets appropriate timeouts for reasoning models.
- Code implementations have been verified against integration tests or mock executions.