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.
A clear initial prompt makes every turn of the loop easier. Vague goals create vague plans, which create vague actions, which create confusing observations.
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.
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.
Long loops are expensive. Every turn consumes tokens. A task that should take ten turns can cost ten times as much if it takes a hundred. If the agent is looping, stop and reframe the task.
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.
Originally published at pickuma.com. Subscribe to the RSS or follow @pickuma.bsky.social for new reviews.
Top comments (0)