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.
2The three parts
Prompts, logic and memory.
Underneath all of it sits memory: the values passed along the chain. Without it, every step starts blank.
| Part | What it is | Where it lives |
|---|---|---|
| Prompts | Instructions plus input sent to a model | Strings in your code |
| Logic | If/else, loops, calling a function or API | Your program, not the model |
| Memory | What earlier steps produced | A 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).
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"])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.
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)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.
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.
if. Only then ask whether it needs to be an agent.