Prompt chaining is the most basic way to build something bigger than a single call: split the task into steps and pipe each step's output into the next. Outline, then draft. Draft, then critique, then revise. The path is fixed and you designed it, which makes chaining a workflow, not an agent.
The pattern
Each step gets a focused prompt and a focused context, instead of asking one prompt to do everything at once:
import { generateText } from 'ai'
import { openai } from '@ai-sdk/openai'
const model = openai('gpt-5-mini')
const outline = await generateText({
model,
prompt: 'Give a 3-bullet outline for a short note about RAG.',
})
const draft = await generateText({
model,
prompt: 'Write one tight paragraph from this outline: ' + outline.text,
})The outline becomes part of the second prompt. Two small, well-scoped calls tend to beat one sprawling instruction.
Why break it up
- Each step is easier to get right. A focused prompt with a focused context has less room to wander.
- Each step is easier to debug. When the output is wrong you can see exactly which stage produced the problem.
- You can mix models. Use a cheap model for the easy steps and an expensive one only where it earns its keep.
The cost is rigidity: a chain always runs the same steps in the same order. When the right sequence depends on the input, add a routing step, and when it genuinely cannot be known in advance, reach for 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 →Routing
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.
Read definition →