Strip the hype and an AI agent is the same loop every robot runs, with a language model as the “think” step:
loop (with a step limit):
action = model(conversation so far) # think
if action is a final answer: stop
result = run_tool(action) # act
add action and result to the conversation # sense
The model never does anything. It only chooses what to do next; your code runs the tools and feeds back what happened. That boundary is where almost all the engineering lives: which tools exist, what they’re allowed to touch, what happens when they fail, and how you stop a runaway loop.
A complete agent you can run
To keep this free and deterministic, a tiny scripted function stands in for the language model. Everything else — the loop, the tool registry, the step limit — is exactly what a real agent has. Swap scripted_model for a call to a real model and nothing else changes.
import json
ORDERS = {"A100": {"status": "shipped", "eta_days": 2},
"B200": {"status": "processing", "eta_days": 5}}
def lookup_order(order_id):
return ORDERS.get(order_id, {"error": "unknown order"})
TOOLS = {"lookup_order": lookup_order}
def scripted_model(messages):
"""Stands in for an LLM: picks the next step from the conversation so far."""
question = messages[0]["content"]
results = [m for m in messages if m["role"] == "tool"]
if "order" in question and not results:
return {"tool": "lookup_order", "args": {"order_id": question.split()[-1]}}
if results:
r = json.loads(results[-1]["content"])
if "error" in r:
return {"final": "I couldn't find that order."}
return {"final": f"Your order is {r['status']}, arriving in {r['eta_days']} days."}
return {"final": "Sorry, I can only help with orders."}
def run_agent(question, model=scripted_model, max_steps=5):
messages = [{"role": "user", "content": question}]
for _ in range(max_steps):
action = model(messages)
if "final" in action:
return action["final"]
result = TOOLS[action["tool"]](**action["args"])
messages.append({"role": "assistant", "content": json.dumps(action)})
messages.append({"role": "tool", "content": json.dumps(result)})
return "Stopped: too many steps."
print(run_agent("where is order A100"))
print(run_agent("where is order Z999"))
print(run_agent("tell me a joke"))
Your order is shipped, arriving in 2 days.
I couldn't find that order.
Sorry, I can only help with orders.
What production adds (and what breaks first)
- Step limit and budget.
max_stepsis the seatbelt. Without it a confused model loops forever, and you pay for every turn. - Tool errors are data. The unknown-order case returns an error to the model instead of crashing. Agents that handle failure as input recover; ones that throw don’t.
- Least privilege. Each tool should do one narrow thing.
lookup_ordercan only read. An agent with arun_any_sqltool is one bad prompt away from an incident. - Real models add uncertainty. The same input can give different actions. You can’t unit-test your way out of that — you measure it. That’s the next lesson: evaluating agents.
Exercises
- Add a
cancel_ordertool. What checks should happen before it runs, and who should confirm? - Make
lookup_orderraise an exception. Where should it be caught — in the tool, the loop, or the model? - Set
max_steps=1. What does the user see, and is that acceptable?
Takeaways
- An agent is a loop around a model; the model chooses, your code acts.
- Safety and reliability come from the tools, limits and error handling around the model.
- Same loop as a robot: sense, think, act — see the loop every robot runs.