⚡ "One loop & Bash is all you need" — one tool + one loop = one Agent.

Harness layer: the loop — the first connection between the model and the real world.

Problem

Suppose you ask the large model: "Help me read what files are in my directory,and execute XXX.py". OK, the model can output a bash command, but it stops after outputting it — it won't run it itself, nor will it see the result and continue reasoning.

Of course, you can run it manually, paste the output back into the chat box, and let it continue working. When the next command comes out, you run it again and paste it back, just like how we wrote code with ChatGPT in 2023.

Every round trip, you are acting as themiddle layer, and automating it is what this chapter is about.

Solution

Agent Loop — One Loop Is Enough

The tool in the image is also just a single Bash tool as an example.

Essentially it is just a while True loop: if the model calls a tool, it continues; if not, it stops. The entire process has only two signals:

Signal

Meaning

Loop action

stop_reason == "tool_use"

The model raises its hand and says, "I want to use a tool."

Execute → feed the result back in → continue

stop_reason != "tool_use"

The model says, "I'm done."

Exit the loop

How it works

📍 The original text starts here and uses Python code to explain the principle. For convenience, I won't use code here; I'll only introduce the idea and logic, not the technical details. If you're interested in the technical side, you can check:https://github.com/shareAI-lab/learn-claude-code/blob/main/s01_agent_loop/README.md#%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86

Actually, this part isn't complicated. Simply put, there is a loop—if you know a little about code, you'll recognize it as the most basic if loop.

The condition is based on whether the model asks to call a tool. If it does, let the model call the corresponding tool, then append the result of the tool call to the user's question—that is, return to the point at the very beginning when the user's question was handed to the AI. But this time, in addition to the user's question, there is also the result of the AI's tool call from the previous round. At this point, the previous condition check is performed again. If the model decides it doesn't need to call a tool, OK, the loop ends, and it moves on to the next step or directly outputs.

If the text is still hard to understand, I had AI generate a little comic here; you can understand what's really going on this way.

Agent Loop — one loop is enough