Module 1: What is an AI agent? · 10 min
The agent loop
Almost every agent runs the same simple loop.
- Observe: read the goal, the history and the latest tool result.
- Think: the model decides the next step.
- Act: call a tool (search, read a file, send a request) or give the final answer.
- Repeat until done, or until a limit is reached.
messages = [goal]
for step in range(MAX_STEPS): # always set a limit
reply = model(messages, tools)
if reply.is_final_answer:
return reply.text
result = run_tool(reply.tool_name, reply.tool_args)
messages.append(reply)
messages.append(result)
The model never runs the tool itself. It only asks for it, and your code runs it and sends the result back.
← Chatbot, workflow or agent? | When an agent is the right choice →