Skip to main content

Installation

The AI SDK is available as npm packages. You can install the core SDK and optional provider packages based on your needs.

Requirements

Before you begin, ensure you have:
  • Node.js: Version 18 or higher
  • Package manager: npm, pnpm, yarn, or bun

Install the core SDK

The ai package contains the core SDK functionality including generateText, streamText, and other core APIs:
npm install ai

Choose a provider

You have two options for accessing AI models: The Vercel AI Gateway provides access to hundreds of models from different providers with a single API key. The gateway provider is included in the ai package by default.
1

Get an API key

2

Add to environment

Create a .env.local file in your project root:
touch .env.local
Add your API key:
AI_GATEWAY_API_KEY=your_api_key_here
3

Start using models

You can now access any model using a simple string:
import { generateText } from 'ai';

const { text } = await generateText({
  model: 'openai/gpt-4o',
  prompt: 'What is an agent?',
});

Option 2: Direct provider packages

Install specific provider packages to connect directly to AI services:
npm install @ai-sdk/openai
Then configure your provider credentials:
OPENAI_API_KEY=your_openai_key_here
ANTHROPIC_API_KEY=your_anthropic_key_here
Use the provider in your code:
import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { anthropic } from '@ai-sdk/anthropic';

// OpenAI
const result1 = await generateText({
  model: openai('gpt-4o'),
  prompt: 'What is an agent?',
});

// Anthropic
const result2 = await generateText({
  model: anthropic('claude-sonnet-4-5'),
  prompt: 'What is an agent?',
});

Framework-specific packages

If you’re building chat or generative UI applications, install the framework integration for your UI library:
npm install @ai-sdk/react
These packages provide hooks and utilities for building chat interfaces:
import { useChat } from '@ai-sdk/react';

export default function ChatComponent() {
  const { messages, input, handleInputChange, handleSubmit } = useChat();
  
  // Build your chat UI
}

Additional packages

Depending on your use case, you may need additional packages:

Schema validation

For structured outputs and tool definitions, install Zod:
npm install zod

Next.js RSC (React Server Components)

For Next.js applications using React Server Components:
npm install ai @ai-sdk/react

Verify installation

Create a simple test file to verify your installation:
import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';

const result = await generateText({
  model: openai('gpt-4o'),
  prompt: 'Say hello!',
});

console.log(result.text);
Run the file:
node --loader ts-node/esm test.ts
Make sure you’ve set up your API keys in the .env.local file before running the test.

TypeScript configuration

The AI SDK is written in TypeScript and provides full type safety. Ensure your tsconfig.json includes:
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "lib": ["ES2020"],
    "moduleResolution": "bundler",
    "strict": true
  }
}

Next steps

Quickstart

Build your first AI application

Providers

Learn about available AI providers

AI SDK Core

Explore core functions and APIs

AI SDK UI

Build chat and generative interfaces