“Agent” is the most hyped word in AI, but the idea underneath is small. An agent is a model that can use tools, put in a loop: think, act, look at the result, and decide what to do next, until the job is done. Watch one work, find out where it goes wrong, and see the two guardrails every agent needs.
A model, some tools and a loop#
With function calling, a model can ask your code to run a tool once. An agent simply keeps going. Each time around the loop, your code calls the model with everything so far; the model either asks for another tool or gives its final answer. Your code runs the tool, adds the result to the conversation, and goes round again.
Here’s an agent with two tools answering a question that needs three of them in a row.
Notice the numbers under the loop. Every model call sends the whole conversation so far, because the model remembers nothing between calls. So each step costs more than the last, and the total grows faster than the number of steps. Long-running agents get slow and expensive for exactly this reason.
This agent could also have been quicker. It looked up the two cities one after the other, when it could have asked for both in a single step, as the function calling explainer shows. Fewer round trips, lower cost.
Guardrail one: a step limit#
Nothing in the loop itself makes it stop. A model can get stuck retrying a failing tool, or wander off on a tangent, and every lap costs money. So your code sets a maximum number of model calls.
Predict first
Set the step limit in the figure to 3 and run it. What happens?
Guardrail two: ask before anything you can’t undo#
Reading is safe. Deleting, paying and sending are not. This agent has been asked to clean up an inbox, and it’s about to make a confident mistake. Run it with the safety check on, then switch it off and run it again.
The agent’s reasoning wasn’t crazy: an unknown sender, a deadline and a request to sign is what phishing looks like. It was just wrong, and with no check in place, nobody would have known until Friday. With the check on, a person sees the action before it happens and can stop it.
Predict first
Where should the rule “never delete without asking” live?
In code#
The whole loop, with both guardrails, using the Anthropic Python SDK:
import json
import anthropic
client = anthropic.Anthropic()
MAX_STEPS = 10
RISKY = {"delete_email", "send_email"}
def run_tool(name, args):
if name in RISKY:
answer = input(f"Allow {name}({json.dumps(args)})? [y/N] ")
if answer.strip().lower() != "y":
return "The user declined this action.", True
return TOOLS[name](**args), False # TOOLS maps tool names to your functions
def run_agent(task, tools):
messages = [{"role": "user", "content": task}]
for step in range(MAX_STEPS):
response = client.messages.create(
model="claude-opus-5-5", max_tokens=16000, tools=tools, messages=messages
)
if response.stop_reason != "tool_use":
return next((b.text for b in response.content if b.type == "text"), "")
messages.append({"role": "assistant", "content": response.content})
results = []
for block in response.content:
if block.type == "tool_use":
output, is_error = run_tool(block.name, block.input)
results.append({"type": "tool_result", "tool_use_id": block.id,
"content": json.dumps(output), "is_error": is_error})
messages.append({"role": "user", "content": results})
return "Stopped: reached the step limit before finishing."Frameworks and SDK helpers can run this loop for you, and for bigger projects they’re worth it. Write it by hand once first, though: when an agent misbehaves, knowing that it’s just this loop makes it far easier to see why.
What to remember
- An agent is a model with tools in a loop: think, act, observe, repeat, until it answers.
- Every lap resends the whole conversation, so cost grows with each step. Always cap the number of steps, and handle hitting the cap.
- Put a human check in code around anything you can’t undo. Prompts can be misread or overridden; code can’t.