Nexus Learn

Log in · Create free account

Module 1: What is an AI agent? · 10 min

The agent loop

Almost every agent runs the same simple loop.

  1. Observe: read the goal, the history and the latest tool result.
  2. Think: the model decides the next step.
  3. Act: call a tool (search, read a file, send a request) or give the final answer.
  4. 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.

(free account needed to save progress)

← Chatbot, workflow or agent?  |  When an agent is the right choice →