Quickstart

Generate your first response with Chai AI SDK.

Plain text generation

The simplest entry point is generateText() — a single call to a model, with no tools or agent loop:

index.ts
import { generateText } from "chai-ai";
import { google } from "@chai-ai/google";

const { text } = await generateText({
  model: google("gemini-3.1-pro-preview"),
  messages: [{ role: "user", content: "What is love?" }],
});

console.log(text);

Swap @chai-ai/google for @chai-ai/openai and google("gemini-3.1-pro-preview") for openai("gpt-4o-mini") to run the same call against OpenAI instead — generateText() itself doesn't change.

Adding tools with an Agent

For anything beyond a single text response — tool calling, multi-step reasoning, streaming — use Agent:

index.ts
import { Agent, tool } from "chai-ai";
import { google } from "@chai-ai/google";
import { z } from "zod";

const calculator = tool({
  description: "Performs a single arithmetic operation on two numbers.",
  inputSchema: z.object({
    operation: z.enum(["add", "multiply"]),
    a: z.number(),
    b: z.number(),
  }),
  execute({ operation, a, b }) {
    return operation === "add" ? a + b : a * b;
  },
});

const agent = new Agent({
  name: "calculator-agent",
  instructions:
    "You are a helpful assistant. Always use the calculator tool for arithmetic instead of computing it yourself.",
  model: google("gemini-3.1-pro-preview"),
  tools: { calculator },
});

const result = await agent.generate({
  prompt: "What is 47 multiplied by 23?",
});

console.log(result.text);
console.log(result.steps);

Continue with the Agents guide for how the tool-calling loop works, or Streaming to consume the same run as live events instead of waiting for a final result.

On this page