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:
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.
Related terms
Agents vs. workflows
A workflow follows a path you designed in advance; an agent decides its own path at run time by calling tools in a loop toward a goal. Knowing which one you actually need is the first context-engineering decision.
Read definition →Prompt chaining
Prompt chaining breaks a task into a fixed sequence of steps, feeding each step’s output into the next. It is the simplest workflow pattern, and it beats one giant prompt whenever a task has natural stages.
Read definition →Tool use
Tool use lets a model do more than produce text: you expose named actions with typed inputs, and the model calls them to read data, run code, or reach the outside world. It is the bridge from talking to doing.
Read definition →