Lesson 1 of 5 · 2 min
An agent is a loop
Call the model, run the tool it asks for, add the result to the messages and repeat until it stops asking. Frameworks wrap this loop. Write it once by hand and they stop being mysterious.
notes = {"deadline": "Friday", "owner": "Meera"}
def model(history):
seen = [m for m in history if m["role"] == "tool"]
if not seen:
return {"tool": "read_note", "key": "deadline"}
if len(seen) == 1:
return {"tool": "read_note", "key": "owner"}
return {"answer": f"{seen[1]['content']} must finish by {seen[0]['content']}."}
history = [{"role": "user", "content": "Who is due when?"}]
for step in range(6):
move = model(history)
if "answer" in move:
print("answer:", move["answer"])
break
result = notes.get(move["key"], "not found")
print("step", step, move["tool"], move["key"], "->", result)
history.append({"role": "tool", "content": result})
