An AI agent is just a while-loop
The whole thing in 20 lines of Python, and the three places it breaks.

Welcome to the BitSketch Academy blog. Every week or two I take one idea about how AI agents work and explain it with a sketch and a bit of code. No hype, no framework of the month. This is the first one, and it starts at the bottom.
Agent frameworks make it look complicated. There are planners, executors, memory modules, orchestrators, graphs of nodes. Read the docs for a weekend and you can still come away unsure what actually happens when you press run.
Here's the secret: underneath every one of them is the same small loop.
- Send the conversation so far to the model.
- If the model asks to use a tool, run it and add the result to the conversation.
- If it doesn't ask, it has answered. Stop.
That's an agent. Planning, memory, sub-agents and guardrails are all things bolted onto this loop.

The whole thing
Here it is in plain Python. call_model stands for whatever model API you use: it takes a list of messages and returns the model's reply.
def run_agent(task, tools, call_model):
messages = [{"role": "user", "content": task}]
while True:
reply = call_model(messages, tools=tools)
messages.append(reply)
if not reply.tool_calls: # no tool requested: this is the answer
return reply.content
for call in reply.tool_calls: # the model asked for one or more tools
result = tools[call.name](**call.arguments)
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": str(result),
})It's under 20 lines, and it's a real agent. Give it a tool that reads files and one that writes them, and you have the core of a coding assistant.
One run, step by step
Say the task is "What's 17% of the price of order #4411?" and the agent has one tool, get_order.

Two model calls and one tool call. Notice what the model didn't do: it never touched your database. It asked, and your code did the work.

The best way I know to think about it: the model is a very smart colleague on the phone. They can't see your screen. They can only tell you what to do, and they only know what you read back to them.
Three places it breaks
The loop is simple, and it's also fragile. Read the code again and you'll find three problems hiding in it.
1. The model has no memory. It remembers nothing between calls. The messages list is its memory, and you resend all of it every time. Long tasks make that list grow, so every step gets slower and more expensive, until the context is full of old tool output and the model starts losing the plot.
2. The model decides when it's done. The loop ends only when the model stops asking for tools. You're trusting it to know when the job is finished. Sometimes it quits early and says "done" when nothing was done. Sometimes it never stops.
3. There is no brake. while True will happily run all night if the model keeps asking for tools. Nothing in that code stops a loop that calls the same failing tool 400 times and burns through your API budget.
Every serious agent framework is, in large part, a set of fixes for these three problems. "Max iterations" is a brake on while True. "Memory" is code that edits messages. "Tool schema" is a description of tools sent to the model. Once you can see the loop, the framework stops being a black box.

Where to go from here
I turned this into a short, free text course: The Agent Loop, From Scratch. It builds this loop one piece at a time, fixes each of the three problems above with code you can copy, and ends with a working agent you can point at your own codebase:
- The loop in 30 lines
- Tools: how the model acts
- Stopping on purpose: step limits, cost budgets and done-checks
- Where the agent's state lives
- Debugging from the trace
- Project: a codebase Q&A agent
- Finding the loop in any framework
If you'd rather watch than read, the same ideas come as hand-drawn Shorts on YouTube.
Next time: the contract between the model and your tools, which is where most beginner agent bugs live. Leave your email below so you don't miss it.
Get the next post
One idea about AI agents every week or two, explained with a sketch, plus the launch discount for the video course.
You'll only hear from us when there's something new: a course, an ebook or a discount. Unsubscribe anytime. Privacy