Think, act, observe, repeat, with a step limit so it never runs forever. It searches Q3 revenue (4.2 million), takes 15% 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 model import think
from tools import TOOLS
memory = ["What is 15% of Q3 revenue?"]
for step in range(5):
action, arg = think(memory)
if action == "answer": break
memory.append(TOOLS[action](arg))
print("answer:", arg)Every AI agent runs on one tiny loop. Think. Act. Observe.
Repeat. Finance asks: what's 15% of Q3 revenue? Give it two tools and a memory holding the question. Each step, the model thinks, then picks an action.
If it's ready, it answers and stops. If not, it runs the tool and saves the result to memory. range(5) is the step limit, so it never loops forever. Run it.
It searches, finds 4.2 million, takes 15%, and answers: $630,000. Think, act, observe, with a limit. That's every agent.
Read the lesson on GitHub →