Knaph

Tools and the Loop17 of 375 min read

Think, Act, Look, Repeat

You ask an agent to "fix the failing test". Twenty minutes later it's done: it ran the tests, read three files, changed one, ran the tests again, saw a new error, changed another file, and ran them a third time. Nobody told it to do any of those steps. In the last lesson you saw one tool call. This lesson is about what turns one tool call into twenty.

The loop

The answer is surprisingly small. The program around the model repeats one cycle:

  1. Send the request to the model.
  2. If the reply asks for tools, run them, add their results to the conversation, and go back to step 1.
  3. If the reply asks for no tools, it's the final answer: show it, and stop.

That's the agent loop. Here it is in Python. You don't need to know Python to follow it:

messages = [your_request]
while True:
    reply = model.respond(system_prompt, tools, messages)
    messages.append(reply)
    if not reply.tool_calls:
        break
    results = [run_tool(call) for call in reply.tool_calls]
    messages.append(results)
print(reply.text)
Python

Line by line: start a conversation with your request. Then, again and again (while True): send everything to the model, and add its reply to the conversation. If the reply has no tool calls, leave the loop (break): the model is done. Otherwise, run each tool it asked for, add the results to the conversation, and go round again. At the end, show the model's final words.

Each time round is called a turn, or a step. Notice who decides when to stop: not the program, but the model, by writing a reply with no tool calls.

The agent loop as a circle. The model writes a reply; if it asks for tools, the program runs them and adds the results, then the model is asked again. When a reply asks for no tools, the loop ends with the final answer.


Leila That's it? I thought an agent would be some huge, clever program.

Omid The clever part is the model. The loop is just patient. What does the loop give the model that a single question and answer doesn't?

Leila A chance to see what happened. It can try something, look at the result, and change its mind.

Omid Exactly. Think about how you'd fix a broken spreadsheet formula. Would you write the fix without ever looking at the result?

Leila No. I'd try, look, and try again. So the loop lets the model work the way people work.

Omid And that's why agents are so much better at real tasks than a single answer: every turn, they get evidence about whether they're on the right track.

Leila But every turn sends everything again, doesn't it? The whole conversation, every file it's read...

Omid Every turn. Twenty turns is twenty full requests, each a bit longer than the last.


A session, turn by turn

Here's "fix the failing test" as the strip of turns it really is:

A session as a strip of turns: run the tests, read the test file, read the code file, edit the code, run the tests again and see a new error, edit again, run the tests and see them pass, then a final answer. Each turn's request is longer than the one before.

A few things become clear from this picture:

  • Each turn is a full request. The cost and the context grow with every turn, as you saw in Paying by the Token and Everything It Can See.
  • One turn can ask for several tools at once. If the model wants to read three files, it can ask for all three in one reply, and the program runs them side by side. That saves turns.
  • Results steer the next step. The new error at turn five is what made the model edit a second file. No one planned that in advance.

How it ends

A loop like this ends in one of three ways:

  • The model decides it's done, and writes a final answer with no tool calls. This is the normal way.
  • It hits a limit the program sets: a maximum number of turns, a time limit, or a budget.
  • You stop it. In Claude Code, pressing Escape interrupts the agent at once, and you can tell it what to do differently.

This is also why agents can work for an hour or more on a big task: as long as the model keeps asking for tools, the loop keeps going. And it's why agents sometimes go round in circles: trying the same failing fix, or reading the same files again. The loop doesn't know it's stuck; only the model can notice, and sometimes it doesn't.

Try it

Give an agent a small task with a check in it, like "make a file called notes.txt with today's date in it, then read it back to make sure". Count the turns in what it shows you: the tool calls, then the final answer. If you have no agent, find a recording of one online and count the steps.

Using it well

  1. Give the agent a way to check its work. Tests to run, a page to look at, a number to compare: the loop is only as good as the evidence each turn brings back.
  2. Step in when it goes round in circles. If you see the same attempt twice, stop it and add what it's missing. A sentence from you can save twenty wasted turns.
  3. Keep tasks a sensible size. A task that needs 200 turns will crowd the context and cost a lot; split it into pieces that each end with something you can check.

Words to know

  • agent loop - the cycle an agent runs: send the request, run any tools the reply asks for, add the results, and repeat until a reply asks for none
  • turn - one time round the loop: one request to the model and its reply; also called a step

Big idea

An agent is a model in a loop: ask, act, look at the result, repeat, until the model says it's done. The loop is simple; its power comes from giving the model evidence at every turn, and its cost from sending everything again each time.

Test your understanding

Sign in to test your understanding of this lesson and track your progress.