The Vercel AI SDK provides a simple and unified API for interacting with OpenAI-compatible language models. This guide explains how to integrate the SDK with OpenAI or any OpenAI-compatible API endpoint for text generation, streaming, image generation, and multimodal (vision) capabilities.
Prerequisites
Before you begin, ensure you have:
- Node.js 18+
- An OpenAI-compatible API endpoint
- An API key
- npm or another package manager
Installation
Install the required packages:
Package Overview
Environment Variables
Create a .env file:
BASE_URL is optional when using the official OpenAI API. Specify it only when connecting to an OpenAI-compatible provider or proxy.
Client Configuration
Create a reusable client configuration.
Next Steps
To explore individual capabilities with code examples, check the following sections in the sidebar:
Best Practices
- Store API keys in environment variables.
- Reuse a single OpenAI client instance throughout your application.
- Use streaming for chat interfaces to improve user experience.
- Validate generation parameters before making requests.
- Handle API errors gracefully with retry or fallback logic.
- Use system prompts to define consistent assistant behavior.
- Keep prompts concise and specific for better model responses.
Supported Features
Next Steps
You are now ready to build applications using the Vercel AI SDK with any OpenAI-compatible endpoint. Explore additional SDK capabilities such as structured outputs, tool calling, agents, and chat history management as your application grows.