SyntaxPilot
Courses/AI: Zero to Agents/Lesson 26

AI: Zero to Agents · Lesson 26 of 40 · 11 min

What is an AI agent?

queued

These are the notes for AI episode 26. Until now our chai shop bot could only answer. Today it learns to do things. You'll see why a language model on its own can only write text (so "Done, booked!" can be a lie), how a tool lets the model ask while your code acts, what a workflow is, and how letting the model choose its own next step in a loop turns it into an AI agent. Then you'll add a stopping condition, a human checkpoint, and learn when not to use an agent. Every Python example runs as-is in the SyntaxPilot compiler; outputs and errors are real (Python 3.13). The "models" in the code are toys made of if-rules, with the same shape as the real thing.

Real-life example: Priya wants a table tonight

What happened In AI
Priya messages the bot: "book a table for 4 at 8 pm" a user gives a goal
The bot replies "Done! Booked." the model writes text that sounds like an action
At 8 pm there's no booking nothing in the environment changed: a hallucinated action
A real assistant picks up the phone, checks, books, reports back an agent: it uses tools and checks the results

Problem 1: the bot only talks

A language model (LLM) takes text in and produces text out. That's all. It has no hands: it can't write to a database, call an API, or pick up a phone.

bookings = []                       # the restaurant's real booking list

def chatbot(message):               # toy LLM: it can only write text
    if "book" in message.lower():
        return "Done! Your table for 4 at 8 pm is booked."
    return "Sorry, I don't know."

print("Bot:", chatbot("Please book a table for 4 at 8 pm"))
print("Bookings:", bookings)        # what really happened?

Output:

Bot: Done! Your table for 4 at 8 pm is booked.
Bookings: []

The bot says "Done!", and the booking list is still empty. When a model says it did something, it's only predicting the words a helpful reply would contain. Claiming an action that never happened is one of the most dangerous kinds of hallucination.

Real-life example: a manager and an office helper

In an office In an AI app
The manager decides what should happen the model decides which tool to use, with what inputs
She writes a request slip: "book table, 4 people, 8 pm" a tool call: a tool name plus arguments, as data
The helper actually picks up the phone your code runs the real function: the tool
The helper reports "Booked, table 7" the tool result: what really happened

The fix: a tool

A tool is an ordinary function in your program that the model is allowed to ask for. The model replies with a request; your code reads the request and runs the function.

bookings = []
def book_table(people, time):       # a TOOL: real code that acts
    bookings.append((people, time))
    return f"booked {people} at {time}"

def model(message):                 # toy LLM: replies with a REQUEST
    if "book" in message.lower():
        return {"tool": "book_table", "args": {"people": 4, "time": "20:00"}}
    return {"text": "Sorry, I don't know."}

reply = model("Please book a table for 4 at 8 pm")
print("Model asks:", reply)
result = book_table(**reply["args"])   # OUR code runs the tool
print("Result:", result)
print("Bookings:", bookings)

Output:

Model asks: {'tool': 'book_table', 'args': {'people': 4, 'time': '20:00'}}
Result: booked 4 at 20:00
Bookings: [(4, '20:00')]

This time the booking list really contains the booking. The line book_table(**reply["args"]) is the key: the model asks, your code acts.

Real models work exactly like this. On 13 June 2023 OpenAI announced function calling: developers describe functions to the model, and the model can choose to output "a JSON object containing arguments to call those functions". The model never runs anything itself. (Episode 28 builds this properly, with JSON schemas.)

Problem 2: a fixed workflow gets stuck

Booking blindly is risky, so we write a smarter path: check first, then book.

tables = {"20:00": 0, "21:00": 0, "22:00": 3}   # free tables per time
def check(time): return f"{tables[time]} free at {time}"
def book(time):
    tables[time] -= 1
    return f"booked at {time}"

def workflow(time):                # a FIXED path, written by us
    if check(time).startswith("0"):
        return "Sorry, 8 pm is full."
    return book(time)

print(workflow("20:00"))
# Customer: "8 pm, or any time later tonight."
# 9 pm? 10 pm? Phone the next place? More and more if-else...

Output:

Sorry, 8 pm is full.

Correct, 8 pm is full. But Priya said "8 pm, or any time later tonight". Try 9? 10? Another branch? Every variation needs more if/else written by you, in advance.

This is a workflow. Anthropic's guide Building effective agents (19 Dec 2024) defines it as a system "where LLMs and tools are orchestrated through predefined code paths". Workflows are great when you know the steps, and stuck when you don't.

Workflow vs agent

Workflow Agent
Who picks the next step? you, in code the model, at run time
Steps same every time depend on what happens
Good for known, repeatable tasks open-ended goals
Cost and speed cheaper, faster, predictable more model calls: slower, costlier
Testing easy harder

The same guide defines agents as "systems where LLMs dynamically direct their own processes and tool usage".

The fix: the agent loop

Give the model the goal, the tools, and the history of what happened so far, and let it choose only the next step. Run that step, record the result, and ask again.

tables = {"20:00": 0, "21:00": 0, "22:00": 3}
def check(time): return f"{tables[time]} free at {time}"
def book(time):
    tables[time] -= 1
    return f"booked at {time}"
TOOLS = {"check": check, "book": book}

def model(goal, history):          # toy LLM: picks the NEXT step
    if not history: return ("check", "20:00")
    tool, arg, result = history[-1]
    if result.startswith("0 free"):            # full? one hour later
        return ("check", f"{int(arg[:2]) + 1}:00")
    if tool == "check": return ("book", arg)
    return ("done", result)

goal = "Table for 4 at 8 pm, or later if it's full"
history = []
while True:                        # the agent loop
    tool, arg = model(goal, history)
    if tool == "done":
        print("Agent:", arg); break
    result = TOOLS[tool](arg)      # act...
    print(f"{tool}({arg}) -> {result}")
    history.append((tool, arg, result))   # ...and observe

Output:

check(20:00) -> 0 free at 20:00
check(21:00) -> 0 free at 21:00
check(22:00) -> 3 free at 22:00
book(22:00) -> booked at 22:00
Agent: booked at 22:00

We never wrote "try 9, then 10". The model chose every step by looking at the real result of the last one. Anthropic calls these results ground truth from the environment, and describes agents as "typically just LLMs using tools based on environmental feedback in a loop".

        +--------+      +-------+      +---------+
goal -> | DECIDE | ---> |  ACT  | ---> | OBSERVE | --+
        +--------+      +-------+      +---------+   |
            ^      (model)   (your code)  (result)    |
            +-------------------------------------------+
                      repeat until "done"

What makes it an agent?

  • Goal: what the user wants done ("a table for 4, 8 pm or later").
  • Tools: functions the model can ask for (check, book).
  • Loop: decide → act → observe → repeat.
  • Environment: the world the tools touch (the booking list, a database, files, a website).
  • Autonomy: the model chooses its own steps instead of you writing them in advance.

AI agent = model + tools + loop + goal. No magic.

Problem 3: a busy Saturday

tables = {"20:00": 0, "21:00": 0, "22:00": 0}
def check(time): return f"{tables[time]} free at {time}"
def book(time):
    tables[time] -= 1
    return f"booked at {time}"
TOOLS = {"check": check, "book": book}

def model(goal, history):          # toy LLM: picks the NEXT step
    if not history: return ("check", "20:00")
    tool, arg, result = history[-1]
    if result.startswith("0 free"):            # full? one hour later
        return ("check", f"{int(arg[:2]) + 1}:00")
    if tool == "check": return ("book", arg)
    return ("done", result)

goal = "Table for 4 at 8 pm, or later if it's full"
history = []
while True:                        # the agent loop
    tool, arg = model(goal, history)
    if tool == "done":
        print("Agent:", arg); break
    result = TOOLS[tool](arg)      # act...
    print(f"{tool}({arg}) -> {result}")
    history.append((tool, arg, result))   # ...and observe

Output:

check(20:00) -> 0 free at 20:00
check(21:00) -> 0 free at 21:00
check(22:00) -> 0 free at 22:00
Traceback (most recent call last):
  File "main.py", line 22, in <module>
    result = TOOLS[tool](arg)      # act...
  File "main.py", line 2, in check
    def check(time): return f"{tables[time]} free at {time}"
                               ~~~~~~^^^^^^
KeyError: '23:00'

Everything is full, so the model keeps trying later times until it asks for 23:00, which doesn't exist: KeyError: '23:00'. The loop's only exit is "done"; an impossible goal never gets there. With a real model the loop could run for ever, and every turn is a paid model call.

The fix: a stopping condition and a human hand-off

tables = {"20:00": 0, "21:00": 0, "22:00": 0}   # a busy Saturday
def check(time):
    if time not in tables: return f"error: no slot {time}"
    return f"{tables[time]} free at {time}"
def book(time):
    tables[time] -= 1
    return f"booked at {time}"
TOOLS = {"check": check, "book": book}

def model(goal, history):
    if not history: return ("check", "20:00")
    tool, arg, result = history[-1]
    if result.startswith(("0 free", "error")):
        return ("check", f"{int(arg[:2]) + 1}:00")
    if tool == "check": return ("book", arg)
    return ("done", result)

MAX_STEPS = 5                      # stopping condition
history = []
for step in range(1, MAX_STEPS + 1):
    tool, arg = model("Table for 4, 8 pm or later", history)
    if tool == "done":
        print("Agent:", arg); break
    result = TOOLS[tool](arg)
    print(f"step {step}: {tool}({arg}) -> {result}")
    history.append((tool, arg, result))
else:
    print("Agent: stopped after", MAX_STEPS, "steps. Asking a human.")

Output:

step 1: check(20:00) -> 0 free at 20:00
step 2: check(21:00) -> 0 free at 21:00
step 3: check(22:00) -> 0 free at 22:00
step 4: check(23:00) -> error: no slot 23:00
step 5: check(24:00) -> error: no slot 24:00
Agent: stopped after 5 steps. Asking a human.

Two changes: the tool returns errors as text (so the model can see them instead of the program crashing), and a stopping condition (MAX_STEPS) ends the loop and hands over to a person. Anthropic's guide: "it's also common to include stopping conditions (such as a maximum number of iterations) to maintain control", and agents "can then pause for human feedback at checkpoints or when encountering blockers".

The for ... else in Python: the else block runs only when the loop finished without break, which here means "no answer within 5 steps".

Human checkpoints

Kind of tool Examples If it goes wrong Rule
Read-only check tables, search, look up a price nothing is lost let the agent run it
Acts on the world book, pay, send email, delete may not be undoable ask the user to confirm first

OpenAI's function-calling announcement recommends "user confirmation steps before performing actions with real-world impact". Also check what a tool returned (right date? right number of people?) before acting on it. Episode 35 (agent safety) goes deeper.

The building block: an augmented LLM

A new employee An augmented LLM
a smart graduate who knows nothing about your shop the LLM: general knowledge from training
the shop's handbook retrieval (RAG, episode 24)
phone, till, register tools
a notebook memory (like our history list)

Anthropic calls "an LLM enhanced with augmentations such as retrieval, tools, and memory" the basic building block of agentic systems.

When NOT to use an agent

Anthropic's advice is to find "the simplest solution possible, and only increasing complexity when needed". Agents suit "open-ended problems where it's difficult or impossible to predict the required number of steps", but "agentic systems often trade latency and cost for better task performance".

Use a workflow (or a single model call) when... Use an agent when...
the steps are known in advance the goal is open-ended
it's the same task every time the number of steps can't be predicted
it must be fast and cheap it must react to results
it must be easy to test and explain the extra time and cost are worth it

Glossary

Term Meaning Example from this episode
LLM Large language model: predicts text from text our chatbot()
Chatbot An app that only replies with text "Done! Your table is booked."
Hallucinated action The model claims it did something it didn't booking list still []
Tool A function your code runs when the model asks book_table(people, time)
Tool call The model's request: a tool name plus arguments {"tool": "book_table", "args": {...}}
Arguments The inputs of a tool call people=4, time="20:00"
Tool result What the tool returned: what really happened "booked 4 at 20:00"
Function calling Model APIs that let a model output tool calls as JSON OpenAI, June 2023
Workflow Models and tools following steps fixed in your code check → book → or "sorry"
Agent A model that chooses its own next steps using tools in a loop tries 8, 9, 10 pm by itself
Goal What the user wants the agent to achieve "8 pm or later"
Environment The world the tools read or change the tables dictionary
Observation / ground truth The real result the agent sees after acting "0 free at 21:00"
Agent loop Decide → act → observe → repeat the while True: loop
Autonomy How much the model decides by itself it picks the next time slot
Stopping condition A rule that ends the loop MAX_STEPS = 5
Human checkpoint A pause where a person confirms or takes over "Asking a human."
Augmented LLM An LLM plus retrieval, tools and memory the "new employee"
Latency How long the user waits each loop = another model call

Common mistakes

Mistake What you see Why Fix
Trusting a chatbot that says "done" Bookings: [] an LLM alone only writes text give it a tool; your code acts
A loop with no limit KeyError: '23:00', or an endless loop and bill an impossible goal never says "done" MAX_STEPS; return tool errors as text
Real-world actions with no check wrong date booked, money spent nobody confirmed ask the user before book/pay/send
An agent for a fixed task slow, costly, hard to test the steps were known use a workflow or one call

Mini project: a two-branch booking agent

Add a third tool other_branch(time) that checks the other branch of the restaurant. Make the toy model try the other branch at 8 pm before trying later times, stop after 6 steps, and ask for a yes/no before calling book.

Practice

1. Make the agent print how many model calls it used.

Solution
tables = {"20:00": 0, "21:00": 0, "22:00": 3}
def check(time): return f"{tables[time]} free at {time}"
def book(time):
    tables[time] -= 1
    return f"booked at {time}"
TOOLS = {"check": check, "book": book}

def model(goal, history):
    if not history: return ("check", "20:00")
    tool, arg, result = history[-1]
    if result.startswith("0 free"):
        return ("check", f"{int(arg[:2]) + 1}:00")
    if tool == "check": return ("book", arg)
    return ("done", result)

history, calls = [], 0
while True:
    calls += 1
    tool, arg = model("Table for 4, 8 pm or later", history)
    if tool == "done":
        print("Agent:", arg); break
    result = TOOLS[tool](arg)
    history.append((tool, arg, result))
print("model calls:", calls)

Output:

Agent: booked at 22:00
model calls: 5

2. Ask the user before booking. (confirm() stands in for input(); change answer to "yes" to see it book.)

Solution
tables = {"20:00": 0, "21:00": 2}
def check(time): return f"{tables[time]} free at {time}"
def book(time):
    tables[time] -= 1
    return f"booked at {time}"

def confirm(question, answer="no"):   # input() in a real app
    print(question, answer)
    return answer == "yes"

history = []
for time in ["20:00", "21:00"]:
    result = check(time)
    print(result)
    if not result.startswith("0"):
        if confirm(f"Book a table at {time}? (yes/no)"):
            print(book(time))
        else:
            print("Not booked. Nothing was changed.")
        break

Output:

0 free at 20:00
2 free at 21:00
Book a table at 21:00? (yes/no) no
Not booked. Nothing was changed.

3. Is a nightly job that reads every new invoice, extracts the total and saves it to a sheet an agent or a workflow?

Answer

A workflow. The steps are the same every time and known in advance, so fix them in code: cheaper, faster and easier to test.

Quick quiz 1

Every invoice: read it → extract the total → save to a sheet. Agent or workflow?

Answer

B: a workflow. The steps are fixed and known in advance.

Quick quiz 2

model("book a table") returns {'tool': 'book_table', 'args': {...}}. Who runs book_table?

Answer

B: your code. The model only asks; your code runs the function and sends back the result.

Interview questions

  1. What's the difference between a chatbot and an AI agent? A chatbot only returns text. An agent uses tools to act, observes the results, and chooses its next steps in a loop towards a goal.
  2. Workflow vs agent? In a workflow the steps are fixed in code ("predefined code paths"). In an agent the model directs its own process and tool use at run time.
  3. Does the model execute the tool? No. It outputs a tool call (name + arguments); your application runs the function and returns the result.
  4. Why do agents need stopping conditions? An impossible or confusing goal can loop for ever; every iteration costs time and money. Use a maximum number of steps and hand off to a human.
  5. When would you not use an agent? When the steps are predictable: a workflow or a single model call is cheaper, faster and easier to test.

Summary

  • An LLM alone only writes text: "done" can be a hallucinated action.
  • A tool is a function your code runs when the model asks: the model asks, your code acts.
  • A workflow fixes the steps in code; an agent lets the model choose each next step.
  • The agent loop: decide → act → observe → repeat, using ground truth from the environment.
  • Add a stopping condition and human checkpoints before real-world actions.
  • Start simple: agents trade latency and cost for flexibility.

Next: episode 27, the agent loop: how a real model sees the history, thinks, acts and observes, step by step.

Sources: Anthropic, "Building effective agents", 19 Dec 2024 (anthropic.com/research/building-effective-agents); OpenAI, "Function calling and other API updates", 13 Jun 2023 (openai.com/index/function-calling-and-other-api-updates).