How Agent Tool-Use Loops Work
AI coding agents follow a loop of planning, reading, editing, and verifying. Understanding that loop helps you write prompts that get better results.
When you ask an AI coding agent to do something, it does not produce the final answer in one shot. It runs a loop. Each cycle, the agent decides what tool to use, observes the result, and decides what to do next. This is called a tool-use loop, and it is the core of agentic coding.
Understanding the loop helps you predict what the agent will do, when it might fail, and how to write prompts that keep it on track.
The Basic Loop
Most coding agents follow the same pattern:
- Plan. The agent breaks your request into steps.
- Read. It loads relevant files or runs commands to gather information.
- Act. It edits files, runs tests, or executes shell commands.
- Observe. It reads the result of the action.
- Repeat. It continues until the task is done or it gets stuck.
The agent sees the full history of the loop in its context window. That history is both its memory and its main source of confusion.
Tools in the Loop
Agents use a fixed set of tools. Common ones include:
- Read file. Loads a file into context.
- Edit file. Replaces text in a file.
- Run command. Executes a shell command and captures output.
- Search. Finds files or text by keyword.
- Summarize. Condenses long output into a shorter form.
The agent chooses which tool to use based on the current state and the goal. Your role is to approve or reject each action, depending on the tool.
OpenCode
OpenCode exposes its tool-use loop in the terminal. You approve each read, edit, and command, giving you full visibility into what the agent is doing.
Free tier available
Affiliate link · We earn a commission at no cost to you.
Why Loops Get Stuck
Three things commonly break the loop:
- Ambiguous goals. The agent does not know what “done” looks like.
- Missing context. The agent cannot find the files it needs.
- Noisy output. A command dumps thousands of lines, swamping the context window.
When the loop gets stuck, the agent may retry the same action, hallucinate a solution, or ask for help. The best fix is usually to restart with a clearer prompt or more context.
Writing Prompts for the Loop
Good prompts give the agent a goal, constraints, and a way to verify success. For example:
“Add rate limiting to the API routes in
src/routes/. Use the existing Redis client insrc/lib/redis.ts. Update the tests intests/routes/and run them. Do not change the public response format.”
This prompt names the files, the pattern, the verification step, and a constraint. The agent’s loop has a clear target at every turn.
FAQ
Can I see the tool-use loop while it runs?
What happens if I do not approve an action?
How many turns can a loop take?
Tools used in this review
Some links above are affiliate links. We may earn a commission if you sign up. See our disclosure for details.
Related reading
2026-07-16
Context Windows in AI Coding Agents Explained
Context windows limit how much code an AI agent can see at once. Understanding that limit explains why agents miss files, repeat work, and sometimes lose track of the task.
2026-07-16
How AI Coding Agents Read and Index Your Codebase
AI agents do not magically understand your project. They use a mix of file listing, search, and context loading to find relevant code. Here is how it works.
2026-07-16
Inline Completion vs Agentic Coding: What Is the Difference?
Inline completion and agentic coding are both AI-assisted development, but they solve different problems. Here is how to tell which one you need.
2026-07-16
Why Terminal-Based AI Agents Use Text In, Text Out
The terminal seems old-fashioned, but its text-based interface is exactly why AI coding agents work there. Here is the engineering reason behind the trend.
2026-06-22
TCP vs UDP, Explained Through What Breaks When You Pick Wrong
TCP and UDP aren't interchangeable. We walk through the exact failure modes — head-of-line blocking, silent packet loss, Nagle delays — that show up when you pick the wrong transport.
Get the best tools, weekly
One email every Friday. No spam, unsubscribe anytime.