Agent Routing Pattern
Agent Routing Pattern
Route user queries to specialized AI agents based on context and intent. Includes dynamic agent selection, load balancing, and fallback handling.
Problems solved
- Classify support queries into specialized agent categories
- Route each query to a tailored agent system prompt
- Select models by query complexity before streaming a reply
- Stream classification metadata to the client as custom data parts
- Fall back across OpenAI, Perplexity, and Deepseek providers
Use cases
- Customer support desks that route by intent
- Multi-department help centers
- Sales and billing inquiry triage
- Technical support chat with specialist agents
- Intent-based agent orchestration demos
Setup
Requirements, wiring steps, and what this pattern adds to your project.
Getting started
Pick how you want to pull this pattern in. Then wire env vars and routes the same way.
- 1
Install from the preview toolbar

Copy the install command above and run it in your project — Pro patterns include a short-lived token.
- 2
Add environment variables to .env.local
OPENAI_API_KEY, PERPLEXITY_API_KEY, DEEPSEEK_API_KEY
- 3
Point client fetches at your API routes
Replace preview `/view/.../api/...` paths with `/api/...` in the installed files.
- 4
Customize the agent and tool files
Adapt prompts, tools, and stop conditions for your product — Copy for AI in the toolbar helps seed that work.
- 5
Run your dev server and open the pattern route
Install dependencies if needed, then start the app and verify responses.
Environment variables
Capabilities
AI SDK APIs
generateObjectstreamTextconvertToModelMessagesAgenttoolstepCountIsProviders
Files added
4 filesapp/ai-agents-routing/page.tsxapp/ai-agents-routing/layout.tsxapp/api/routing-agent/route.tslib/ai-routing-types.ts
Dependencies
9 totalnpm packages
6
Registry components
Critical files
routing-agent/route.tsCore routing agent API route. Implements a 3-stage AI pipeline: (1) classifies user input via generateObject into support categories, (2) routes to specialized agent config with tailored system prompts, (3) selects model based on complexity. Uses createUIMessageStream with custom data parts for streaming classification metadata and agent info to the client.
lib/ai-routing-types.tsDefines the routing agent's type system: RoutingDataParts, RoutingMetadata, RoutingUIMessage, and agentConfigs map that maps 7 classification categories to agent configurations (task, icon, color, systemPrompt). Contains getAgentConfig() which resolves classification type to agent config.
Requirements
- Complexity
- Intermediate
- Setup time
- ~15 minutes
- Node
- 24+
- Memory
- 512MB









