← Back to dashboard
xaiaifresh

xAI Integration Guide

What is xAI?

The real model

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.

Text
██╗  ██╗ █████╗ ██╗
╚██╗██╔╝██╔══██╗██║
 ╚███╔╝ ███████║██║
 ██╔██╗ ██╔══██║██║
██╔╝ ██╗██║  ██║██║
╚═╝  ╚═╝╚═╝  ╚═╝╚═╝

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 official xai-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


SDK Setup

xAI has no official JS SDK; its docs use the AI SDK provider.

Bash
npm i @ai-sdk/xai ai
TypeScript
import { 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:

TypeScript
import { createXai } from "@ai-sdk/xai";

const xai = createXai({ apiKey: process.env.XAI_API_KEY });

TypeScript — OpenAI SDK (drop-in compatibility)

TypeScript
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

Bash
pip install xai-sdk   # Python >= 3.10
Python
from 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 IDAliasesModalitiesContextNotes
grok-4.3grok-4.3-latest, grok-latesttext + image → text1M tokensFlagship reasoning model
grok-4.20-0309-reasoningtext + image → text1M tokensReasoning variant (non-reasoning variants exist)
grok-build-0.1text → textFunction calling, structured outputs, reasoning
grok-imagine-image-qualitytext → imageImage generation

Model lineup and pricing move fast — check https://docs.x.ai/developers/models and the console before hardcoding a model ID; prefer grok-latest only for experiments, pinned IDs in production.


Core Patterns

Chat Completion (OpenAI-compatible)

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

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

Bash
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"}]
  }'
Python
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 counts

Scope searches with domain filters (max 5 domains; allowed_domains and excluded_domains are mutually exclusive):

TypeScript
// AI SDK form
xai.tools.webSearch({ allowedDomains: ["centralbank.go.ke"] });
Python
# xai-sdk form
web_search(allowed_domains=["centralbank.go.ke"])

Structured Outputs (AI SDK + Zod)

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

TypeScript
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

TypeScript
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:

TaskRecommended
Real-time news / market / social sentimentgrok-4.3 + Live Search
"What's happening on X about …"grok-4.3 + x_search
Complex reasoning, agents, code genAnthropic Claude
Bulk cheap inferenceDeepSeek / Haiku tier

Environment Variables

Bash
# Required — create at https://console.x.ai
XAI_API_KEY=xai-...

codeAmani notes

  • Secrets server-side only. XAI_API_KEY lives 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/xai also 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_domains scoped 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

IssueFix
401 UnauthorizedVerify XAI_API_KEY (starts with xai-) and that it's set server-side
Request times outReasoning models think before answering — raise SDK timeout (docs use up to 3600s)
allowed_domains rejectedMax 5 domains; cannot combine with excluded_domains in one request
No citations in responseCitations require a search tool (web_search / x_search) in tools
cached_tokens always 0Send a stable x-grok-conv-id header so requests hit the same cache
Model not foundCheck current IDs at docs.x.ai/developers/models — lineup rotates quickly