xAI (Grok)
Use xAI's Grok models through the OpenAI-compatible API with Anvia.
xAI exposes an OpenAI-compatible chat completions surface at https://api.x.ai/v1. In Anvia, configure OpenAIClient with that baseUrl, then pass Grok model ids to completionModel(...). xAI also exposes dedicated endpoints for image generation, video generation, and voice (realtime, TTS, STT).
Create the Client
import { AgentBuilder } from "@anvia/core";
import { OpenAIClient } from "@anvia/openai";
const client = new OpenAIClient({
baseUrl: "https://api.x.ai/v1",
apiKey: process.env.XAI_API_KEY,
});
const model = client.completionModel("grok-4.3");
const agent = new AgentBuilder("support", model)
.instructions("Answer support questions clearly.")
.build();
const response = await agent.prompt("Hello!").send();
console.log(response.output);baseUrl makes Anvia use the OpenAI-compatible chat completion adapter. The model id is the Grok id, not an Anvia-specific alias.
Get the Model List
xAI exposes a /v1/models endpoint that returns the model ids available to your key. Because the client was created with baseUrl, listModels() calls xAI's /models endpoint.
const models = await client.listModels();
console.table(
models.data.map((model) => ({
id: model.id,
name: model.name,
contextLength: model.contextLength,
})),
);Use the id field directly with completionModel(...).
Available Models
Sample ids and pricing per 1M tokens (captured June 15, 2026). The knowledge cutoff for Grok 3 and Grok 4 is November 2024.
| Model | Context | Input | Output |
|---|---|---|---|
grok-4.3 | 1,000,000 | $1.25 | $2.50 |
grok-4.20-0309-reasoning | 1,000,000 | $1.25 | $2.50 |
grok-4.20-0309-non-reasoning | 1,000,000 | $1.25 | $2.50 |
grok-4.20-multi-agent-0309 | 1,000,000 | $1.25 | $2.50 |
grok-build-0.1 | 256,000 | $1.00 | $2.00 |
grok-imagine-image | — | $0.02 / image | — |
grok-imagine-image-quality | — | $0.05 / image | — |
grok-imagine-video | — | $0.050 / sec | — |
grok-imagine-video-1.5-preview | — | $0.080 / sec | — |
Model alias rules:
<modelname>resolves to the latest stable version.<modelname>-latestresolves to the latest version, including previews.<modelname>-<date>pins a specific release and never updates.
Prices shown above were captured on June 15, 2026. Discounts, regional pricing, and available models change frequently, so always confirm the latest rates on x.ai before quoting or budgeting.
Notes
- xAI API keys are passed as bearer tokens in the
Authorizationheader. - Grok 4.3 and Grok 4.20 are the recommended chat and coding models. Image, video, and voice use dedicated ids above.
- Chat models accept
system,user, andassistantmessages in any order. There is no strict role ordering. logprobsandtop_logprobsare silently ignored ongrok-4.20and newer models.- Image inputs are limited to 20 MiB per image. Supported file types are
jpg,jpeg, andpng. There is no limit on the number of images per request, and text/image order is flexible. - Grok has no knowledge of current events beyond its training data. Enable server-side Web Search or X Search tools to incorporate realtime data.
- Realtime voice and TTS/STT use separate endpoints and pricing from chat completions. See the xAI voice API for details.
For current xAI API details, see the xAI models documentation and the xAI API reference.
