Skip to content

13. Building a Simple Agent: Architecture Procedure ​

This chapter gives a reusable design procedure. When asked to build a simple agent, do not start by choosing a framework. Start by defining the architecture.

Step 1: Define the Task Boundary ​

Answer these questions before designing components.

QuestionWhy it matters
What is the exact goal?Prevents the agent from optimizing for plausible but irrelevant output
What counts as success?Enables final-answer verification
What is out of scope?Limits tool selection and permission design
What actions may the agent take?Defines the action space
What actions are forbidden?Defines guardrails
Who or what approves risky actions?Defines autonomy level

A simple agent performs best when its task boundary is narrow and well-defined. Broad autonomy without clear scope leads to unpredictable tool selection, permission creep, and failures that are hard to diagnose.

Step 2: Choose the Autonomy Level ​

Autonomy levelDesign consequence
Recommend onlyNo automatic side effects
Read-only autonomyAgent may search and inspect, but not mutate
Bounded mutationAgent may modify a scoped workspace
External actionAgent may call external systems, with approval or strict policy

Start with the lowest autonomy level that can solve the task (2).

Step 3: Define the Context Layout ​

Design the context before writing prompts.

Context sectionContent
SystemRole, constraints, output protocol, safety rules
TaskGoal, inputs, success criteria
ToolsAvailable tools and argument contracts
StateCurrent plan, current step, known facts
HistorySummarized prior steps
ObservationLatest tool result or environment feedback
ReserveSpace for the model’s next output

The context layout should make the next decision easy.

Step 4: Define the Output Protocol ​

The model must produce intents that the harness can parse.

A minimal protocol needs:

IntentUse
Tool requestAsk the harness to execute a tool
Final answerDeclare task complete
ClarificationAsk for missing information
AbortStop because the task cannot or should not continue

Keep the protocol small. Complex output protocols increase format-drift risk.

Step 5: Select Tools ​

Choose tools based on the action space.

Design questionGuideline
Does the agent need to read information?Add read-only retrieval tools
Does it need to compute?Add computation tools
Does it need to mutate state?Add scoped mutation tools
Does it need to produce an artifact?Add artifact storage or final submission tools
Does it need human help?Add clarification or escalation tools

Prefer a small set of well-described tools.

Step 6: Design the Loop ​

A simple production loop needs:

text
Start
  |
  v
Build context
  |
  v
Call model
  |
  v
Parse intent
  |
  v
Validate intent
  |
  +-- tool request ----> execute tool ----> record observation
  |
  +-- final answer ----> verify answer ----> stop
  |
  +-- clarification ----> ask user ----> wait or stop
  |
  +-- abort -----------> record reason ----> stop

Add:

  • Maximum iterations
  • Token budget
  • Cost budget
  • Timeout
  • Repeated-action detection
  • Explicit stop reason

Step 7: Add Guardrails ​

At minimum, a simple agent needs:

GuardrailPurpose
Tool allowlistPrevents hallucinated tools
Argument validationPrevents invalid or unsafe parameters
Permission scopeLimits what tools can touch
Iteration capPrevents infinite loops
Cost capPrevents runaway spend
Final-answer checkPrevents unsupported completion claims
Audit logPreserves evidence of decisions

Step 8: Design Memory and State ​

For a simple agent, memory can start small.

State itemNeeded?
Current stepYes
Goal summaryYes
Known factsUsually
Tool result referencesYes, if results are large
Error historyYes, if retries are allowed
Long-term user memoryOnly if the task requires it

Do not add long-term memory until short-term context management is stable.

Step 9: Add Observability ​

A minimal trace should record:

  • Run ID
  • Step number
  • Model input size
  • Model output
  • Parsed intent
  • Validation result
  • Tool name and status
  • Observation summary
  • Stop reason
  • Token and cost totals

If a failure cannot be reconstructed from the trace, the observability design is incomplete.

Step 10: Test Failure Modes ​

Before trusting the agent, test:

Failure scenarioExpected behavior
Model returns invalid outputValidator rejects and retries or stops
Model requests unknown toolTool allowlist denies
Tool fails transientlyControlled retry or structured error observation
Tool returns huge outputObservation is summarized or stored externally
Agent repeats same actionRepetition detector stops or escalates
Context approaches limitSummarization or truncation triggers
Task is impossibleAgent aborts or asks for clarification
Unsafe action is requestedGuardrail blocks and logs

Minimal Reference Architecture ​

text
+---------------------------------------------------------------------+
|                        Simple Agent Harness                         |
|                                                                     |
|  +----------+   +-------------+   +----------------+                |
|  | Context  | --> | Model Call| --> | Intent Parser |                |
|  | Builder  |   +-------------+   +----------------+                |
|  +----------+            ^                       |                  |
|      ^                   |                       v                  |
|      |                   |              +----------------+          |
|  +---+---------+         |              | Validator /    |          |
|  | State /     |         |              | Guardrail      |          |
|  | Memory      |         |              +----------------+          |
|  +---+---------+         |                       |                  |
|      ^                   |                       v                  |
|      |                   |              +----------------+          |
|  +---+---------+         |              | Tool Executor  |          |
|  | Observation | <-------+--------------+----------------+          |
|  | Recorder    |                                |                  |
|  +--------------+-------------------------------+                  |
|                                                                     |
|  +----------------+   +----------------+   +----------------+       |
|  | Trace Writer   |   | Budget Tracker |   | Stop Controller|       |
|  +----------------+   +----------------+   +----------------+       |
+---------------------------------------------------------------------+
                                  |
                                  v
                         +------------------+
                         |   LLM Engine     |
                         | Stateless model  |
                         +------------------+