Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 39 ยท Agent Concepts and Loops

Workflows versus Agents: Chaining, Routing, Parallel

Compare fixed LLM workflows with free-running agents and learn the building blocks: prompt chaining, routing, parallelization and evaluator loops, in Python.

Not every LLM application needs an agent. A workflow is a program where your code decides the sequence of steps and the model fills in individual steps. An agent lets the model decide the sequence. Workflows are more predictable, cheaper and easier to test; agents are more flexible. A good engineer knows both and picks the simplest one that works, often starting with a workflow and adding autonomy only where needed.

flowchart LR
  subgraph W["Workflow: code controls the path"]
    A1["Step 1"] --> A2["Step 2"] --> A3["Step 3"]
  end
  subgraph G["Agent: model controls the path"]
    B1["Decide"] --> B2["Act"] --> B3["Observe"] --> B1
  end

A workflow follows the arrows you drew. An agent chooses its own arrows at run time.

Below are the common workflow patterns. Each uses a stand-in model function so that the control structure is the only thing being tested.

Pattern 1: prompt chaining

Break a task into a fixed sequence where each step's output feeds the next. Add a gate between steps: a code check that stops the chain early if the intermediate result is bad.

Python Stand-in: a chain with a gate

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Use chaining when a task splits cleanly into stages and accuracy improves by doing each stage separately.

Pattern 2: routing

Classify the input first, then send it to a specialized handler: a cheap model for easy questions, a stronger one for hard ones, different prompts for billing versus technical support.

Python Stand-in: a router

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Routing works when inputs fall into distinct categories that each deserve a different treatment. Always include a "general" or "unknown" route so that surprising input has somewhere safe to go.

Pattern 3: parallelization

Run independent model calls at the same time and combine them. Two flavors: sectioning (different subtasks in parallel) and voting (the same task several times, taking the majority for confidence). In real programs use threads or asyncio; the logic of combining is what matters.

Python Voting across several attempts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Pattern 4: evaluator-optimizer

One call produces a draft, another critiques it against clear criteria, and the loop repeats until the critic approves or a limit is reached. This is a small, bounded loop inside an otherwise fixed workflow, and it foreshadows the reflection lesson.

Choosing: a rule of thumb

Ask in order:

  1. Can one well-written prompt do it? Do that.
  2. Can you list the steps? Use chaining, routing or parallel calls.
  3. Is the path unknowable until you start? Use an agent, with the limits from stop conditions and budgets.

You can also mix them: a workflow whose single step is an agent ("research this subtopic") keeps the overall shape predictable while allowing flexibility where it pays.

Python
# Read-only: a workflow that calls an agent for one open-ended step
def handle_ticket(ticket):
    category = classify(ticket)                 # routing (cheap, predictable)
    if category == "technical":
        return investigate_with_agent(ticket)   # open-ended: agent with a budget
    return answer_from_templates(category, ticket)

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Who decides the sequence of steps in a workflow?

    In a workflow the path is fixed by code. In an agent the model chooses it.
  2. 2. What is the purpose of a gate in a prompt chain?

    A cheap check between stages catches errors before they flow into later steps.
  3. 3. Which pattern runs the same task several times and picks the most common answer?

    Voting trades extra calls for higher confidence in the result.
  4. 4. Why include a general or unknown route in a router?

    Inputs outside your categories will occur. A default route prevents failures or wrong specialized handling.

Summary

  • Workflows put control in code; agents put it in the model.
  • Prompt chaining, routing, parallelization and evaluator loops cover many real tasks.
  • Add gates between steps and a default route in routers.
  • Prefer the simplest design: single prompt, then workflow, then agent.
  • Combine them: workflows can call agents for the open-ended steps.

Next: Structured Outputs and Parsing