Skip to content
Docs

AI SDK with AI Gateway

The AI SDK is the recommended way to build AI-powered TypeScript applications with AI Gateway. Pass a model string like 'anthropic/claude-sonnet-5' directly to AI SDK functions and requests route through AI Gateway automatically.

See the AI SDK getting-started guide for runtime and package setup.

Install the ai package:

Terminal
npm install ai@latest
Terminal
yarn add ai@latest
Terminal
pnpm add ai@latest
Terminal
bun add ai@latest

See the generateText reference for options and return values.

Generate text by passing a plain string model ID. AI Gateway resolves the provider and routes the request automatically.

index.ts
import { generateText } from 'ai';
 
const { text } = await generateText({
  model: 'anthropic/claude-sonnet-5',
  prompt: 'Explain quantum computing in one paragraph.',
});
 
console.log(text);

See the streamText reference for stream events and response helpers.

Stream responses token-by-token for real-time output:

stream.ts
import { streamText } from 'ai';
 
const result = streamText({
  model: 'openai/gpt-6-astra',
  prompt: 'Write a short story about a robot discovering music.',
});
 
for await (const textPart of result.textStream) {
  process.stdout.write(textPart);
}

See the AI SDK structured-output guide for schemas, output types, and validation.

Generate type-safe structured data with generateText and Output.object and a Zod schema:

structured.ts
import { generateText, Output } from 'ai';
import { z } from 'zod';
 
const { output } = await generateText({
  model: 'anthropic/claude-sonnet-5',
  output: Output.object({ schema: z.object({
    name: z.string(),
    age: z.number(),
    city: z.string(),
  }) }),
  prompt: 'Extract: John is 30 years old and lives in NYC.',
});
 
console.log(output); // { name: 'John', age: 30, city: 'NYC' }

See the AI SDK tool-calling guide for execution, tool results, and multi-step calls.

Define tools that models can invoke to interact with external systems. Describe each tool's input with inputSchema:

tools.ts
import { generateText, isStepCount, tool } from 'ai';
import { z } from 'zod';
 
const { text } = await generateText({
  model: 'anthropic/claude-sonnet-5',
  stopWhen: isStepCount(5),
  tools: {
    getWeather: tool({
      description: 'Get the current weather for a location',
      inputSchema: z.object({
        location: z.string().describe('City name, e.g. San Francisco'),
      }),
      execute: async ({ location }) => ({
        location,
        temperature: 72,
        condition: 'sunny',
      }),
    }),
  },
  prompt: "What's the weather in Tokyo?",
});
 
console.log(text);

stopWhen is what lets the model answer in words. Without it the request stops as soon as the tool runs, finishing with finishReason: 'tool-calls' and an empty text. The toolResults field contains the tool result, but the model has not generated a text response from it.

See the AI SDK reasoning guide for reading reasoning output and configuring supported models.

Reasoning models think before answering. On AI SDK 7, set the top-level reasoning option and the SDK translates it to each provider's native API, so the same code works across Anthropic, OpenAI, and Google:

reasoning.ts
import { generateText } from 'ai';
 
const result = await generateText({
  model: 'anthropic/claude-sonnet-5',
  prompt: 'A bat and ball cost $1.10. The bat costs $1 more than the ball. How much is the ball?',
  reasoning: 'high',
});
 
console.log(result.reasoningText);
console.log(result.text);
reasoning.ts
import { generateText } from 'ai';
 
const result = await generateText({
  model: 'anthropic/claude-sonnet-5',
  prompt: 'A bat and ball cost $1.10. The bat costs $1 more than the ball. How much is the ball?',
  providerOptions: {
    anthropic: { thinking: { type: 'adaptive' } },
  },
});
 
console.log(result.reasoningText);
console.log(result.text);

On AI SDK 6 the top-level reasoning option is silently ignored: the request succeeds, but no thinking happens and reasoningText is empty. There is no error to catch. Use providerOptions on 6, or upgrade to 7.

For per-provider configuration and the full effort-level reference, see Reasoning.

See Inputs & Tools for complete vision, PDF, audio, and video examples across API formats.

See the AI SDK file-part guide for bytes, data URLs, remote URLs, and media types.

Swap a message's plain string content for an array of parts. A file part carries the bytes and a mediaType telling the model how to read them, so the same shape covers images and documents:

vision.ts
import fs from 'node:fs';
import { generateText } from 'ai';
 
const { text } = await generateText({
  model: 'anthropic/claude-opus-5',
  messages: [
    {
      role: 'user',
      content: [
        { type: 'text', text: 'Describe this image in one sentence.' },
        {
          type: 'file',
          data: fs.readFileSync('./diagram.png'),
          mediaType: 'image/png',
        },
      ],
    },
  ],
});
 
console.log(text);

data takes a Buffer, a Uint8Array, a base64 string, or a URL. Point mediaType at the document type to send a PDF instead:

({
  type: 'file',
  data: fs.readFileSync('./report.pdf'),
  mediaType: 'application/pdf',
});

Older examples use a { type: 'image', image } part. That part still works but is deprecated in AI SDK 7, which warns at runtime and asks for a file part with an image/* media type. The file form shown above works on both 7 and 6.

Whether a given model accepts images or PDFs is a per-model question. Check the model list before sending an attachment.

The examples on this page use AI SDK 7, except tabs explicitly labeled AI SDK 6. AI Gateway supports both versions, but some client APIs differ:

FeatureAI SDK 6AI SDK 7
System instructionssysteminstructions
Tool-loop stop conditionstepCountIsisStepCount
Completion callbacksonFinish, onStepFinishonEnd, onStepEnd
Telemetryexperimental_telemetrytelemetry
Top-level reasoningNot supportedSupported
Full event streamresult.fullStreamresult.stream
Image generationgenerateImage or its experimental aliasgenerateImage

AI SDK 7 requires Node.js 22 or later and ESM. Check your installed version with pnpm list ai. See the AI SDK 7 migration guide before upgrading. The AI SDK for Python beta uses a separate package and API.

See the AI SDK AI Gateway provider reference for API keys, OIDC, and custom provider instances.

The AI SDK uses the AI_GATEWAY_API_KEY environment variable by default. Set it in your .env.local file:

.env.local
AI_GATEWAY_API_KEY=your_ai_gateway_api_key

On Vercel deployments, you can also authenticate with OIDC tokens for keyless authentication.

See Authentication for more details.

Start with an AI SDK template to build a chatbot or route form submissions with Jev:

Last updated September 21, 2026

Was this helpful?

supported.