Agent patterns

Routing

Also called: classify and dispatch

Routing classifies an input and sends it to the handler built for it. It keeps each path specialised and lets you send easy cases to a cheap model and hard cases to an expensive one, without any of the cost of a full agent.

James Phoenix
Understanding Data Updated July 3, 2026

Routing is the workflow pattern where you first decide what kind of thing this input is, then hand it to the handler built for that kind. Support queries go to billing, technical, or general. Simple questions go to a small model, gnarly ones to a big one. The path is chosen by a classification, not by open-ended reasoning, so like prompt chaining it is a workflow, not an agent.

Classify, then dispatch

The cleanest way to route is to force the classifier into a fixed set of options, then branch on the result:

TypeScript
import { generateText, generateObject } from 'ai'
import { openai } from '@ai-sdk/openai'
import { z } from 'zod'

const model = openai('gpt-5-mini')
const query = 'How do I reset my password?'

const { object } = await generateObject({
  model,
  schema: z.object({ route: z.enum(['billing', 'technical', 'general']) }),
  prompt: 'Classify this support query into billing, technical, or general: ' + query,
})

const answer = await generateText({
  model,
  prompt: 'You are the ' + object.route + ' specialist. Answer: ' + query,
})

The enum keeps the classifier honest: it has to pick one of your routes, not invent a new one.

Why route at all

  • Specialised beats general. A prompt written for one kind of query outperforms one prompt trying to cover every kind.
  • Spend where it matters. Send the cheap cases to a cheap model and reserve the expensive model for the hard ones.
  • It composes. Each route can itself be a chain, a tool-using step, or, where truly needed, an agent.
Note
Routing is where many production systems start. Most inbound work is heterogeneous, and a good router plus a handful of focused handlers covers the majority of it without ever needing autonomy.

Related terms

Engineering context for real systems?

Getting the right information into the window at the right time is most of the job. If you want that thinking applied to your product, that is what I do.

See how I can help