The Agent Loop

Think, Act, Observe, Repeat

The agent loop is the core execution pattern: the agent receives the current state, reasons about what to do, takes an action, observes the result, and repeats — until the goal is complete or a stopping condition is reached.

The ReAct Pattern

Structured alternation

Thought: reasoning about the current situation
Action: a specific tool call with parameters
Observation: the result (provided by the framework)
Thought: updated reasoning incorporating the observation
Action: next tool call
... repeat ...
Final Answer: response to the user

This structured alternation makes the agent's decision-making inspectable and debuggable — you can read the full thought-action-observation trace to understand exactly what the agent did and why.

Context Window Accumulation

Each loop iteration adds content to the LLM's context: actions, observations, and thoughts. Over many iterations, context grows and can approach the model's limit.

1

Summarization — Compress older parts of the history into a summary when they exceed a threshold.

2

Sliding window — Keep only the most recent N turns of history; discard the oldest.

3

External memory — Persist detailed history to a vector database and retrieve only the relevant parts per step.

Stopping Conditions and Human Checkpoints

Stopping Conditions

Max iterations (e.g., stop after 50 steps). Timeout (stop after 5 minutes). Explicit termination output. Error thresholds (stop after 3 consecutive failures of the same action).

Human-in-the-Loop

Before high-stakes actions (send email, delete file, make purchase), pause and request human approval. Essential for production agents where mistakes have real consequences.

Need Help?

Ask the AI assistant about the agent loop, the ReAct pattern, context accumulation, or how to design stopping conditions.