pickuma.
Dev Knowledge

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.

6 min read

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:

  1. Plan. The agent breaks your request into steps.
  2. Read. It loads relevant files or runs commands to gather information.
  3. Act. It edits files, runs tests, or executes shell commands.
  4. Observe. It reads the result of the action.
  5. 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

Try OpenCode

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 in src/lib/redis.ts. Update the tests in tests/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?
Yes. Terminal agents like OpenCode print each tool call and its result. IDE agents show the plan and actions in a side panel. Either way, you can review before approving.
What happens if I do not approve an action?
The agent stops and waits. You can modify the plan, tell it to skip a step, or cancel the task. Approval is the human control point.
How many turns can a loop take?
It depends on the task. A simple edit might take 3-5 turns. A complex refactor can take 30-50. Most tools handle hundreds of turns, but cost and context window become limiting factors long before that.

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

See all Dev Knowledge articles →

Get the best tools, weekly

One email every Friday. No spam, unsubscribe anytime.