Agent Mentor Learn
Designing Agent Workflows: From One-Off Conversations to Multi-Step Automation · Lesson 2 of 6

Lesson 2: Workflow Building Blocks: Steps, State, Branches, Loops

Learning goals:

  • Master the four core building blocks of a workflow
  • Understand dependencies and data passing between steps
  • Learn to design a workflow's execution flowchart

Prerequisites: Lesson 1: From Conversation to Workflow | Next: Lesson 3 >>

Workflows Aren't Magic, They're Composition

In the last lesson we saw that a workflow can coordinate dozens of agents to finish a complex task. But open up a workflow script and you'll find it's just ordinary code: functions, loops, conditionals.

The power of a workflow comes from combining four simple building blocks:

  1. Steps — the basic unit of work
  2. State — data shared between steps
  3. Branches — choosing a path based on a condition
  4. Loops — repeating a similar operation

Once you understand these four building blocks, you can design a workflow of any complexity.1

Building Block 1: Steps

A step is a workflow's atomic operation. Each step is either an Agent call or a deterministic function.2

Agent Steps vs Function Steps

When to use an Agent step:

  • You need to understand fuzzy input (natural language, unstructured data)
  • You need to generate creative content (docs, code, explanations)
  • You need to make a judgment (does this code have a security problem?)

When to use a function step:

  • Data transformation (filter, sort, format)
  • Math (statistics, aggregation)
  • Conditional checks (if-else logic)
  • File operations (read, write, move)

Best practice: Agent steps reason, function steps compute. Don't make the LLM do a simple array filter or add up numbers — it's slow, expensive, and unreliable.2

A Step's Input/Output Contract

Every step should have a clear input/output contract:

A clear contract makes a workflow easy to understand and debug. When step 5 breaks, you can immediately see it's because step 4's output was in the wrong format.3

Building Block 2: State

State is the data shared between steps. It's like the workflow's memory, holding intermediate results and execution progress.4

Two Kinds of State

Workflow State:

  • All the information about the current task: which step you're on, each step's result, what the next step needs
  • Stored in script variables or an external database
  • Passed between steps, but not across sessions

Session State:

  • The user's conversation history and preference settings
  • The Agent manages this itself; the workflow doesn't need to care about it4

State Management Patterns

Pattern 1: Script variables (good for short workflows)

Pattern 2: A state object (good for medium complexity)

Pattern 3: External storage (good for long-running workflows)

Checkpointing: Save state after key steps so the workflow can resume from the point of failure instead of starting over.5

Building Block 3: Branches

A branch chooses a different execution path based on a condition.6

Simple Branch

Branching on an Agent's Decision

Error-Handling Branch

Building Block 4: Loops

A loop lets you run the same operation over many similar objects. This is the core source of a workflow's power.6

Sequential Loop

Parallel Loop

Here, limit = 5 is the concurrency cap: run at most 5 at a time instead of firing off all 100 at once with Promise.all, which would open too many connections or file handles.

Loop with Accumulation

Loop with Conditional Termination

Combining the Building Blocks: A Complete Workflow

Let's combine these four building blocks to design a "microservice health check" workflow:

mermaid
graph TD    A[Start] --> B[List all services]    B --> C{More than 10 services?}    C -->|Yes| D[Check all services in parallel]    C -->|No| E[Check all services sequentially]    D --> F[Collect results]    E --> F    F --> G{Any failed services?}    G -->|Yes| H[Generate alert report]    G -->|No| I[Generate health report]    H --> J[Send notification]    I --> K[End]    J --> K

The matching script:

This workflow uses all four building blocks:

  • Steps: listServices, checkServiceHealth, the agent() calls
  • State: the state object holding the total and the healthy/unhealthy lists
  • Branches: parallel vs sequential based on service count, and report type based on health status
  • Loops: the map parallel loop, the for sequential loop

How to Think About Designing Workflows

Work backward from the endpoint:

  1. What's the final output? (a report, deployed services, cleaned-up code)
  2. What input does the last step need? (aggregated data, validated results)
  3. Where does that input come from? (the previous step's output)
  4. Repeat until you reach the start (user input or the file system)

Spot the parallel opportunities:

  • If several steps don't depend on each other, they can run in parallel
  • "For each X, do Y" can usually be parallelized
  • Parallelism can take ten 5-minute tasks from 50 minutes down to 5

Make dependencies explicit:


Next: Lesson 3: Decomposing a Complex Task into a Workflow — strategies for systematically breaking a complex task down into workflow steps

Footnotes

  1. MindStudio: Five Claude Code Agentic Workflow Patterns — https://www.mindstudio.ai/blog/claude-code-agentic-workflow-patterns

  2. Mae Capozzi: Building a Multi-Agent Orchestrator — https://maecapozzi.com/blog/building-a-multi-agent-orchestrator 2

  3. AWS Marketplace: Agent Orchestration — https://aws.amazon.com/marketplace/build-learn/ai-agent-learning-series/agent-orchestration

  4. MindStudio: Workflow State vs. Session State — https://www.mindstudio.ai/blog/workflow-state-vs-session-state-ai-agents 2

  5. MachineLearningMastery: 5 Architectural Patterns for Persistent Memory and State in AI Agents — https://machinelearningmastery.com/5-architectural-patterns-for-persistent-memory-and-state-in-ai-agents/

  6. Alex Op: Claude Code Workflows and Deterministic Orchestration — https://alexop.dev/posts/claude-code-workflows-deterministic-orchestration/ 2

Exercises

01

Task: Design a "batch image processing" workflow. The input is 50 images, and you need to: (1) resize them to 800x600, (2) add a watermark, (3) convert them to WebP format.

Level 1: Design a Simple Workflow

Requirements:

  • Draw the flowchart (a text description works too, e.g. A → B → C)
  • State which steps use functions and which use an Agent
  • State where things can run in parallel
  • Write the core part of the pseudocode (loop and branch)
Done criteria · checked locally
02

A "codebase migration" workflow needs to: (1) scan 200 files to find the API calls that need migrating, (2) migrate all files in parallel, (3) run tests, (4) if the tests fail, roll back all changes.

Level 2: Identify State Management Needs

Questions:

  1. What state does this workflow need to save? List at least 3 state fields.
  2. After which step should you set a checkpoint? Why?
  3. If step 3 (running tests) fails, what state information does the workflow need to roll back correctly?
Done criteria · checked locally