xAI

Install

npm install @better-agent/xai

Agent model

import { defineAgent } from "@better-agent/core";
import { xai } from "@better-agent/xai";

export const supportAgent = defineAgent({
  name: "support",
  model: xai("grok-4"),
  instruction: "You help customers.",
});

Configure

import { createXai } from "@better-agent/xai";

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

Hosted tools

xAI hosted tools are available on xai.tools.

const agent = defineAgent({
  name: "researcher",
  model: xai("grok-4"),
  tools: [
    xai.tools.webSearch({}),
  ],
});

See Tools for local tools, client tools, approvals, MCP, and hosted provider tools.

Direct generation

Use generation models when a tool needs a focused model call without running an agent.

const text = xai.text("grok-4");

const rewrite = defineTool({
  name: "rewrite",
  description: "Rewrite text for a specific audience.",
  inputSchema: z.object({
    content: z.string(),
    audience: z.string(),
  }),
  execute: async ({ content, audience }) => {
    const result = await text.generate({
      input: `Rewrite this for ${audience}:\n\n${content}`,
    });

    return { rewritten: result.text };
  },
});

Other generation helpers:

const image = xai.image("grok-2-image");
const video = xai.video("grok-video");

Model types

xai("grok-4") is an agent model for defineAgent. Agent models need text for messages, tool decisions, and streaming. They can support more than text depending on the model.

xai.text(...), xai.image(...), and xai.video(...) are generation models for direct calls from app code or tools. They do not run the agent loop.

Provider options

Pass xAI options at run time with the xai provider key.

await app.agent("support").run({
  messages,
  providerOptions: {
    xai: {
      reasoningEffort: "medium",
    },
  },
});

Capabilities

FeatureSupport
Agent modelYes
Text generationYes
StreamingYes
Structured outputYes
Hosted toolsYes
EmbeddingsNo
ImagesYes
VideoYes
AudioNo

Source: built on @ai-sdk/xai.