Multi-agent orchestration for AI SDK v5. Automatic handoffs, programmatic routing, and seamless coordination across any AI provider. Perfect for complex tasks requiring distinct expertise. Includes built-in memory system for persistent context.
npm install @ai-sdk-tools/agents @ai-sdk-tools/memory ai zod
import { Agent } from '@ai-sdk-tools/agents' import { DrizzleProvider } from '@ai-sdk-tools/memory' import { openai } from '@ai-sdk/openai' const mathAgent = new Agent({ name: 'Math Tutor', model: openai('gpt-4o'), instructions: 'Help with math problems', matchOn: ['math', 'calculate', /\d+/] }) const historyAgent = new Agent({ name: 'History Tutor', model: openai('gpt-4o'), instructions: 'Help with history', matchOn: ['history', 'war'] }) const orchestrator = new Agent({ name: 'Triage', model: openai('gpt-4o-mini'), instructions: 'Route to specialists', handoffs: [mathAgent, historyAgent], memory: { provider: new DrizzleProvider(db), workingMemory: { enabled: true, scope: 'user' }, history: { enabled: true, limit: 10 } } }) // Auto-routes and remembers context const result = await orchestrator.generate({ prompt: 'What is 15 * 23?' }) console.log(`Handled by: ${result.finalAgent}`) // "Handled by: Math Tutor"
Pattern-based routing with regex and string matching. Route requests instantly to the right specialist without LLM overhead.
Agents can transfer control while preserving full conversation context. Include handoff reasons and relevant data for smooth transitions.
Use different AI models for different tasks. GPT-4 for analysis, Claude for writing, Gemini for review - all in one workflow.
Pass typed context to agents for team/user-specific behavior. Dynamic instructions based on preferences, permissions, and state.
Built-in working memory and conversation history. Agents remember context across chats with flexible storage providers (InMemory, Drizzle, Upstash).
Input/output validation, content moderation, and tool permissions. Block, modify, or approve content before and after agent execution.
Triage → Technical Support → Billing
Route customer inquiries to specialized agents. Technical questions to engineering support, billing to finance, general to product.
Research → Writing → Editing → Publishing
Multi-stage content pipeline. Research agent gathers info, writer creates content, editor reviews, publisher formats and posts.
Planning → Implementation → Testing → Documentation
Specialized agents for each phase. Architect plans, coder implements, tester validates, documenter writes guides.
Collection → Processing → Visualization → Insights
Agent pipeline for data workflows. Collector fetches data, processor cleans, visualizer creates charts, analyst generates insights.
// One model handles everything import { streamText } from 'ai' import { openai } from '@ai-sdk/openai' export async function POST(req: Request) { const { messages } = await req.json() const result = streamText({ model: openai('gpt-4o'), messages, system: `You are a helpful assistant. Handle math questions, history questions, coding questions, and general inquiries. Be expert at everything!`, tools: { calculate, searchHistory, runCode, webSearch, // ... 20 more tools } }) return result.toDataStreamResponse() } // Issues: // - No specialization // - Tool overload // - Poor performance on specific domains // - Hard to maintain instructions
// Specialized agents for each domain import { Agent } from '@ai-sdk-tools/agents' import { openai } from '@ai-sdk/openai' const mathAgent = new Agent({ name: 'Math Expert', model: openai('gpt-4o'), instructions: 'Expert at math. Show work.', tools: { calculate }, matchOn: ['math', 'calculate', /\d+/] }) const historyAgent = new Agent({ name: 'History Expert', model: openai('gpt-4o'), instructions: 'Expert at history. Cite dates.', tools: { searchHistory }, matchOn: ['history', 'war', 'civilization'] }) const orchestrator = new Agent({ name: 'Router', model: openai('gpt-4o-mini'), instructions: 'Route to specialists', handoffs: [mathAgent, historyAgent] }) // Benefits: // - Focused expertise // - Relevant tools only // - Better performance // - Easy to maintain & extend
Multi-agent orchestration for AI SDK v5. Works with any AI provider.