Every agent runs on one loop: think, act, observe, with a step limit as the guardrail. Asked for 15% of Q3 revenue, ours searches, calculates and answers $630,000.
python3 --version.git clone https://github.com/DayanEbrar0X/data-anatomy.ai.git cd data-anatomy.ai
python3 -m venv .venv source .venv/bin/activate
cd ai-engineering/03-ai-agent-loop python3 src/agent.py
from tools import search, calculator
from model import think # the LLM call
goal = "What is 15% of Q3 revenue?"
memory = [goal]
for step in range(5):
action, arg = think(memory)
if action == "answer":
print("answer:", arg)
break
tool = {"search": search,
"calc": calculator}[action]
result = tool(arg)
memory.append(result)
print(step, action, "->", result)Every AI agent you've heard of runs on one tiny loop. Think. Act. Observe.
Repeat. Say finance asks: what's 15% of Q3 revenue? The model alone can't know that number. It needs tools.
So we hand it two: a search over company data, and a calculator. Think is the model. Here it's a stand-in. In production, it's one LLM call.
Memory starts with just the goal. Now the loop. Each step, the model reads memory and picks an action. If it's ready, it answers and stops.
If not, we grab the tool it asked for, run it, and write the result into memory, so the next thought knows more. Let's run it. Step zero: it searches, and finds Q3 revenue: 4.2 million.
Step one: it calls the calculator. 630,000. Step two: it has everything, so it answers. That's the whole trick.
Swap in real APIs, and this same loop books meetings, files tickets, or queries your warehouse. And range(5) on line 7? That's your guardrail. Without a limit, an agent can loop forever.
Think. Act. Observe. Every agent, one loop.
Read the full article →