Index Ai Academy
Phase 1 · Building with LLMs · Tools & agents

The agent loop

Model → tool → result → model, until the task is done.

About 30 minutes · Free account required

Open this lesson Create a free account

Why it matters

An agent is a model using tools in a loop: it looks something up, reads the result, decides what to do next, and repeats until the job is done. That's how an assistant can answer "which active campaign has the best ROAS?" when it first has to find the active campaigns, then fetch each one's numbers, then compare. The loop itself is about 15 lines. The judgment is in the guardrails around it.

What you'll be able to do

  • Turn a single tool round trip into a loop that runs until the model is done
  • Use stop_reason to decide whether to continue
  • Add the guardrails every agent needs: a turn limit, approval for risky tools, and a log of steps
  • Explain why agents get slower and more expensive with every turn

Before this lesson

Next in this phase: Model Context Protocol