In the rapidly evolving landscape of artificial intelligence, agentic workflows empower developers to create sophisticated, autonomous systems that leverage large language models (LLMs) with enhanced capabilities like tool calling and task orchestration. These workflows provide structured approaches to break down complex tasks, route queries efficiently, parallelize operations, coordinate multi-step processes, and iteratively optimize outputs. This article explores five foundational agentic workflow patterns, complete with practical examples, to help developers design robust AI agents for real-world applications.
1. Prompt Chaining
When to Use:
- Structured multi-step tasks
- Where outputs of one step feed the next
Example: Generate a commit message and validate it
import { generateText } from "ai";
async function generateCommitMessage(diff) {
const raw = await generateText({
model: "gpt-4",
prompt: `Write a conventional commit message for this diff:\n${diff}`,
});
const valid = /^(feat|fix|docs|chore)/.test(raw);
if (!valid) {
return await generateText({
model: "gpt-4",
prompt: `Fix this commit message to follow conventional commit format:\n${raw}`,
});
}
return raw;
}
2. Routing
When to Use:
- Tasks vary by type or domain
- Specialized models or paths needed
Example: Classify and delegate user query
import { generateObject, generateText } from "ai";
import { z } from "zod";
const schema = z.object({
type: z.enum(["code", "creative", "search"]),
reason: z.string(),
});
const modelMap = {
code: "claude",
creative: "gpt-4",
search: "gemini",
};
async function routeQuery(query) {
const { type } = await generateObject({
model: "gpt-4",
schema,
prompt: `Classify the query into code, creative, or search:\n${query}`,
});
return await generateText({ model: modelMap[type], prompt: query });
}
3. Parallelization
When to Use:
- Multitasking subtasks simultaneously
- Independent analysis by multiple agents
Example: Review code for security, performance, and maintainability
import { generateObject, generateText } from "ai";
async function reviewCode(code) {
const tasks = [
{
label: "security",
prompt: `List security issues in this code:\n${code}`,
},
{ label: "performance", prompt: `Find performance bottlenecks:\n${code}` },
{
label: "maintainability",
prompt: `Comment on maintainability:\n${code}`,
},
];
const reviews = await Promise.all(
tasks.map((task) =>
generateText({ model: "gpt-4", prompt: task.prompt }).then((result) => ({
type: task.label,
result,
}))
)
);
const summary = await generateText({
model: "gpt-4",
prompt: `Summarize the following review results:
${JSON.stringify(reviews)}`,
});
return summary;
}
4. Orchestrator
When to Use:
- Controlled flow with dependency between steps
- Multiple types of tasks need coordination
Example: Implementing a feature with task-specific calls
async function implementFeature(goal) {
const plan = await generateObject({
model: "openai/gpt-3.5",
prompt: `Break down this feature into file tasks with change type:
${goal}`,
});
const fileChanges = await Promise.all(
plan.files.map((file) => {
const actionPrompt =
file.changeType === "create"
? `Create a new file ${file.path} with purpose: ${file.purpose}`
: file.changeType === "modify"
? `Improve existing file ${file.path}`
: `Remove unused code in ${file.path}`;
return generateText({ model: "gpt-4", prompt: actionPrompt });
})
);
return fileChanges;
}
5. Evaluator + Optimizer
When to Use:
- Iterative quality improvement
- Evaluation-driven refinement
Example: Improve code comments until quality is acceptable
async function improveComment(code) {
let comment = await generateText({
model: "gpt-4",
prompt: `Write a concise helpful comment for:
${code}`,
});
for (let attempt = 0; attempt < 3; attempt++) {
const evalResult = await generateObject({
model: "gpt-4",
schema: z.object({ clarity: z.number(), issues: z.string() }),
prompt: `Evaluate this comment: ${comment}`,
});
if (evalResult.clarity >= 8) break;
comment = await generateText({
model: "gpt-4",
prompt: `Improve this comment based on issues:
${evalResult.issues}`,
});
}
return comment;
}
Summary
| Workflow Type | Use Case |
| --------------- | ------------------------------- |
| Prompt Chaining | Structured, multi-step tasks |
| Routing | Handle diverse task types |
| Parallelization | Speed up subtasks |
| Orchestrator | Fine-grain control, multi-agent |
| Evaluator+Opt. | Iterative improvement |
Together, these patterns form the building blocks for robust, smart, autonomous agents.
Agents are basically llms with added functionalities (tools calling)
˚
i wanna paste it to my medium articke