← Home · Tutorial 10 · Agents

An agent is prompts joined by logic and memory

A plain definition of an agent, and the chain that leads into one.

Last reviewed 5 October 2026 · Written with AI assistance and reviewed by me

Where I learned this: the mental model comes from my notes on DeepLearning.AI's ChatGPT Prompt Engineering for Developers (Isa Fulford and Andrew Ng), which ends with chaining prompts and building a small chatbot. The code and framing are my own, and the loop at the end is checked against Anthropic's tool-use documentation.
Who this is for: anyone who keeps hearing the word agent and wants a definition they can test. You will learn the three parts of an agent, how a chain differs from one, and where agents go wrong.

1Purpose first

A definition you can test any product against.

"Agent" is used loosely. This page gives a plain definition you can test any product against: an agent is prompts joined by logic and memory.

Each of the three parts is something you already know. A prompt is a call to a model. Logic is ordinary code that decides what happens next. Memory is whatever you carry from one step to the next. Join them and you have a system that can do more than one reply can.

Why it matters: once you see the three parts, an agent stops being a mystery. You can ask of any system which part does what, and where it can go wrong.

2The three parts

Prompts, logic and memory.

The three parts
Prompta model call
→
Logiccode decides next step
→
Promptanother model call
→
Answeror an action

Underneath all of it sits memory: the values passed along the chain. Without it, every step starts blank.

PartWhat it isWhere it lives
PromptsInstructions plus input sent to a modelStrings in your code
LogicIf/else, loops, calling a function or APIYour program, not the model
MemoryWhat earlier steps producedA variable, a file or a database

3A chain you can run

One prompt feeding the next.

The simplest version is a chain: the output of one prompt becomes the input of the next. The model call below is a stand-in so the code runs anywhere. Swap it for a real API call (see tutorial 2).

Python
def call_model(prompt):
    # stand-in for a real API call, so this runs anywhere
    if "Classify" in prompt:
        return "billing" if "invoice" in prompt else "other"
    return "Thanks for flagging this. Our billing team will correct it within two working days."

state = {"message": "My invoice shows the wrong amount."}

# step 1: a prompt that decides the route
state["category"] = call_model(f"Classify this message as billing or other: {state['message']}")

# step 2: ordinary code branches on the result
if state["category"] == "billing":
    # step 3: a second prompt, using what step 1 produced
    state["reply"] = call_model(f"Write a short reply to this billing message: {state['message']}")
else:
    state["reply"] = "Passed to a human."

print(state["category"])
print(state["reply"])
Output (run to check)
billing
Thanks for flagging this. Our billing team will correct it within two working days.

Notice what the model did and did not do. It classified, and it wrote. It did not decide to branch. The if statement did that, in code you can read and test.

4Why chains help

Smaller jobs, easier fixes.

Splitting the work this way has real advantages over one giant prompt.

Why chains help

  • Each prompt does one job, so each is easier to get right
  • You can test and fix one step at a time
  • Code handles the branching, so it is predictable
  • You can use a cheaper model for simple steps

What they cost

  • More calls, so more time and money
  • A mistake early on is passed to every later step
  • More moving parts to keep in sync

5From chain to agent

Letting the model choose the step.

A chain follows a path you wrote. An agent, in the usual sense, lets the model choose the next step. You give it tools, it asks to use one, your code runs it, and the result goes back in, repeating until it answers.

The agent loop
Modeldecides
→
Tool callasks for an action
→
Your coderuns it
→
Resultadded to memory
↺
Python
def fake_model(history):
    # asks for a tool once, then answers
    if not any(m["role"] == "tool" for m in history):
        return {"action": "get_weather", "input": {"city": "Delhi"}}
    return {"action": "answer", "text": "It is 31C with haze in Delhi."}

def get_weather(city):
    return {"Delhi": "31C haze"}.get(city, "unknown")

tools = {"get_weather": get_weather}
history = [{"role": "user", "content": "Weather in Delhi?"}]

for step in range(5):                      # a hard cap: never loop forever
    out = fake_model(history)
    if out["action"] == "answer":
        print("ANSWER:", out["text"]); break
    result = tools[out["action"]](**out["input"])
    history.append({"role": "tool", "content": result})
    print("step", step + 1, "called", out["action"], "->", result)
Output (run to check)
step 1 called get_weather -> 31C haze
ANSWER: It is 31C with haze in Delhi.

The model here is faked, but the shape is real: a loop, a set of tools your code controls, memory that grows, and a hard cap on steps. Tutorial 9 shows the real version with the Claude API.

6Where it goes wrong

Compounding errors and runaway loops.

Errors compound. If each step is right 95% of the time, ten steps in a row are right only about 60% of the time (0.95 to the power 10). Short chains and checks between steps matter more than clever prompts.
Always cap the loop. An agent with no step limit can run, and bill, for a long time. Limit steps, limit spend, and limit what the tools are allowed to do.

Rule of thumb: use a plain chain when the steps are known in advance, and an agent only when the path genuinely depends on what the model finds along the way.

7Check yourself

Short questions and one line to remember.

What are the three parts of an agent?

Prompts (model calls), logic (code that decides what happens next) and memory (what is carried between steps).

What is the difference between a chain and an agent?

In a chain, your code fixes the path. In an agent, the model chooses the next step from the tools you gave it.

Why cap the number of steps?

So a confused or looping agent cannot run forever or run up cost.

One line to remember: "An agent is just prompts joined by logic and memory, so the skill is deciding which part should do which job."
Try it yourself: take a task you do in two or three steps, write each step as a prompt, and join them with an if. Only then ask whether it needs to be an agent.
Notes based on DeepLearning.AI’s ChatGPT Prompt Engineering for Developers by Isa Fulford and Andrew Ng, with the tool-use loop checked against Anthropic’s tool-use documentation. Independent notes, not affiliated with or endorsed by any company or course named here. Examples and wording are my own.