The Core Idea: A workflow is a graph of steps. Each step does some work, updates the state and names the next step. Explicit branches, parallel steps and checkpoints make agents predictable and recoverable.
- Beginner: Sequential Workflows
A sequential workflow runs steps in a fixed order: classify the question, then research, then draft, then review. The language model can be used inside some steps, but your code decides the order.
| Free agent loop | Workflow | |
|---|---|---|
| Who picks the next step | The model, every time | Your code, from a graph |
| Predictability | Lower | Higher |
| Flexibility | Higher | Lower |
| Best for | Open-ended tasks | Known processes with some model steps |
Practical advice: Many successful products use workflows with small agent-like steps inside, because they are easier to test and cheaper to run.
- Beginner: Conditional Workflows and Branching
A conditional step looks at the state and chooses where to go next. This is branching:
| Condition | Branch |
|---|---|
| Question mentions an invoice | Billing path |
| Question is technical | Technical path |
| Draft failed the review | Go back to drafting |
| Confidence is low | Ask a human |
In the code: The classify step sets the kind of question, and the review step either approves the draft or sends the run back to draft. That second case is a loop inside the workflow.
- Medium: Parallel Execution
When subtasks do not depend on each other, run them at the same time. In the code, three sources are searched in parallel, so the total time is about that of the slowest one instead of the sum of all three.
| Pattern | Description |
|---|---|
| Fan-out | Start several steps at once |
| Fan-in | Wait for all and combine the results |
| Race | Use the first good result and cancel the rest |
Careful: Parallel steps must not write to the same state at the same time. Give each its own output and merge afterwards.
- Medium: State Transitions
Think of the workflow as a state machine: it is always in one state (a step), and each step ends by returning the next state. All the data lives in a shared state object.
| Part | Role |
|---|---|
| Nodes | Steps that do work |
| Edges | Allowed moves from one node to another |
| State object | Data passed along and updated |
| Terminal state | done or failed |
- Advanced: Checkpoints
A checkpoint saves the state after each step. If the process crashes, is restarted or must wait for a human, the run resumes from the last checkpoint instead of starting over.
In the code: The run crashes after the research step. After a restart it loads the checkpoint and continues at the draft step. The classify and research work is not repeated.
Tip: Make steps idempotent (safe to run twice) so a resume after a partial step does not duplicate effects like sending an email.
- Visual Explanation: A Workflow With Branches, Parallel Steps and a Checkpoint
The figure shows the four nodes of the workflow in the code, then what happens when the run crashes and resumes.
Click to see why steps should be idempotentCompare ▼
Part 1 labels each node with the pattern it demonstrates. Part 2 shows the crash and the resume. Open the bar to see why idempotent steps matter.
- Python Implementation: A State Machine With Parallel Steps and Checkpoints
This script implements a four-node workflow. It branches on the question type, searches three sources in parallel, loops between draft and review, saves a checkpoint after every node, simulates a crash, and resumes from the checkpoint.
import json, os, tempfilefrom concurrent.futures import ThreadPoolExecutor CHECKPOINT = os.path.join(tempfile.mkdtemp(), "run.json") def save(state, next_node): with open(CHECKPOINT, "w") as f: json.dump({"state": state, "next": next_node}, f) def load(): with open(CHECKPOINT) as f: data = json.load(f) return data["state"], data["next"] # Each node does some work and names the next node (that is the state transition)def classify(s): s["kind"] = "billing" if "invoice" in s["question"] else "technical" # conditional branch return "research" def research(s): sources = ["knowledge_base", "faq", "tickets"] with ThreadPoolExecutor(max_workers=3) as pool: # parallel execution s["facts"] = list(pool.map(lambda name: "fact from " + name, sources)) return "draft" def draft(s): s["attempts"] = s.get("attempts", 0) + 1 s["draft"] = "Answer built from " + str(len(s["facts"])) + " facts" return "review" def review(s): return "done" if s["attempts"] >= 2 else "draft" # first review rejects: loop back NODES = {"classify": classify, "research": research, "draft": draft, "review": review} def run(state, node, crash_after=None): while node != "done": finished = node node = NODES[node](state) save(state, node) # checkpoint after every node print("ran", finished.ljust(9), "-> next:", node) if finished == crash_after: print("*** process crashed ***") return None return state state = {"question": "Why was my invoice charged twice?"}run(state, "classify", crash_after="research") print("--- restart: load the checkpoint and continue ---")state, node = load()final = run(state, node)print("Result:", final["kind"], "|", final["draft"], "| attempts:", final["attempts"])After the crash the run reloads its state and continues at the draft node. The review step rejects the first draft and approves the second, so draft runs twice, and the final state contains the type of question, the draft and the number of attempts.
Knowledge Check
Key Points
The Big Picture
Nodes do work → return the next node → state is saved after each one → crashes become resumes
Workflows make agents predictable. Next you will make them reliable when things go wrong.
Up Next: No agent runs perfectly forever. In Error Handling and Recovery you will learn retries, fallbacks, timeouts and how to stop endless loops.