PatternsAgent Patterns

Agent Routing Pattern

Loading pattern preview

Loading preview…

Problems solved

Technical challenges this implementation handles out of the box.
  • 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

Products and workflows this pattern is designed to support.
  • 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. 1

    Install from the preview toolbar

    Shadcn Icon

    Copy the install command above and run it in your project — Pro patterns include a short-lived token.

  2. 2

    Add environment variables to .env.local

    OPENAI_API_KEY, PERPLEXITY_API_KEY, DEEPSEEK_API_KEY

  3. 3

    Point client fetches at your API routes

    Replace preview `/view/.../api/...` paths with `/api/...` in the installed files.

  4. 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. 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

generateObjectstreamTextconvertToModelMessagesAgenttoolstepCountIs

Providers

OpenAIPerplexitydeepseek

Files added

4 files
  • app/ai-agents-routing/page.tsx
  • app/ai-agents-routing/layout.tsx
  • app/api/routing-agent/route.ts
  • lib/ai-routing-types.ts

Dependencies

9 total

npm packages

6
AISDKlucide-reactNext.jsReactsonnerZod

Shadcn IconRegistry components

3
badgeemptyscroll-area

Critical files

  • routing-agent/route.ts

    Core 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.ts

    Defines 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