
Mapping Thought: Sequential vs Branching Workflows
Learn how to structure complex reasoning. Master the design patterns for linear processes versus decision-heavy branching logic in autonomous agents.
Sequential vs Branching Workflows
In the previous module, we discussed the "Mindset" of an agent. Now, we must discuss the Structure. When you design an agent in LangGraph, you are creating a map for its thoughts. This map can be a straight line (Sequential) or a complex maze of decisions (Branching).
Choosing the right structure is the difference between a system that is robust and one that is fragile.
1. Sequential Workflows: The Pipeline Pattern
A sequential workflow is a fixed chain of events. A must happen before B, and B must happen before C.
Characteristics
- No loops: It moves in one direction.
- High Success Rate: Since the path is fixed, there are fewer ways for the agent to "get lost."
- Low Agency: The "Reasoning" is restricted to fulfilling the current step.
Example: The "Blog Post Generator"
- Node 1: Create an outline from a topic.
- Node 2: Research the specific points in the outline.
- Node 3: Write the draft.
- Node 4: Format the draft as Markdown.
graph LR
Start --> Outline
Outline --> Research
Research --> Write
Write --> Format
Format --> End
2. Branching Workflows: The Decision Pattern
A branching workflow uses a Router node. Based on the output of a reasoning step, the system chooses one of many potential paths.
Characteristics
- Adaptive: Can handle different types of user requests in a single flow.
- Complex: Requires careful state management to ensure the branching nodes don't lose the original context.
Example: The "Smart Email Assistant"
- The Brain reads an email.
- The Router classifies it:
- Path A: If it's a Meeting Request, go to the
Calendar_Node. - Path B: If it's a Compliment, go to the
Thank_You_Node. - Path C: If it's a Complaint, go to the
Escalation_Node.
- Path A: If it's a Meeting Request, go to the
graph TD
Entry[Read Email] --> Router{Classify}
Router -->|Meeting| Cal[Calendar Node]
Router -->|Compliment| Thank[Appreciate Node]
Router -->|Help| Support[Support Node]
Cal --> Exit
Thank --> Exit
Support --> Exit
3. The "Conditional Edge" in LangGraph
In code, we implement branching using add_conditional_edges. This is a powerful feature that allows your graph to be "intelligent" about its own navigation.
def route_request(state: MyState):
# This function returns the NAME of the next node
if "refund" in state["query"]:
return "billing_department"
return "general_support"
# Map the decision to the graph
workflow.add_conditional_edges("inbox_node", route_request)
4. When to Use Which?
| Requirement | Sequential | Branching |
|---|---|---|
| Consistency | 🟢 High | 🔴 Low |
| Logic Complexity | 🔴 Low | 🟢 High |
| Testing | 🟢 Easy | 🔴 Difficult |
| Versatility | 🔴 Low | 🟢 High |
5. Merging Paths: The "Join" Pattern
A common production pattern is to branch out to perform multiple tasks in parallel and then Join them back together before giving a final answer.
Example: Comprehensive Research
- Branch 1: Search X (Twitter) for sentiment.
- Branch 2: Search Financial News for stock price.
- Wait: The system pauses until both branches return data.
- Join: A "Synthesis Node" takes data from both and writes the report.
6. The Danger of "Infinite Branching"
In fully autonomous agents, branching can become recursive. An agent might say, "I need an ID to call this tool. To get the ID, I need to search the database. To search the database, I need to find the database name..."
Rule of Thumb: For production, limit your branching "depth" to no more than 3-4 levels. Anything deeper should be broken into a separate agent.
Summary and Mental Model
Think of a Sequential Workflow like an Assembly Line. Think of a Branching Workflow like a Decision Tree.
Most production agents start as a Sequence (to gather data) and end with a Branch (to handle different actions based on that data).
In the next lesson, we will learn how to handle the inevitable failures of these flows using Conditional Execution and Retries.
Exercise: Workflow Mapping
- Draw the Graph: Map out the workflow for an agent that takes a user's resume, finds 3 matching jobs on LinkedIn, and then drafts 3 unique cover letters.
- Is it Sequential or Branching?
- Where would you put a "Join" node?
- Logic Check: Why would a "Branching" workflow be better for a medical triaging app than a sequential one?
- The State: If a workflow branches into 3 paths, which object in LangGraph is responsible for making sure the data from Path A is available to Path C?
- (Hint: It starts with an 'S'). Ready to add complexity? Let's move to Retries.