Back to docs
Recipe

Agent loop design

The core execution cycle that powers autonomous agents — plan, act, observe, repeat.

Overview

Every autonomous agent runs a tight loop. The agent receives a goal, reasons about the next step, executes an action, observes the result, and feeds that observation back into the next reasoning cycle. This recipe covers the canonical loop structure and the design decisions that make it reliable.

The four phases

Plan

The agent examines the current state and decides what to do next. This is typically an LLM call with tool definitions in the system prompt.

Act

The chosen tool is invoked with the arguments the model produced. This could be a function call, an API request, or a shell command.

Observe

The result of the action is captured — stdout, return value, error message — and formatted for the next planning step.

Repeat

The observation is appended to the message history and the loop continues until a terminal condition is met.

Termination conditions

A loop without an exit is a runaway agent. Common stops include a dedicated finish tool, a maximum iteration count, a token budget, or a timeout. Always combine at least two conditions — a soft stop (the model declares done) and a hard stop (iteration limit).

Error recovery

Tool calls fail. Networks drop. APIs return 429s. The loop must surface the error to the model so it can adapt — retry with backoff, pick a different tool, or ask the user for guidance. Never silently swallow failures; they are observations too.

Context window management

Long-running loops fill the context window fast. Summarize older turns, truncate verbose tool outputs, and keep only the essential state. A sliding window of the last N observations plus a compressed summary of earlier history works well in practice.