Vercel AI SDK
Vercel AI SDK
The Vercel AI SDK integration provides a streamlined way to build AI agents that leverage Vercel's AI SDK for text generation while integrating seamlessly with our platform services. This guide shows you how to set up models, process messages via streaming, and configure workers for agent access.
Quick Start
1. Install Dependencies
2. Set Up Your Environment
Add your API keys and provider configuration to your .dev.vars file:
3. Create Your First Agent
Advanced Configuration
Dynamic Model Selection
You can implement dynamic model selection based on message content or user preferences:
Streaming Responses
Basic Streaming with Platform Services
Advanced Streaming with Tools
MCP Integration Example
The framework provides built-in MCP integration through the ToolboxService. Configure your MCP servers in an mcp.json file using the standard url format for public MCPs and source format for GitHub-based MCPs:
Configuration Options:
url: Use for standard public MCP servers with themcp://protocolsource: Use for GitHub-based MCPs, especially Cloudflare Worker MCPs with formatgithub:owner/repo
Durable Objects for Session Management
The framework leverages Cloudflare Durable Objects for persistent session management. Each agent instance maintains its own state and can handle multiple concurrent conversations:
The Durable Object pattern ensures:
- Session Isolation: Each conversation maintains separate state
- Automatic Scaling: Sessions are created on-demand
- Persistence: Conversation history and context are preserved
- Global Distribution: Sessions can be accessed from any region
Worker Setup
Complete Worker with Durable Object Routing
The modern pattern uses Hono for routing with automatic session management through Durable Objects:
Environment Variables Configuration
Add these environment variables to your wrangler.toml:
Multiple Agent Types
You can create different agent classes for different use cases:
Platform Service Integration
Document Processing Agent
Video Analysis Agent
Best Practices
Error Handling and Resilience
Performance Optimization
Next Steps
- Session Router: Learn how to implement session routing for multi-user conversations
- Tool Integration: Add custom tools for specific business logic
- Platform Services: Explore all available platform services (Storage, Queues, Analytics, etc.)
- Advanced Patterns: Implement multi-agent workflows and complex orchestration
The Vercel AI SDK integration provides a powerful foundation for building intelligent agents that can leverage the full capabilities of our platform while maintaining excellent performance and user experience.