xAI
Use xAI with Better Agent by creating the provider and choosing a model.
Quick Start
import { betterAgent, defineAgent } from "@better-agent/core";
import { createXAI } from "@better-agent/providers/xai";
const xai = createXAI({
apiKey: process.env.XAI_API_KEY,
});
const assistant = defineAgent({
name: "assistant",
model: xai.text("grok-4"),
instruction: "You are a concise assistant. Keep replies short and natural.",
});
const app = betterAgent({
agents: [assistant],
});Model Helpers
Use the helper that matches the kind of model you want:
| Helper | Use it for |
|---|---|
xai.text(...) | Text and multimodal response models |
xai.image(...) | Image generation and editing |
xai.model(...) | Any xAI model id when you already know the exact model |
xAI Model Options
const assistant = defineAgent({
name: "assistant",
model: xai.text("grok-4"),
defaultModelOptions: {
reasoningEffort: "low",
},
});Hosted Tools
const assistant = defineAgent({
name: "assistant",
model: xai.text("grok-4"),
tools: [
xai.tools.webSearch({}),
xai.tools.xSearch({}),
],
});Files
const upload = await xai.files.upload({
file: pdfBuffer,
filename: "report.pdf",
mimeType: "application/pdf",
});
const input = [
{
type: "message",
role: "user",
content: [
{ type: "text", text: "Summarize this PDF." },
{
type: "file",
source: {
kind: "provider-file",
ref: { provider: "xai", id: upload.value.id },
},
},
],
},
];