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

bash

2. Set Up Your Environment

Add your API keys and provider configuration to your .dev.vars file:

bash

3. Create Your First Agent

typescript

Advanced Configuration

Dynamic Model Selection

You can implement dynamic model selection based on message content or user preferences:

typescript

Streaming Responses

Basic Streaming with Platform Services

typescript

Advanced Streaming with Tools

typescript

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:

json

Configuration Options:

  • url: Use for standard public MCP servers with the mcp:// protocol
  • source: Use for GitHub-based MCPs, especially Cloudflare Worker MCPs with format github:owner/repo
typescript

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:

typescript

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:

typescript

Environment Variables Configuration

Add these environment variables to your wrangler.toml:

toml

Multiple Agent Types

You can create different agent classes for different use cases:

typescript

Platform Service Integration

Document Processing Agent

typescript

Video Analysis Agent

typescript

Best Practices

Error Handling and Resilience

typescript

Performance Optimization

typescript

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.