Agents and tools

The agent loop

Follow the repeated observe, decide, act, and check cycle behind agentic work.

8 minute lesson

~~~

An agent starts with a goal and the state it can observe. The model decides on a next step, the host executes a tool, and the result becomes new context.

The cycle is often described as observe, decide, act, check. Real loops are messy. A file may not exist, a command may fail, or a tool may return an unexpected shape.

Tool success is not task success

Suppose the agent runs a build and receives exit code 0. This proves the build completed successfully. It does not prove the new button looks correct, the authorization rule is safe, or the requested behavior works in the browser.

Every check must support a claim. The agent should stop only when the goal’s acceptance criteria have relevant evidence, not when the last tool call happened to succeed.

Failure should change the next step

Imagine a test command fails because a dependency is missing. Running the identical command five times adds no information. The agent should read the error, inspect the project setup, install the dependency if authorized, or ask for help.

A useful retry changes something: the input, context, tool, or hypothesis. Repetition without new evidence is a stuck loop.

Some actions are unsafe to repeat

Reading a file twice is usually harmless. Sending an email, charging a card, or creating a database record twice may have real consequences.

For write operations, consider idempotency: can the same request run twice without creating a second effect? Use idempotency keys, existence checks, transactions, or explicit confirmation where the system supports them.

Give the loop boundaries

A good agent loop limits:

  • attempts, time, and cost
  • files, services, or records in scope
  • permissions and available tools
  • actions that require human approval
  • conditions that mean stop, replan, or ask for help

The agent should ask when it lacks required context, authority, or a safe recovery path. I cannot verify the payment flow without sandbox access is a useful stop. Pretending the goal is complete is not.

Try this

Review an agent trace from a recent task. Mark each observation, action, and check. Find one place where a result should have changed the plan or where the completion claim needed stronger evidence.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →