In the last post we said an agent is just a language model plus tools plus a loop. That sounds like something you'd need a team and a framework to build. You don't. You need three things: the model, a way to run a small piece of code, and a loop you can write in about a dozen lines. This post gets you from nothing to understanding the exact shape of that loop, using Claude.
Step 1: Talk to the intern
Before you build an agent, build a chatbot. It's one function call, and it makes the later steps obvious by contrast. First install the one library you need:
pip install anthropicThen send a single message and print the reply:
import anthropic
client = anthropic.Anthropic() # reads your API key from the environment
response = client.messages.create(
model="claude-opus-5",
max_tokens=1024,
messages=[
{"role": "user", "content": "In one sentence, what is an AI agent?"}
],
)
# The reply comes back as a list of blocks; print the text ones.
print("".join(block.text for block in response.content if block.type == "text"))Run it and Claude answers your question. That's the whole program. It's also a perfect example of what an agent is not: it responded once and stopped. It couldn't look anything up, couldn't take an action, and had no way to keep going. It's an intern who can only talk. To make it act, we add a loop.
Step 2: Give the intern a loop
Here's the shape of every agent you'll ever build. Read it once for the structure, not the details. The comments do the explaining:
messages = [{"role": "user", "content": "your goal for the agent"}]
while True:
response = client.messages.create(
model="claude-opus-5",
max_tokens=1024,
tools=tools, # the tools the intern is allowed to use
messages=messages,
)
if response.stop_reason == "tool_use":
# The intern asked to use a tool. Run it, hand back the result,
# and let the loop go around again.
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "user", "content": run_the_requested_tools(response)})
continue
# No tool needed. The intern is done. Print the answer and stop.
print("".join(b.text for b in response.content if b.type == "text"))
breakThat while-loop is what separates a chatbot from an agent. The one line that matters is stop_reason. When Claude decides it needs a tool, it doesn't make one up. It stops and tells you, in a structured way, "I want to run this tool with these inputs." Your code runs the tool, hands the result back, and the loop goes around again. Claude reads the result, decides the next step, and either asks for another tool or gives you the final answer. You're not writing the intern's to-do list. You're just running the tools it asks for and letting it keep going.
- 1
Ask
You give the model a goal and the list of tools it's allowed to use.
- 2
Decide
The model either answers directly or says it wants to use a tool. That signal is the stop_reason.
- 3
Run and return
If it wants a tool, your code runs it and hands the result back to the model.
- 4
Repeat
The model reads the result and picks the next step. The loop continues until it has an answer, then stops.
People expect the hard part of an agent to be some clever algorithm. It's a while-loop and one if statement.
Step 3: Keep yourself in the loop
There's a fourth ingredient the diagrams leave out, and it's you. An agent will do exactly what its tools allow, as many times as the loop runs, without pausing to wonder whether it should. That's fine when the tools only read information. It matters a lot the moment a tool can send, delete, buy, or change something. Before you hand an intern the keys, you decide what they're allowed to do unsupervised.
- Start with read-only tools. A tool that looks something up can't break anything. Earn your way up to tools that take action.
- Gate the risky steps. For anything hard to undo, have the loop stop and ask you to approve before it runs.
- Cap the loop. Add a simple counter so a confused agent stops after, say, ten turns instead of spinning forever.
- Read the trace. Print each step while you learn. Watching the intern work is how you learn to trust it, or not.
Key takeaways
- A chatbot is one API call. An agent is that same call wrapped in a loop.
- The whole agent lives in stop_reason: when the model wants a tool, it stops and asks, your code runs it, and the loop continues.
- You don't script the steps. You supply the goal and the tools, and the model decides the order.
- You are the fourth ingredient. Start read-only, gate risky actions, and cap the loop while you're learning.
Right now our loop has a hole in it: the tools list is empty and run_the_requested_tools does nothing. That's on purpose. A loop with no tools is just a chatbot with extra steps. Next post, we hand the intern its first real tool.



