Chapter 4
Agent Loop
The Agent's heartbeat — a cycle of think, act, observe
🎯
Like a Chef Cooking
A chef's process goes: read the recipe (think) → chop vegetables (act) → taste (observe) → decide to add salt or sugar (think again) → keep going. This loop continues until the dish is done and served. The Agent Loop works exactly the same way — the AI continuously cycles through "think → use tool → check result → think again" until the task is complete.
What Is the Agent Loop?
The Agent Loop is the core engine of the entire Harness — you could call it the Agent's "heartbeat."
Its workflow is straightforward:
1. Send the user's message to the LLM
2. The LLM returns a response — either text or a tool call request
3. If it's a tool call, execute the tool and feed the result back to the LLM
4. Go back to step 2, continue the loop
5. Until the LLM considers the task done, returning a final text response
This is the secret behind how Agents accomplish complex tasks — they don't give a one-shot answer; they progressively advance through multiple loop iterations.
🔄 Agent Loop Animation
User
LLM
Tool Call
Permission
Execute
Result
💬Step 1 :User sends message
User inputs a request, e.g. "Read the config.json file for me"
Let's Look at Real Code (Simplified)
openharness/engine/query.py
1async def run_query(context):2 """Agent Loop core: keep looping until the task is done"""34 for turn in range(context.max_turns):5 # Step 1: Send messages to LLM, get response6 response = await stream_response(7 context.api_client,8 context.messages,9 context.tools10 )1112 # Step 2: If LLM didn't request a tool call, task is done13 if response.stop_reason != "tool_use":14 break1516 # Step 3: Execute each tool the LLM requested17 for tool_call in response.tool_uses:18 result = await execute_tool(tool_call)19 context.messages.append(result)2021 # Step 4: Tool results added to messages, back to top of loop
Here's the core Agent Loop code from OpenHarness, simplified to under 20 lines:
Why Is the Agent Loop So Important?
The Agent Loop is the foundation of all Agent capabilities. Without it, tools, permissions, and skills are just scattered components. The Agent Loop strings them together into a complete workflow.
Think about it: if a task requires reading a file, modifying content, running tests, and fixing errors — that takes multiple loop iterations. In each iteration, the LLM decides what to do next based on the previous step's results.
This "think → act → observe" cycle is what makes AI Agents powerful.
The Secret of stop_reason
In the Agent Loop, there's one critical condition: stop_reason.
When the LLM returns stop_reason as "tool_use," it means the LLM wants to use a tool — the loop continues.
When stop_reason is something else (like "end_turn"), it means the LLM believes the task is complete — the loop ends.
This simple check is the mechanism by which the Agent knows when to keep working and when to stop.
📌 Key Takeaway
The Loop Is the Agent's Core Pattern
The Agent Loop is a simple but powerful pattern: send message → get response → if tools needed, execute them → feed results back to LLM → repeat. The entire Agent Loop core in OpenHarness is under 70 lines of code, yet it drives all complex functionality.
🧠 Check Your Understanding
When does the Agent Loop stop iterating?
🧠 Check Your Understanding
If an Agent needs to "read a file → modify content → run tests," roughly how many loop iterations will it take?