Learn/Phase 1/Agents and Workflows

Agents and Workflows

Ch 03 · Foundations 45 min
Agent loopStopping conditionsWorkflow vs. prompt
Hands-on:MESHFLOW_MOCK=1 python3 hands_on/03_policy_and_budget.py

Lesson 03: Agents And Workflows

Lesson Goal

This lesson explains what an AI agent is, what a workflow is, and why strong systems often use both. By the end, you should be able to look at an AI idea and decide whether it should be built as:

  • A single LLM call.
  • A tool-using assistant.
  • An agent loop.
  • A fixed workflow.
  • A workflow that contains one or more agents.

1. Start With The Simplest Thing

Not every AI feature needs an agent.

Use a single LLM call when:

  • The task is short.
  • No tools are needed.
  • No durable memory is needed.
  • The output can be checked easily.
  • The process does not need multiple steps.

Example:

Summarize this paragraph in three bullets.

Use more structure when the task requires state, tools, decisions, or review.

2. What Is An Agent?

An AI agent is a system that uses a model to pursue a goal by choosing actions, observing results, and continuing until it stops.

The agent loop:

observe -> decide -> act -> observe result -> decide again -> stop

An agent usually has:

  • Goal: what it is trying to accomplish.
  • Model: the LLM that reasons over the task.
  • Instructions: role, constraints, style, and policies.
  • Context: what the model can see now.
  • Tools: actions it may request.
  • Memory or state: information carried across turns or steps.
  • Stopping rule: when to finish.
  • Guardrails: what it is not allowed to do.

3. Agent Example

Goal:

Find the best source in the course for explaining memory.

Possible loop:

  1. Observe the user goal.
  2. Decide that it needs to search course files.
  3. Call a search tool for "memory".
  4. Observe matching lesson files.
  5. Decide which file is most relevant.
  6. Read that file.
  7. Produce an answer with the chosen source.
  8. Stop.

The important part is that the agent chooses the next action instead of following one fixed prompt.

4. What Is A Workflow?

A workflow is an explicit process made of steps and dependencies.

Example:

capture goal
  -> gather context
  -> draft answer
  -> check quality
  -> request approval
  -> publish final answer

Each step has a known job. The workflow controls order, inputs, outputs, and stopping points.

A workflow usually has:

  • Nodes or steps.
  • Dependencies.
  • Inputs and outputs.
  • Artifacts.
  • Conditions or branches.
  • Gates.
  • Traces.

5. Agent vs Workflow

QuestionAgentWorkflow
Who chooses the next action?The agent loopThe workflow graph
Is the path predictable?Less predictableMore predictable
Best forOpen-ended tasksRepeatable processes
Main riskDrift, loops, unsafe actionRigid design, missed edge cases
NeedsStopping rules and tool controlsClear artifacts and dependencies

Use an agent when the path is uncertain. Use a workflow when the process must be repeatable, auditable, or governed.

6. The Best Pattern: Agents Inside Workflows

Production systems often place agents inside workflow boundaries.

workflow controls:
  step order
  artifacts
  gates
  budgets
  trace

agent controls:
  local reasoning
  tool selection
  drafting
  exploration

Example:

capture_goal
  -> researcher_agent
  -> reviewer_tool
  -> writer_agent
  -> approval_gate
  -> final_output

The agents do intelligent work, but the workflow still defines the production process.

7. Stopping Rules

Every agent needs a stopping rule. Without one, an agent can repeat itself, call tools too many times, or keep searching forever.

Common stopping rules:

  • Stop after producing a required artifact.
  • Stop after a maximum number of turns.
  • Stop when confidence is high enough.
  • Stop when a reviewer approves.
  • Stop when a budget is reached.
  • Stop when a required tool fails.
  • Stop when the user interrupts.

Beginner rule: define the stop condition before you define the tools.

8. Tool Permissions

Agents are only as safe as their permissions. Give agents the smallest useful tool set.

Poor design:

research_agent can search, email customers, update database, deploy code

Better design:

research_agent can search read-only sources
writer_agent can draft text
reviewer_agent can score text
publisher_action requires human approval before sending

Separate roles reduce risk.

9. Workflow Artifacts

Artifacts make agent work visible. Instead of letting an agent think in a hidden conversation, require named outputs.

Examples:

  • research_questions
  • source_notes
  • risk_assessment
  • draft_answer
  • review_score
  • approval_record
  • final_answer

If the artifact is named, downstream steps can depend on it and humans can inspect it.

10. Hands-On: Compare Examples

Run a simple LLM workflow:

python3 -m src.mini_meshflow run examples/01_basic_llm.json

Run a tool and memory workflow:

python3 -m src.mini_meshflow run examples/02_tools_and_memory.json

Run an agent with a gate:

python3 -m src.mini_meshflow run examples/03_agent_with_gate.json

Ask:

  • Where does the model generate text?
  • Where does a tool run?
  • Where does an agent plan?
  • Where does execution stop?
  • Which artifacts are produced?

11. Design Exercise

Turn this vague request into a workflow:

Help me prepare a beginner training lesson on AI agents.

One possible workflow:

capture_goal
  -> outline_lesson
  -> collect_examples
  -> draft_lesson
  -> beginner_review
  -> revise_lesson
  -> approval_gate
  -> publish

Now decide which steps should be agents:

  • outline_lesson: agent, because it designs structure.
  • collect_examples: agent with search/read tools.
  • beginner_review: tool or reviewer agent.
  • approval_gate: not an agent; it is a hard workflow stop.

12. Common Beginner Mistakes

Mistake 1: Calling every LLM feature an agent.

Correction: An agent chooses actions across a loop. A single prompt is not an agent.

Mistake 2: Giving agents too many tools.

Correction: Use role-specific tools and gates for risky actions.

Mistake 3: Hiding the process in one giant prompt.

Correction: Use workflow nodes and artifacts for important steps.

Mistake 4: Forgetting stopping rules.

Correction: Define limits, completion criteria, and failure behavior.

Mistake 5: Treating workflows as too rigid.

Correction: Put flexible agents inside controlled workflow steps.

13. Summary

An LLM generates text. An agent uses an LLM inside a goal-directed loop. A workflow defines a repeatable process. The strongest production pattern is often a workflow that contains agents, tools, memory, artifacts, gates, and traces.


Exercises

Exercises

Exercise 1: Classify Systems

For each item, decide whether it is a plain LLM call, an agent, a workflow, or a workflow containing an agent. Write a one-sentence justification for each.

  • Summarize this document.
  • Search the web, compare results, and keep going until you find a source.
  • Draft, review, approve, and publish a tutorial.
  • Choose which API to call based on a user request.
  • Check a credit application, score it, route it to an underwriter if high risk,

and archive the result.

  • Answer a customer support question from a knowledge base.

Check your answers in answers.md.

Exercise 2: Design A Tiny Workflow

Write a three-step workflow for planning a study session. Use this format:

node_id -> node_id -> node_id

Then add artifact names:

node_id → produces: artifact_name
node_id (depends on: artifact_name) → produces: artifact_name
node_id (depends on: artifact_name) → produces: artifact_name

Expected output: a small table with three rows — one per node — listing id, dependency, and artifact.

Exercise 3: Identify The Agent Loop

Run the gated workflow:

python3 -m src.mini_meshflow run examples/03_agent_with_gate.json

Find the node that performs the most open-ended reasoning. Answer:

  • What task does it receive?
  • What artifact does it produce?
  • Could a simpler prompt node replace it, or does it need the reasoning loop?

Exercise 4: Add A Stopping Condition

Take the workflow you designed in Exercise 2 and add a stopping condition:

  • What event signals the workflow is done?
  • What would cause the workflow to stop early (fail or block)?
  • Write one sentence for each condition.

Example format:

Done when:    final_answer artifact is produced and quality_score > 0.8
Stopped when: research_step fails OR budget_exceeded gate blocks

Exercise 5: Write The Agent Loop In Plain Language

Without using code, describe in five steps what the agent loop does inside the research_agent node in the gate example:

Step 1: receive ___
Step 2: reason about ___
Step 3: call tool ___ with input ___
Step 4: observe ___
Step 5: produce ___ and stop because ___

This exercise builds the habit of reasoning about what an agent is actually doing before you wire it into a workflow.