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
