AutoGen Workflows
Workflows in AutoGen orchestrate how tasks flow through agents, tools, messages, and events. They provide the structure that turns a collection of intelligent components into a reliable, automatable, and observable system. This article is your practical, implementation‑focused guide to designing and executing workflows with AutoGen—covering sequential, conditional, event‑driven, iterative, human‑in‑the‑loop, and tool‑oriented patterns. You’ll learn how to manage state, control execution, handle errors, and build production‑grade automation.
What Are Workflows in AutoGen
A workflow is the defined sequence of steps that process a user request from start to finish. It determines:
- Which agents participate and in what order.
- How messages pass between them.
- When tools are invoked.
- What events trigger state changes.
- When the process is complete.
Workflows can be as simple as a single agent answering a question, or as complex as a multi‑agent team researching, fact‑checking, drafting, and reviewing a report—all driven by explicit control logic.
A minimal workflow example (one‑step agent):
response = await agent.on_messages([TextMessage(content="Hello", source="user")])
But real applications demand more. AutoGen’s workflow building blocks—GroupChat, HandoffMessage, TaskRunner, event handlers, and the runtime—allow you to compose these simple interactions into sophisticated pipelines.
Why Workflows Matter
- Predictable execution – You control exactly how and when agents collaborate, preventing chaotic conversations.
- Automation – Entire business processes (customer support, data enrichment, report generation) can run without human intervention.
- Maintainability – Well‑defined workflows are easy to modify and extend.
- Reusability – Patterns like “research → analyse → write → review” can be templated across projects.
- Reliability – Explicit termination conditions, retries, and error handling make workflows robust.
Without workflows, you have a bag of agents; with them, you have a coordinated team.
How AutoGen Workflows Work
At a high level, a workflow progresses through these stages:
User Request → Workflow Initialization → Agent Processing → Tool Execution → Event Handling → Response Generation
The runtime manages the conversation state, delivers messages, and dispatches tool calls. The workflow’s logic—who speaks next, when to stop, what to do on failure—is encoded in the choice of team type (e.g., RoundRobinGroupChat, SelectorGroupChat), handoff rules, termination conditions, and custom event handlers.
Core Workflow Concepts
| Concept | Purpose |
|---|---|
| Workflow | The overall orchestration logic that defines the sequence of operations |
| Agent | A participant that processes messages and performs tasks |
| Message | Typed data (text, tool request, handoff) that flows between agents |
| Event | Runtime signal (tool request, execution result, pause) that can modify the workflow |
| Tool | Pre‑defined external capability (API, function) the agent can invoke |
| Context | Shared message history and state that persists across steps |
Workflow Lifecycle
Every workflow passes through a standard lifecycle, regardless of its complexity:
- Workflow initialization – The team (group chat) is assembled, agents are registered, and initial state is created.
- Context creation – The user message is wrapped in a
TextMessageand placed in the conversation. - Message generation – An agent (or manager) processes the input and may produce new messages.
- Agent execution – The next speaker’s
on_messagesis called. The agent reasons, may request tools. - Tool invocation – The runtime intercepts tool requests, executes the tool, and returns results as messages.
- Event processing – Events like
ToolCallRequestEvent,ToolCallExecutionEventfire; custom handlers can pause, approve, or modify. - Output generation – The workflow ends when a termination condition is met, and the final message is returned.
Sequential Workflow Pattern
The simplest pattern: agents execute one after another in a predetermined order.
Flow: Step A → Step B → Step C
Implementation: Use a GroupChat with allowed_or_disallowed_speaker_transitions to enforce a linear chain.
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.teams._group_chat_manager import GroupChatManager
from autogen_agentchat.agents import AssistantAgent
# ... define agents: researcher, writer, reviewer ...
# Configure transitions: researcher -> writer -> reviewer -> stop
allowed_transitions = {
"researcher": ["writer"],
"writer": ["reviewer"],
"reviewer": [] # end
}
team = RoundRobinGroupChat(
participants=[researcher, writer, reviewer],
# RoundRobinGroupChat doesn't directly support allowed_transitions;
# Use SelectorGroupChat or a custom selector with allowed transitions.
)
# For sequential enforcement, use SelectorGroupChat with a custom selector function:
async def selector(messages):
# logic: if last speaker was researcher -> writer, etc.
...
team = SelectorGroupChat(participants=[...], selector_func=selector)
Use cases: Data processing pipelines, content generation chains, multi‑step validation.
Benefits: Simple to reason about, easy to debug. Limitations: No branching; all steps always run.
Conditional Workflow Pattern
The next step is determined at runtime based on state or agent output.
Implementation: Use SelectorGroupChat with an LLM‑based selector or a custom selector_func that examines the conversation. The selector returns the name of the next agent.
async def conditional_selector(messages):
last_msg = messages[-1].content
if "refund" in last_msg:
return "refund_specialist"
elif "sales" in last_msg:
return "sales_agent"
else:
return "general_agent"
team = SelectorGroupChat(
participants=[general, refund_specialist, sales_agent],
selector_func=conditional_selector
)
You can also use handoffs: an agent emits a HandoffMessage(target="refund_specialist") when it detects a certain condition.
Use cases: Customer support triage, task routing based on intent.
Event‑Driven Workflow Pattern
Workflow progression is triggered by runtime events rather than explicit agent output. For example, a tool call request might pause the workflow, await human approval, and then resume.
Implementation: Subscribe to events on the runtime and use custom handlers to inject new messages or redirect the conversation.
from autogen_core import EVENT_TOPIC
async def handle_tool_request(event, context):
if isinstance(event, ToolCallRequestEvent):
approval = await get_human_approval(event.content)
if not approval:
# Cancel the call by not executing, or inject a refusal message
...
runtime.subscribe(EVENT_TOPIC, handle_tool_request)
Use cases: Sensitive actions (financial transactions, external API calls) that always require human sign‑off, dynamic workflow branching based on tool results.
Iterative Workflow Pattern
An agent (or a group) loops through refinement steps until a quality threshold is met or a maximum number of iterations is reached.
Implementation: A group chat where the final agent (e.g., reviewer) can send the work back to a previous agent (writer) for revision. The termination condition is met only when the reviewer approves.
allowed_transitions = {
"writer": ["reviewer"],
"reviewer": ["writer", "__end__"]
}
team = SelectorGroupChat(
participants=[writer, reviewer],
selector_func=revision_selector,
termination_condition=TextMentionTermination("APPROVED")
)
The revision_selector would route back to writer if the reviewer’s message does not contain “APPROVED”. The conversation loops until approved.
Use cases: Content improvement, code review & fix, data validation loops.
Human‑in‑the‑Loop Workflow Pattern
Human judgment is inserted at a specific step. The workflow pauses, presents information to a human, and resumes with the human’s response.
Implementation: Use a UserProxyAgent that represents the human. In the group chat, when it’s the human’s turn, the system waits for input (e.g., from a console or a web UI).
user_proxy = UserProxyAgent(name="human", ...)
team = RoundRobinGroupChat([agent, user_proxy])
For more fine‑grained control, intercept events and pause the runtime until a human provides a Command(resume=…).
Use cases: Approval workflows, review gates, manual corrections.
Tool‑Oriented Workflow Pattern
The workflow is heavily driven by tool invocations. Agents gather data via tools, pass the results, and make decisions.
Implementation: Provide agents with relevant tools and design the conversation to naturally prompt tool usage. For example, a research agent uses a search tool, then passes the raw data to an analyst agent that uses a computation tool.
The workflow logic is still governed by the conversation pattern (sequential, routing, etc.), but the value is produced by the tool calls.
Use cases: Search‑augmented generation, data retrieval and analysis pipelines, automation scripts.
Workflow State Management
State is the backbone of a reliable workflow.
- Shared state – The message list itself serves as the shared state. All agents see the full history (unless filtered).
- Context propagation – When handing off, you can include a summary or key data to keep the context lean.
- State persistence – Use
runtime.save_state()andload_state()to persist the entire workflow to disk or a database. This enables crash recovery and resumption. - State recovery – If a workflow fails, you can reload the state from the last checkpoint and continue from there.
In production, always use a persistent runtime with a database backend.
Workflow Control Mechanisms
| Mechanism | Description |
|---|---|
| Branching | Select next agent based on state or agent output (conditional edges). |
| Routing | Dynamic routing via selector function or handoffs. |
| Looping | Repeatedly invoke an agent until a condition is met (iterative pattern). |
| Termination Conditions | TextMentionTermination, MaxTurnTermination, or custom functions that stop the workflow. |
| Retry Logic | Within tools or at the workflow level: if an agent fails, catch the exception, adjust state, and re‑run the step. |
These controls give you precise command over execution flow without writing a custom state machine.
Error Handling
Errors are inevitable. A robust workflow anticipates them.
- Agent failures – Wrap agent calls in try/except. If an exception occurs, log it and decide whether to retry, fallback to another agent, or terminate with a graceful message.
- Tool failures – Catch exceptions inside tool functions and return an error string. The agent can then attempt a different approach.
- Event failures – If an event handler raises an exception, the runtime can skip the handler (depending on configuration). Always wrap handlers.
- Context corruption – If the message history becomes inconsistent, restore from a saved state.
- Recovery strategies – Implement a “reset” keyword, a dead‑letter queue for failed workflows, and alerts to operations.
Workflow Observability
You need full visibility into workflow execution to debug and optimise.
- Execution tracing – Use the runtime’s event stream. Subscribe to all events and log them with timestamps.
- Workflow logging – Log every speaker transition, tool call, and termination.
- Metrics – Track workflow duration, success/failure rate, token usage, and agent latency.
- Failure analysis – When a workflow fails, capture the full message history and the stack trace.
Integrate with OpenTelemetry or Azure Monitor to build dashboards and alerts.
Performance Optimization
- Simplify workflows – Reduce the number of agents and turns. Each turn adds LLM latency.
- Reduce unnecessary steps – If an agent can answer directly, skip the tool call.
- Efficient message design – Keep messages concise; avoid bloated context.
- Context optimization – Truncate old messages or summarise them.
- Tool optimization – Cache deterministic tool results. Use local code execution instead of remote APIs when possible.
Common Workflow Patterns
| Pattern | Typical flow | Use case |
|---|---|---|
| Research | Search → Retrieve → Analyse → Summarise | Market intelligence |
| Content Generation | Research → Outline → Draft → Edit → Publish | Blog writing |
| Data Processing | Extract → Clean → Transform → Load → Validate | ETL pipelines |
| Customer Support | Classify intent → Retrieve knowledge → Draft response → Review (optional) → Send | Helpdesk automation |
| Automation | Trigger event → Gather data → Execute actions → Report result | DevOps, business automation |
Each can be built by composing the primitive patterns discussed above.
Common Beginner Mistakes
- Overcomplicated workflows – Too many agents and conditional branches that are never fully tested.
- Poor state management – Not persisting state, leading to lost context on restarts.
- Missing termination conditions – Workflows that hang forever. Always set a
max_turnsor a clear termination signal. - Excessive context growth – Passing the entire conversation every turn, causing high token costs and latency.
- Weak error handling – A single tool failure aborts the entire workflow.
Best Practices
- Design simple workflows first – Start linear, then add branching only where needed.
- Use clear transitions – Explicitly define allowed speaker transitions.
- Keep agents focused – Each agent does one job well.
- Minimize unnecessary communication – Avoid idle chatter; use handoffs for direct delegation.
- Track workflow metrics – Monitor duration, turn count, and failure rate from day one.
- Build recovery mechanisms – Always have a fallback path and state persistence.
Practical Example: Research Report Workflow
We’ll build a workflow that researches a topic, analyses the data, writes a draft, and reviews it. We’ll use a sequential chain enforced via a group chat with allowed transitions, and a termination condition when the report is approved.
1. Define agents and tools
from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_core.tools import FunctionTool
model_client = OpenAIChatCompletionClient(model="gpt-4o")
def web_search(query: str) -> str: ...
search_tool = FunctionTool(web_search, description="Search the web")
researcher = AssistantAgent("researcher", model_client=model_client, tools=[search_tool],
system_message="Research the topic. Output a summary of findings.")
analyst = AssistantAgent("analyst", model_client=model_client,
system_message="Analyse the research summary. Identify trends and key insights.")
writer = AssistantAgent("writer", model_client=model_client,
system_message="Write a report from the analysis. Include sections: Introduction, Findings, Conclusion.")
reviewer = AssistantAgent("reviewer", model_client=model_client,
system_message="Review the report. If it needs changes, say 'REVISE' and provide feedback. If OK, say 'APPROVED' and output the final report.")
2. Configure the sequential chain with a custom selector
from autogen_agentchat.teams import SelectorGroupChat
from autogen_agentchat.teams._termination_condition import TextMentionTermination
# Define the allowed sequence: researcher -> analyst -> writer -> reviewer -> (loop back to writer if needed)
allowed = {
"researcher": ["analyst"],
"analyst": ["writer"],
"writer": ["reviewer"],
"reviewer": ["writer", "__end__"]
}
async def sequential_selector(messages):
last_source = messages[-1].source
next_speakers = allowed.get(last_source, [])
if not next_speakers:
return "__end__"
# If reviewer said 'APPROVED', end
if last_source == "reviewer" and "APPROVED" in messages[-1].content:
return "__end__"
return next_speakers[0] # follow the chain (or loop if reviewer says REVISE -> writer)
termination = TextMentionTermination("APPROVED")
team = SelectorGroupChat(
participants=[researcher, analyst, writer, reviewer],
selector_func=sequential_selector,
termination_condition=termination
)
3. Run the workflow
result = await team.run(task="Research the impact of AI agents on software development")
print(result.messages[-1].content) # final approved report
Execution flow:
researcherreceives the task, callsweb_search, and outputs a summary.analystreceives the summary, identifies trends.writerreceives the analysis, writes a draft.reviewerreads the draft. If it says “REVISE”, the selector routes back towriter(iterative loop). If “APPROVED”, the workflow ends.- The final message containing “APPROVED” is returned.
State management: The entire message history is available to each agent. Because we use a persistent runtime (not shown), the state can be saved and resumed.
Observability: All messages and transitions are logged; you could add event handlers to record metrics.
Workflows in Production Systems
- Reliability – Use a persistent runtime, retries, and termination conditions. Design workflows to be idempotent where possible.
- Monitoring – Track workflow duration, failure rate, and agent‑level metrics. Set up alerts for anomalies.
- Scaling – Run multiple workflow instances concurrently. Use a shared message bus or database for state.
- Cost control – Monitor token usage per workflow. Set per‑workflow budget caps.
- Maintenance – Version your workflow definitions. Test changes in staging before rolling out.
Workflows and Other AutoGen Concepts
- Core Concepts – Agents, messages, tools, and runtime are the building blocks of any workflow.
- Conversation Patterns – Workflows are higher‑level orchestrations of these patterns.
- Tools & Code Execution – Workflows heavily use tools and code as actionable steps.
- Production – The deployment and operational aspects of workflows.
- MCP – Standardised tool interfaces that can be plugged into workflows.
For cross‑framework comparisons, visit the LangGraph, CrewAI, and OpenAI Agents SDK guides, and the framework comparison.
FAQ
1. What is a workflow in AutoGen?
A workflow is the orchestrated sequence of agent interactions, tool calls, and event handling that fulfills a user request.
2. How do workflows work?
They work by defining a team (group chat), transition rules, and termination conditions. The runtime manages message delivery and agent invocation.
3. What is workflow state?
The shared conversation history and any persisted data. The runtime can save/load state for durability.
4. How do events affect workflows?
Events can pause, redirect, or augment workflows. For example, intercepting a tool call event allows human approval.
5. Can workflows be conditional?
Yes, using dynamic selectors or handoffs based on the current conversation content.
6. How do I handle workflow failures?
Wrap in try/except, use retry logic, and persist state so that a failed workflow can be resumed.
7. Are workflows production‑ready?
Yes, with persistent runtime, proper error handling, and monitoring, AutoGen workflows can power production systems.
8. How do I enforce a strict order of agents?
Use a SelectorGroupChat with a custom selector function that enforces a fixed sequence, or use handoffs.
9. Can I loop back to a previous agent?
Yes, by allowing transitions from a later agent back to an earlier one, combined with a termination condition to break the loop.
10. What is the difference between a workflow and a conversation?
A workflow is the overall business process; a conversation is the mechanism (messages) by which it’s executed. A workflow can span multiple conversations.
11. How do I implement human‑in‑the‑loop?
Add a UserProxyAgent to the team, or intercept events and wait for external input before resuming.
12. Can I run workflows asynchronously?
Yes, the entire runtime is async; you can kick off a workflow and handle the result later.
13. How do I debug a stuck workflow?
Look at the message history, check termination conditions, and use event handlers to log every step.
14. What is the best way to manage context growth?
Truncate messages, use summarization middleware, or store large data outside the message list and reference it.
15. Can I have parallel branches in a workflow?
Yes, by allowing multiple agents to speak concurrently? AutoGen group chats are single‑turn per step. For parallelism, you can fan‑out to multiple separate conversations and merge results.
16. How do I version my workflows?
Store agent configurations and team definitions in version control. Use a registry to map workflow names to their current implementation.
Conclusion
AutoGen Workflows give you the power to orchestrate multi‑agent systems with clarity and control. By composing sequential, conditional, event‑driven, and iterative patterns, you can build automation that is predictable, resilient, and maintainable.
Key takeaways:
- Workflows define the execution logic—they decide which agent speaks next and when to stop.
- Use selectors, handoffs, and termination conditions to shape the flow.
- State management and error handling are critical for production.
- Observability and optimisation are ongoing responsibilities.
Now, continue your journey:
- AutoGen Tools & Code Execution – the actions your workflows will perform.
- AutoGen Production – deploy and monitor your workflows at scale.
- AutoGen Core Concepts – a refresher on the fundamentals.
- AutoGen Conversation Patterns – the building blocks of workflow communication.
For standardised tools, check the MCP Guide. For cross‑framework comparison, see the LangGraph and CrewAI guides, and the full comparison.
Now, orchestrate your agents and automate the future.