Application patterns

Agentic loops

An agentic loop lets the model observe a result, request another tool, and continue until it stops or you stop it.

Mental model

A short research shift with a clock. The model may take another action only while the stop rule says the job is unfinished.

Some tasks have an unknown number of steps: find the file, read it, notice a second file, then answer. A fixed workflow cannot name that path in advance.

When to use

  • You cannot list the steps because each result changes what is needed next.
  • You have a hard stop: a step limit, a budget, or a confirmation before side effects.

When not to use

  • The path is predictable. Use a workflow.
  • A wrong action is expensive and you have no confirmation in the loop.
Agent loop
  1. Goal
  2. Model
  3. Tool
  4. Result

The result goes back to the model until a stop condition.

Example

Goal: "Why did the signup test start failing?"

  1. The model requests search_repo for the test name.
  2. Your code returns the file path and the assertion.
  3. The model requests read_file on the helper the assertion calls.
  4. Your code returns the helper.
  5. The model answers with the mismatch and stops.

The loop ends because the model returned a final answer, or because it hit the step limit. It does not end because the prose sounded finished while another tool call was pending.

Implementation notes

Every iteration should be a tool call or a final answer, not a blend. Keep a transcript of calls and results. That transcript is the artifact you read when the agent wanders.

Cap steps and tool failures. Two identical failing calls in a row should stop the loop and surface the error.

Write side effects so they require a confirmation tool or sit outside the loop. Reading can be iterative. Sending email should not be.

Common mistakes

  • Using a loop because agents are in fashion, on a task with three known steps.
  • No step limit, so a confused model keeps searching until the bill arrives.
  • Letting the model declare success without checking that the original goal was answered.

Build this

Related prompts

Related lessons