liveBook · Manning

Agent loop

As explained in Designing AI Agents

An iterative agent-control pattern in which an agent perceives, reasons, acts, and reflects, with subsequent steps selected dynamically.

An agent loop turns each result into the next observation. A language model selects the next action at runtime, while an engineered harness supplies context, permissions, budgets, and stopping conditions. Unlike a fixed pipeline, the path through the task is selected dynamically. section 1.1

The basic phases are perception, reasoning, and action. Reflection can feed evidence from an action back into another decision. A single pass makes the phases visible, but cannot verify its own result; an iterative loop can use external results to revise its next step. section 2.6.1 section 5.5.2

Listing 2.1 Argus skeleton, one PRA pass (ch02-architecture/argus/core.py)
# argus/core.py — The simplest possible code review agent
import anthropic
import json
 
def review_diff(diff: str, context: str = "") -> dict:
    """One pass through the PRA loop.
 
    Perception: the diff text (+ optional context)
    Reasoning:  Claude analyzes the diff
    Action:     structured review output
    """
    client = anthropic.Anthropic()
 
    # --- PERCEPTION: assemble  #A
➥    # what the agent sees ---
    system = """You are Argus, an expert code reviewer.
Respond with JSON: {"summary": "...", "comments": [
  {"file": "...", "line": ..., "severity": "...", "message": "..."}
]}"""
 
    user_msg = f"## Diff:\n```diff\n{diff}\n```"
    if context:
        user_msg += f"\n\n## Context:\n{context}"
 
    # --- REASONING: Claude analyzes ---  #B
    response = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=2048,
        system=system,
        messages=[{"role": "user", "content": user_msg}],
    )
 
    # --- ACTION: parse structured output ---  #C
    text = response.content[0].text
    if "```json" in text:
        text = text.split("```json")[1].split("```")[0]
    return json.loads(text)

The Argus skeleton separates perception, reasoning, and action in a minimal code-review pass. It returns structured review output without running tests or verifying the review. section 2.6.1

Debug with feedback

Read a failing test, hypothesize a cause, make a bounded edit, and run the relevant tests. Inspect the new output for the original failure and possible regressions, then revise if needed. A passing test is evidence for its acceptance condition, not proof that every reasoning step was correct or that untested regressions are absent. section 5.5.2

Keep tool use governed: a loop can investigate without gaining permission to mutate production state. Validate the design against accepted outcomes; benchmark gains alone cannot isolate the contributions of the model, tools, search, tests, or recovery policy. section 5.5.2

Do not add a loop where a deterministic route already handles the task. For routine support requests with stable policies and a human sending the response, a general loop adds tool choice, completion judgment, retries, and trace management without addressing an observed deficit. Reconsider if exceptions outgrow the policy or reliable tool choice can no longer be encoded. section 11.1.3

How Designing AI Agents covers it

How deep it goes
Builds a minimal perception–reasoning–action pass and elaborates an iterative debugging procedure, while noting that the minimal pass lacks reflection.
Examples
Uses a code-review skeleton and a test-driven debugging sequence, contrasting them with an illustrative deterministic support route.
What it assumes
Assumes experience calling an LLM API and trying a simple tool-calling or agent workflow, but not shipping an agent or knowing a specific framework.
What you can do after
Practice debugging by observing failures, making bounded changes, running checks, and revising from external evidence.
How it connects
Connects the loop to perception, reasoning, action, reflection, governance, and deterministic pipelines.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Designing AI Agents. The section links point to the parts of the book it draws on.