Prompt Chaining and Workflows
When we built an Agent with Tool Calling, Claude made the decisions dynamically. But sometimes, you don't want the AI to be creative. You want a rigid, deterministic, multi-step pipeline.
This is called Prompt Chaining.
What is Prompt Chaining?
Prompt chaining is taking the output of one Claude API call, and feeding it directly into the input of a second Claude API call.
By breaking a massive, complex task into a chain of smaller, focused tasks, you drastically increase the quality and reliability of the final output.
The Writing Pipeline
Imagine you want Claude to write a 1,000-word blog post. If you just send one prompt: "Write a blog post about AI," the output will likely be generic, rambling, and poorly structured.
Instead, you chain it:
Step 1: The Outliner
const outlineResponse = await anthropic.messages.create({
model: 'claude-3-haiku-20240307', // Cheap model
system: "You are a master strategist. Generate a 5-point outline for a blog post based on the user's topic. Only output the outline.",
messages: [{ role: 'user', content: 'Topic: AI in Healthcare' }]
});
const outline = outlineResponse.content[0].text;
Step 2: The Writer
const articleResponse = await anthropic.messages.create({
model: 'claude-3-sonnet-20240229', // Smarter model
system: "You are an expert copywriter. Write a full blog post strictly following the provided outline.",
messages: [
{
role: 'user',
content: `<outline>${outline}</outline>\n\nWrite the post.`
}
]
});
const draft = articleResponse.content[0].text;
Step 3: The Editor
const finalResponse = await anthropic.messages.create({
model: 'claude-3-opus-20240229', // Smartest model
system: "You are a ruthless editor. Read the draft. Fix typos, improve flow, and ensure it sounds professional.",
messages: [
{
role: 'user',
content: `<draft>${draft}</draft>\n\nEdit this draft.`
}
]
});
const finalPost = finalResponse.content[0].text;
Why Chain Prompts?
- Quality: Breaking complex reasoning into discrete steps stops the LLM from getting confused. The Writer doesn't have to think about strategy; it just writes based on the Outliner's plan.
- Cost Optimization: Notice how we used different models! The Outliner used Haiku (cheap). The Editor used Opus (expensive). If you did the whole task in one prompt, you'd have to use Opus for everything, wasting money.
- Debugging: If the final blog post is bad, you can look at the output of Step 1 and Step 2. Was the outline bad? Was the draft bad? Chaining makes it easy to isolate failures.