C O M P L E T E G U I D E · B U I L D A N A I A G E N T F R O M S C RATC H
Step 0
What an Agent Actually
0
Is
The one idea the whole field rests on, taken slowly and from
every angle. No code — just the mental model, rock solid.
Complete, exhaustive walkthrough
Build an AI Agent from Scratch Step 0 of 13
The only step with no code. Its entire job is to plant one idea so firmly that the other thirteen
steps feel like small, obvious additions. If you only ever truly understand one page of this whole
curriculum, make it this one.
Table of contents
1. The one idea, stated plainly
2. Why this matters before any code
3. The loop — the full picture
4. The four turns, one at a time
5. Who does what: the model vs. your code
6. Why agents exist (the problem they solve)
7. Agent vs. everything it gets confused with
8. The autonomy spectrum
9. State, history, and context — the agent's short-term memory
10. The decision at the center: "act or finish?"
11. Three analogies that make it click
12. Worked examples — full loop traces, no code
13. Types of agent behaviour you'll meet later
14. When to build an agent — and when not to
15. Common misconceptions, debunked
16. The vocabulary of Step 0
17. How every later step plugs into this loop
18. Mental models to carry forward
19. Pitfalls and gotchas
20. Self-test (with answers)
21. What "done with Step 0" means
1. The one idea, stated plainly
An agent is a loop.
That is the whole secret of the field. Strip away every brand name — LangGraph, CrewAI,
AutoGPT, "agentic AI," "autonomous agents" — and underneath all of them is the same small
loop. Once you can see it, you cannot un-see it, and everything else in this curriculum becomes
assembly.
The single-sentence definition to memorise:
Step 0 — What an Agent Actually Is 2
A plain language model answers once. An agent answers, acts, sees what happened,
and decides what to do next — over and over — until the goal is met.
The difference between the two is not intelligence. The exact same model can power both.
The difference is the structure you wrap around it:
• A plain call is a straight line: question in → answer out. One shot.
• An agent is a circle: the model's output becomes the next input, again and again, until the
model itself decides the job is done.
That circle — the feedback loop — is what lets a system take multiple actions, react to
surprises, recover from errors, and finish a task instead of merely describing one.
2. Why this matters before any code
People rush to frameworks and copy-paste an agent they don't understand. Then the agent
misbehaves and they have no idea why, because they're debugging a black box they never
opened.
You will build the loop by hand (Steps 1–4) on purpose. By the time you open a real
framework at Step 11, every node, edge, and callback in it will map onto something you wrote
yourself. Frameworks save typing, not understanding.
So Step 0 has exactly one deliverable: you can explain the loop and point to it in any
system you meet. No code. Just the mental model, rock solid.
Step 0 — What an Agent Actually Is 3
3. The loop — the full picture
┌────────────────────────────────────────────────────┐
│ │
│ 1. LLM gets the GOAL + everything that happened │
│ │ │
│ ▼ │
│ 2. LLM DECIDES: act, or finish? │
│ ┌────┴─────┐ │
│ ▼ ▼ │
│ "call a "I'm done — │
│ tool" here's the answer" ───────▶ EXIT │
│ │ │
│ ▼ │
│ 3. YOUR CODE runs the real action │
│ (call an API, read a file, search, compute) │
│ │ │
│ ▼ │
│ 4. The RESULT is appended to the history │
│ │ │
└──────────────┘ ────────▶ back to turn 1 │
Read it as a circle, not a list. The only way out is turn 2 choosing "finish." Everything else
loops back.
4. The four turns, one at a time
Every later step in this guide attaches to one of these four points, so it pays to know them
cold.
Turn 1 — The model gets the goal and the history. On the first pass this is just the user's
request plus your instructions (the system prompt). On every later pass it also contains
everything that has happened so far: which tools ran and what they returned. This growing
record is the conversation history, and keeping/managing it is the seed of memory (Step 5).
Turn 2 — The model decides: act or finish. This single decision is the beating heart of an
agent. Either the model wants to take an action ("call tool X with these arguments") or it
believes the goal is met ("here is the final answer"). For your program to use this decision, it
has to come back in a machine-readable form — which is exactly why controlling the
output / strict JSON (Step 2) comes before tools.
Turn 3 — Your code runs the real action. The model said what to do; now your program
actually does it — calls the weather API, reads the file, runs the database query, performs the
search. This is tool calling (Step 3), the core skill of the entire field. Without this turn, the
model is just talking to itself.
Turn 4 — The result goes back in, and the loop repeats. The action's result is appended
to the history, and control returns to turn 1. The model now sees what happened and decides
Step 0 — What an Agent Actually Is 4
again. Wrapping turns 1–4 in a while loop is literally Step 4 — the agent loop. Nothing
more exotic than that.
5. Who does what: the model vs. your code
This is the single most clarifying distinction in the whole topic, and the one beginners most
often get wrong.
The model (LLM) Your code
Produces Text only — including text that says "call Real effects in the world
tool X"
Can it touch the No. It never calls an API or reads a file. Yes. It performs every action.
world?
Role Decision-maker / brain Hands / executor
Trust boundary Suggests Decides what to actually allow and
run
The model proposes; your code disposes.
The model only ever emits a string. When it "calls a tool," what really happens is: it outputs
text that describes a tool call, your code parses that text, your code decides whether to run it,
your code runs it, and your code feeds the result back. This boundary is where all of your
safety, permissions, and control live. Keep it crisp in your head and most agent confusion
evaporates.
6. Why agents exist (the problem they solve)
A plain LLM has two hard limits:
1. It can't act. It can describe how to check the weather but can't actually check it. It's a
brain with no hands.
2. It answers in one shot. It must produce the whole answer immediately, with no chance to
look something up, try something, see the result, and adjust.
Huge classes of real tasks need both — multiple actions, each depending on what the previous
one returned. "Book me the cheapest flight under $400 next Friday" requires searching,
comparing, maybe re-searching, then booking. No single prediction can do that reliably.
The loop solves both limits at once: tools give the model hands (turn 3), and the repetition
gives it the chance to observe and adjust (turns 4 → 1 → 2). That is the entire reason agents
exist.
Step 0 — What an Agent Actually Is 5
7. Agent vs. everything it gets confused with
Can it act on Who chooses the
System Shape Is there a loop?
the world? next step?
Plain LLM call question → answer No No (text only) N/A
Chatbot many turns Loop, but the No (text only) The human
human closes it
Fixed workflow / A → B → C, No (path fixed in Yes, but on rails You, at coding time
chain hardcoded advance)
RPA / script predetermined No Yes You, at coding time
automation
Agent loop Yes Yes The model, at run
time
Two distinctions are worth saying out loud:
• Workflow vs. agent. A workflow's path is decided by you at coding time; an agent's path is
decided by the model at run time. That's the whole difference. Workflows are predictable;
agents are flexible. Real systems often mix both.
• Chatbot vs. agent. In a chatbot the human reads each reply and types the next message
— the human is the loop. In an agent the model closes the loop itself, acting and reacting
with no human between turns.
8. The autonomy spectrum
"Agent" isn't binary; it's a dial. From least to most autonomous:
1. Plain call — no loop, no tools.
2. Single tool call — the model can call one tool once, then answers.
3. Tool loop (basic agent) — the model calls tools repeatedly until done. (This is what you
build by Step 4.)
4. Planning agent — the model writes a plan first, then executes it (Step 7).
5. Reflecting agent — the model critiques and retries its own work (Step 8).
6. Multi-agent — several agents, each with a role, coordinating.
More autonomy buys flexibility at the cost of predictability, speed, and cost. The skill is
choosing the least autonomy that still solves the task.
Step 0 — What an Agent Actually Is 6
9. State, history, and context — the agent's short-term
memory
The loop needs a memory of what's happened, or every turn would start from scratch. That
memory is just a growing list of messages — typically:
• a system message (the agent's instructions / role),
• the user message (the goal),
• the model's assistant messages (its decisions),
• tool messages (the results your code fed back).
Each turn, you resend this whole list so the model sees the full story. This is short-term
memory in its simplest form. Two practical truths follow:
• The list grows every turn, and the model has a context window (a maximum size). Long
runs eventually overflow it, which is why Step 5 is about trimming or summarising history.
• Long-term memory (facts that outlive one run) is a different thing — stored externally and
retrieved when needed. That's what RAG (Step 9) becomes.
For Step 0, the only thing to internalise: the history is the state, and resending it is how
the model "remembers" within a run.
10. The decision at the center: "act or finish?"
Turn 2 is where the magic and the danger both live. On every pass the model makes one of
two choices:
• Act — "call tool X with arguments Y." The loop continues.
• Finish — "the goal is met; here is the final answer." The loop exits.
Everything that makes an agent good is really about making this decision better:
• Routing (Step 6) helps it pick the right kind of action up front.
• Planning (Step 7) gives it a roadmap so each decision is informed.
• Reflection (Step 8) lets it second-guess a "finish" that wasn't actually good enough.
And the classic failure modes are all decision failures: looping forever (never choosing
"finish"), finishing too early (a wrong answer), or calling the wrong tool. Knowing that these
are decision problems tells you where to fix them.
11. Three analogies that make it click
The new employee. You tell a capable new hire: "find out why last month's sales dipped."
They don't answer instantly. They pull a report (act), read it (observe), spot a gap, query the
database (act), read the result (observe), then write the summary (finish). At each step they
Step 0 — What an Agent Actually Is 7
look at what they just learned and decide the next move. That act–observe–decide cycle is the
agent loop. A plain LLM is the employee forced to answer the instant they hear the question,
with no chance to look anything up.
The thermostat. It senses the temperature, compares it to the goal, acts (heat/cool), then
senses again — forever, until the goal holds. It's the simplest possible goal-seeking loop. An
agent is the same shape, but "sensing" is reading tool results, "acting" is calling tools, and the
goal is written in language instead of a number.
The detective. A detective doesn't solve a case in one thought. They gather a clue, form a
hypothesis, test it (act), see what it reveals (observe), revise, and continue until the picture is
complete. The loop is the investigation.
12. Worked examples — full loop traces, no code
Example A — a chained question. Goal: "What's the population of the capital of the country
that won the most recent World Cup?"
Turn 1 model: "I don't know the latest winner. ACT: search('most recent World Cup
winner')"
code runs search → "Country X"
Turn 2 model: "Now its capital. ACT: search('capital of Country X')"
code runs search → "City Y"
Turn 3 model: "Now the population. ACT: search('population of City Y')"
code runs search → "Z million"
Turn 4 model: "FINISH: The capital is City Y, with about Z million people."
No single model call could answer this reliably — it's three lookups, each depending on the
last. The loop is what makes the chain possible.
Example B — recovering from an error. Goal: "Email the Q3 report to Sarah."
Turn 1 model: "ACT: find_file('Q3 report')"
code → returns "no file named exactly that; closest: '[Link]'"
Turn 2 model: "ACT: find_contact('Sarah')"
code → "two matches: Sarah Lee, Sarah Kim"
Turn 3 model: "Ambiguous. FINISH: Which Sarah, and is '[Link]' the right file?"
Here the loop lets the agent react to imperfect results — it asks instead of guessing. A one-
shot model couldn't have discovered the ambiguity.
Example C — knowing when not to loop. Goal: "What's 2 + 2?"
Turn 1 model: "FINISH: 4."
A good agent takes zero actions when none are needed. The loop runs once and exits. Using
tools you don't need is itself a mistake.
Step 0 — What an Agent Actually Is 8
13. Types of agent behaviour you'll meet later
These are patterns layered on the same loop, not different things:
• ReAct (reason + act) — the model writes a short reasoning note, then an action,
repeatedly. The default style you'll build.
• Planning — produce a full step list first, then execute it (Step 7).
• Reflection / self-critique — answer, critique, revise (Step 8).
• Routing — classify the request first, then dispatch to the right tools (Step 6).
• Multi-agent — several specialised agents passing work between them.
Every one of these is just "how turn 1 is framed" or "an extra loop around the loop." None
changes the fundamental shape.
14. When to build an agent — and when not to
Building an agent is a choice, and more autonomy is not automatically better.
Use a loop/agent when: - the task needs multiple actions whose order you can't know in
advance, - later steps depend on what earlier steps return, - the model genuinely needs to
decide the path.
Prefer a fixed workflow (no agent) when: - you already know the exact sequence of steps, -
predictability, speed, and low cost matter more than flexibility, - the task is a single
transformation (just call the model once).
A fixed workflow is easier to test, cheaper, and more predictable. Knowing when not to reach
for an agent is part of understanding what one is.
15. Common misconceptions, debunked
• "An agent is a smarter kind of model." No — it's the same model. "Agent" describes the
loop around it, not the brain inside.
• "You need a framework to build an agent." No — the whole loop is ~30 lines of plain
Python (you'll write it by Step 4). Frameworks add convenience and production features,
not the concept.
• "The model executes the actions." No — the model only emits text describing an action;
your code performs it. That boundary is where control lives.
• "More autonomy is always better." No — autonomy trades predictability for flexibility.
Choose the least autonomy that solves the task.
• "Agents and RAG are separate worlds." No — RAG is just one tool the loop can call
("retrieve relevant text"). It slots into turn 3 like any action.
• "Agents are magic / mysterious." No — once you see the loop, an agent is one of the
most mechanical things in ML.
Step 0 — What an Agent Actually Is 9
16. The vocabulary of Step 0
• Agent — a loop where an LLM repeatedly decides an action, your code runs it, and the
result feeds back, until the goal is met.
• Loop — the repeating cycle (turns 1–4) that defines an agent.
• Turn / iteration — one pass through the loop.
• Tool — a function your code exposes that the model can ask to run.
• Tool call — the model's text output requesting a specific tool + arguments.
• History / messages / context — the growing record of the run, resent each turn.
• System prompt — the instructions/role given to the model.
• Context window — the maximum amount of text the model can consider at once.
• Goal — what the agent is trying to achieve; the loop runs until it's met.
• Act vs. finish — the two outcomes of the model's decision each turn.
• Autonomy — how much the system decides for itself vs. follows a fixed path.
17. How every later step plugs into this loop
Step What it adds Where it attaches
1–2 · Call & control output A model call that returns parseable Makes turn 2 readable by
decisions code
3 · Tool calling The model can trigger real functions Turn 3 (the action)
4 · The agent loop The while wrapper itself The loop
5 · Memory What history to keep and resend Turn 1 (the input)
6 · Routing Pick the right path/tools up front A decision before turn 2
7 · Planning Write a step list, then execute it Improves turn 2's decisions
8 · Reflection Critique the answer and retry A loop around the loop
9 · RAG A "retrieve knowledge" tool Turn 3 (a special action)
10 · Evaluation Prove the loop actually works Around the whole system
11–13 · Framework, local, Convenience, privacy, production- The same loop, dressed up
enterprise readiness
Step 0 — What an Agent Actually Is 10
18. Mental models to carry forward
• The golden rule: for any system you meet, ask "where is the loop, and what is it
deciding?" If you can answer that, you understand it.
• Brain vs. hands: the model is the brain (decides), your code is the hands (acts). They are
never the same thing.
• A circle, not a line: if there's no feedback edge from output back to input, it isn't an
agent.
• Everything later is decoration: memory, routing, planning, reflection, RAG — each just
improves one of the four turns.
19. Pitfalls and gotchas
• Infinite loops — the model never chooses "finish." Always add a max-turns cap. (You'll see
this in Step 4.)
• Finishing too early — the model declares victory with a wrong answer. Reflection (Step 8)
and evaluation (Step 10) catch this.
• Calling the wrong tool / bad arguments — usually fixed with clearer tool descriptions
and better structured output (Steps 2–3).
• Context overflow — history grows past the window on long runs (Step 5).
• Over-engineering — building an agent where a single model call or a fixed workflow
would do. Match the autonomy to the task.
• Blurring the boundary — assuming the model "did" something when it only asked your
code to. Always trace what actually executed.
20. Self-test (with answers)
Don't move on until you can answer all of these without hesitating.
Q1. In one sentence, how does an agent differ from a plain LLM call? A plain LLM
answers once; an agent answers, acts, observes the result, and decides what to do next,
repeating until the goal is met.
Q2. Name the four turns of the loop, in order. (1) Model gets goal + history → (2) model
decides act or finish → (3) your code runs the action → (4) result is appended to history;
repeat.
Q3. Who actually performs the actions — the model or your code? Your code. The model
only outputs text describing the action.
Q4. Give one task that needs a loop and one that doesn't. Needs a loop: "book the
cheapest flight under $400 next Friday" (search → compare → book, each step depends on the
last). Doesn't: "what's 2 + 2?" (one shot).
Step 0 — What an Agent Actually Is 11
Q5. What ends the loop? Turn 2 choosing "finish" (the model judges the goal met) — or a
safety cap on the number of turns.
Q6. Where does a framework like LangGraph fit? It's convenience over this exact loop —
nodes and edges that map to turns and decisions you can already build by hand. It saves
typing, not understanding.
Q7. Apply the golden rule to a chatbot: where's the loop and what decides it? The loop
exists across turns, but the human closes it by reading each reply and typing the next message
— so it's not an autonomous agent.
21. What "done with Step 0" means
You're finished with Step 0 — no code required — when all of this is true:
• [ ] You can state the one-sentence definition from memory.
• [ ] You can draw or describe the four-turn loop.
• [ ] You can say who decides vs. who acts (model vs. your code).
• [ ] You can name a task that needs an agent and one that doesn't.
• [ ] You can apply the golden rule — "where is the loop, what is it deciding?" — to any
system.
When those are second nature, move to Step 1, which turns turn 1 of this diagram into
running code: a Python script that sends a prompt to your local Ollama model and reads the
reply. From there you build outward — one turn at a time — until the full loop is alive at Step
4.
Step 0 of the "Build an AI Agent from Scratch" curriculum. The concept is the foundation;
everything else is construction on top of it.
Step 0 — What an Agent Actually Is 12