Anybody Can AI

Quick Stats

Completed

0

Time Spent

0m

Streak

0

User

User

AI Agents from Scratch

Agent Fundamentals/The Agent Loop

The Agent Loop

Think, act, observe, repeat.

The core loop

Strip away every framework and an agent is just this loop:

while not done and steps < limit:
    decision = llm(messages, tools)
    if decision.is_final:
        return decision.answer
    result = run_tool(decision.tool, decision.args)
    messages.append(result)

Each pass, the model looks at the conversation so far and the tools available and makes one decision: either "I'm done, here's the answer" or "call this tool with these arguments." If it calls a tool, you run it, append the result to the messages, and loop again — so the model's next decision is informed by what just happened.

What each part needs

For the loop to work, three things have to be right:

  • Tools described clearly — name, what they do, and their inputs — so the model knows when and how to use each. Vague tool descriptions are the most common reason an agent ignores a tool it should use.
  • A stopping condition — a final answer and a hard step limit, so a confused agent can't loop forever.
  • Observation handling — feeding each tool's result back into the context so the model reacts to reality, not its expectation.

The pattern behind the patterns

This is often called the ReAct pattern — reason about what to do, act by calling a tool, observe, repeat. It sounds almost too simple, but everything fancier (planning, reflection, multi-agent) is a variation on this loop.

Frameworks like LangGraph, CrewAI, and the various Agents SDKs are conveniences on top of this loop — not magic. Understand the loop and you can read, debug, or rebuild any of them.

Try this: Trace the loop by hand for "find the population of Japan and divide it by 10." Write out each pass: what the model decides, which tool it calls, what comes back, and how that shapes the next decision. Doing it on paper once makes every agent framework suddenly legible.