AIF-52Recommended
Use a fixed workflow when the steps are known
Build a fixed sequence of model calls and code when the steps of a task are known in advance. Use an agent loop, where the model picks the next tool, only when they are not.
How to do it
Write the task as steps. Some tasks always run the same steps, such as "classify the email, look up the order, draft a reply". Code those in order and call the model only inside steps that need it. A fixed workflow is cheaper, faster and easier to test than a loop. Keep the agent loop for open tasks, such as research across many sources. Give it the limits in this chapter and in limit cost, steps and loops.
Examples
TypeScript example
// A fixed workflow: the model works inside steps, code decides the order.
export async function handleSupportEmail(email: Email, user: User) {
const { intent } = await ai.generate({ promptId: 'classify-email', vars: { body: email.body }, schema: Intent, userId: user.id });
if (intent === 'return') {
const order = await orders.latestFor(user.id);
return ai.generate({ promptId: 'draft-return-reply', vars: { body: email.body, order: JSON.stringify(order) }, schema: Draft, userId: user.id });
}
return routeToPerson(email);
}
Python example
def handle_support_email(email: Email, user: User) -> Draft | None:
intent = ai.generate("classify-email", {"body": email.body}, Intent, user.id).intent
if intent == "return":
order = orders.latest_for(user.id)
return ai.generate("draft-return-reply", {"body": email.body, "order": order.model_dump_json()}, Draft, user.id)
return route_to_person(email)
How to check
A CI script lists every call to the agent runner in the code. Each caller has a decision note that lists its tasks and why fixed steps could not do them. The script fails on any caller without a note.
Related rules
- AIF-71 Stop every agent run at fixed limits set in code in AI features
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/ai-features/agents-in-products/workflow-before-agent">Agent Wranglers AI features playbook</a>. In code you write, add a comment next to it with the same link.