Quickstart
This quickstart guide will walk you through building your first AI application using the AI SDK. You’ll learn how to generate text, stream responses, and create structured outputs.Prerequisites
Before you begin, make sure you have:- Node.js 18+ installed
- An API key from Vercel AI Gateway or a direct provider like OpenAI
Setup
Generate text
Let’s start with the most basic example - generating text from a prompt. Create a file calledgenerate-text.ts:
What’s happening here?
generateText: The core function for generating text completionsmodel: Specifies which AI model to use (GPT-4o in this case)prompt: The input text that guides the model’s responseresult.text: The generated text responseresult.usage: Token usage information (prompt tokens, completion tokens, total tokens)result.finishReason: Why the model stopped generating (e.g., “stop”, “length”)
Stream text
For longer responses, streaming provides a better user experience by showing results as they’re generated. Create a file calledstream-text.ts:
Key differences
streamText: Returns a stream instead of waiting for the complete responseresult.textStream: An async iterable that yields text chunks as they arrive- Properties like
usageandfinishReasonare promises that resolve when streaming completes
Generate structured data
Generate type-safe structured outputs using Zod schemas:Why structured outputs?
- Type safety: Full TypeScript support with schema validation
- Reliability: The model is constrained to follow your schema
- Integration: Easy to integrate with databases and APIs
Use tools
Tools allow AI models to perform actions and retrieve real-time information:How tools work
- You define tools with input schemas and execute functions
- The model decides when to call tools based on the prompt
- The SDK automatically executes the tools and sends results back to the model
- The model uses the tool results to generate its final response
Using Vercel AI Gateway
If you prefer using the Vercel AI Gateway instead of direct provider packages:.env file has:
Next steps
Now that you’ve learned the basics, explore more advanced features:Build a chat interface
Use React, Vue, or Svelte hooks to build chat UIs
Create agents
Build autonomous agents with tools and multi-step reasoning
Explore providers
Learn about all available AI providers and models
API reference
Dive into the complete API documentation
Example projects
Check out complete example applications:Get help
If you run into issues:- Check the troubleshooting guide
- Ask questions in the Vercel Community
- Review API documentation