xAI Integration Guide
What is xAI?
POST /v1/responses with tools: [{type: 'web_search'}, {type: 'x_search'}] — grounding as a request flag.
TypeScript goes through @ai-sdk/xai (there's no official JS SDK); Python gets the official gRPC-based xai-sdk where client.chat.create(tools=[web_search()]) returns response.citations and per-tool usage counts. Scope searches with allowed_domains / excluded_domains (max 5, mutually exclusive). Reasoning models can think for minutes — xAI's own examples set 3600s timeouts, so stream everything user-facing. For codeAmani, xAI is the real-time grounding tier next to Claude: point allowed_domains at CBK or Kenyan news outlets and you get cited East African answers without building a scraper.
Five xAI primitives
One OpenAI-compatible API; Live Search is the reason to reach for it.
██╗ ██╗ █████╗ ██╗
╚██╗██╔╝██╔══██╗██║
╚███╔╝ ███████║██║
██╔██╗ ██╔══██║██║
██╔╝ ██╗██║ ██║██║
╚═╝ ╚═╝╚═╝ ╚═╝╚═╝xAI Integration Guide
Focus: Grok models via the xAI API — real-time Live Search (web + X), 1M-token context, reasoning, structured outputs, and image generation, through
@ai-sdk/xai(TypeScript) or the officialxai-sdk(Python).
Overview
xAI serves the Grok model family at https://api.x.ai/v1. The flagship grok-4.3 is a reasoning model with a 1,000,000-token context window, text + image input, and access to xAI's signature capability: server-side Live Search tools (web_search, x_search) that let the model browse the web and X in real time and return inline citations. The API is OpenAI-compatible — the OpenAI SDK works with a base-URL swap — and xAI also ships an official Python SDK plus first-class support in the Vercel AI SDK.
Official Documentation
| Resource | URL |
|---|---|
| Quickstart | https://docs.x.ai/developers/quickstart |
| Models | https://docs.x.ai/developers/models/grok-4.3 |
| Web Search tool | https://docs.x.ai/developers/tools/web-search |
| Streaming | https://docs.x.ai/developers/model-capabilities/text/streaming |
| Console (keys, billing) | https://console.x.ai |
| Python SDK | https://github.com/xai-org/xai-sdk-python |
SDK Setup
TypeScript — Vercel AI SDK provider (recommended)
xAI has no official JS SDK; its docs use the AI SDK provider.
npm i @ai-sdk/xai aiimport { xai } from "@ai-sdk/xai"; // reads XAI_API_KEY from env
import { generateText } from "ai";
const { text } = await generateText({
model: xai("grok-4.3"),
prompt: "Summarize today's Kenyan mobile-money news.",
});Custom configuration:
import { createXai } from "@ai-sdk/xai";
const xai = createXai({ apiKey: process.env.XAI_API_KEY });TypeScript — OpenAI SDK (drop-in compatibility)
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.XAI_API_KEY,
baseURL: "https://api.x.ai/v1",
timeout: 360000, // reasoning models can think for a while
});Python — official xai-sdk
pip install xai-sdk # Python >= 3.10from xai_sdk import Client
from xai_sdk.chat import user
client = Client() # reads XAI_API_KEY from environment
chat = client.chat.create(model="grok-4.3")
chat.append(user("Hello, how are you?"))
response = chat.sample()
print(response.content)Models
| Model ID | Aliases | Modalities | Context | Notes |
|---|---|---|---|---|
grok-4.3 | grok-4.3-latest, grok-latest | text + image → text | 1M tokens | Flagship reasoning model |
grok-4.20-0309-reasoning | — | text + image → text | 1M tokens | Reasoning variant (non-reasoning variants exist) |
grok-build-0.1 | — | text → text | — | Function calling, structured outputs, reasoning |
grok-imagine-image-quality | — | text → image | — | Image generation |
Model lineup and pricing move fast — check https://docs.x.ai/developers/models and the console before hardcoding a model ID; prefer
grok-latestonly for experiments, pinned IDs in production.
Core Patterns
Chat Completion (OpenAI-compatible)
curl https://api.x.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $XAI_API_KEY" \
-d '{
"model": "grok-4.3",
"messages": [
{"role": "system", "content": "You are Grok, a helpful and truthful AI assistant built by xAI."},
{"role": "user", "content": "What is prompt caching?"}
]
}'Streaming
Set a long timeout — reasoning models can run for minutes before the first token.
const stream = await client.chat.completions.create({
model: "grok-4.3",
messages: [{ role: "user", content: prompt }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}Live Search — web + X with citations
The killer feature: server-side search tools. Declare them in tools and the model plans, searches, browses, and cites — no tool-execution loop on your side. Inline citations are on by default when web_search is enabled.
curl https://api.x.ai/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $XAI_API_KEY" \
-d '{
"model": "grok-4.3",
"input": [{"role": "user", "content": "What is the latest update from xAI?"}],
"tools": [{"type": "web_search"}, {"type": "x_search"}]
}'from xai_sdk import Client
from xai_sdk.chat import user
from xai_sdk.tools import web_search, x_search
client = Client()
chat = client.chat.create(
model="grok-4.3",
tools=[web_search(), x_search()],
)
chat.append(user("What is the latest update from xAI?"))
response = chat.sample()
print(response.content)
print(response.citations) # source URLs
print(response.server_side_tool_usage) # per-tool call countsScope searches with domain filters (max 5 domains; allowed_domains and excluded_domains are mutually exclusive):
// AI SDK form
xai.tools.webSearch({ allowedDomains: ["centralbank.go.ke"] });# xai-sdk form
web_search(allowed_domains=["centralbank.go.ke"])Structured Outputs (AI SDK + Zod)
import { xai } from "@ai-sdk/xai";
import { generateText, Output } from "ai";
import { z } from "zod";
const InvoiceSchema = z.object({
vendor_name: z.string().describe("Name of the vendor"),
invoice_number: z.string().describe("Unique invoice identifier"),
total_amount: z.number().min(0).describe("Total amount due"),
});
const result = await generateText({
model: xai.responses("grok-4.3"),
output: Output.object({ schema: InvoiceSchema }),
prompt: "Extract the invoice fields from this email: ...",
});Prompt Caching — x-grok-conv-id
Requests carrying the same conversation ID route to the same server, maximizing cache hits. Check usage.prompt_tokens_details.cached_tokens to verify.
const response = await client.chat.completions.create(
{ model: "grok-4.3", messages },
{ headers: { "x-grok-conv-id": conversationId } },
);
console.log(response.usage?.prompt_tokens_details?.cached_tokens);Image Generation
import { xai } from "@ai-sdk/xai";
import { experimental_generateImage as generateImage } from "ai";
const { image } = await generateImage({
model: xai.image("grok-imagine-image-quality"),
prompt: "A boda boda rider at sunset in Nairobi",
});AI Routing: When to Use xAI
In codeAmani's routing policy, Anthropic Claude stays primary for complex reasoning and code gen. xAI earns its slot when the answer needs live web or X data:
| Task | Recommended |
|---|---|
| Real-time news / market / social sentiment | grok-4.3 + Live Search |
| "What's happening on X about …" | grok-4.3 + x_search |
| Complex reasoning, agents, code gen | Anthropic Claude |
| Bulk cheap inference | DeepSeek / Haiku tier |
Environment Variables
# Required — create at https://console.x.ai
XAI_API_KEY=xai-...codeAmani notes
- Secrets server-side only.
XAI_API_KEYlives in.env.local/ Vercel env vars / Hazina — never in client bundles. Call xAI from API routes or server actions. - Routing: xAI does not replace Anthropic as the primary provider — it is the real-time grounding tier. Reach for it when Live Search citations beat a stale training cutoff (news digests, price checks, social listening).
- AI Gateway: on Vercel,
@ai-sdk/xaialso works through the Vercel AI Gateway ("xai/grok-4.3"model strings) for unified observability and fallbacks across providers. - Kenya-targeted projects: Live Search with
allowed_domainsscoped to local sources (e.g. CBK, Kenyan news outlets) is a clean way to ground Swahili/English answers in East African context without building a scraper. Watch token spend — search results and reasoning tokens both bill. - Reasoning latency: grok-4.3 is a reasoning model; budget for multi-minute worst-case responses (set SDK timeouts ~360s+) and always stream in user-facing flows.
Troubleshooting
| Issue | Fix |
|---|---|
401 Unauthorized | Verify XAI_API_KEY (starts with xai-) and that it's set server-side |
| Request times out | Reasoning models think before answering — raise SDK timeout (docs use up to 3600s) |
allowed_domains rejected | Max 5 domains; cannot combine with excluded_domains in one request |
No citations in response | Citations require a search tool (web_search / x_search) in tools |
cached_tokens always 0 | Send a stable x-grok-conv-id header so requests hit the same cache |
| Model not found | Check current IDs at docs.x.ai/developers/models — lineup rotates quickly |