Skip to main content

Agent Design Patterns

Master the core design patterns that power production AI agents — from ReAct loops to multi-agent orchestration architectures.

SDK Focus hooks.PreToolUse hooks.PostToolUse hooks.Stop settingSources permissionMode: plan

Cognitive Architecture

An agent's cognitive architecture defines how it processes information, makes decisions, and executes tasks. The most common pattern is ReAct (Reasoning + Acting):

┌─────────────────────────────────────────────────────────┐
│                    Agent Runtime                         │
├─────────────────────────────────────────────────────────┤
│                                                          │
│   ┌─────────┐    ┌─────────┐    ┌─────────┐            │
│   │ Observe │ → │  Think  │ → │   Act   │ ─┐          │
│   └─────────┘    └─────────┘    └─────────┘  │          │
│        ↑                                      │          │
│        └──────────────────────────────────────┘          │
│                                                          │
├─────────────────────────────────────────────────────────┤
│   Memory   │   Tools   │   Knowledge   │   Skills       │
└─────────────────────────────────────────────────────────┘

System Layers

A typical agent system can be divided into the following layers:

1. Interface Layer

Entry points for users to interact with the agent, including:

  • CLI command-line interface
  • Web UI / Chat interface
  • API endpoints
  • IDE integrations

2. Core Layer

The agent's core logic, including:

  • LLM Controller - Manages interaction with the language model
  • Task Manager - Task decomposition and execution management
  • Context Manager - Context window management
  • Decision Engine - Decision-making engine

3. Capability Layer

The agent's capability modules:

  • Tools - File operations, shell commands, API calls, etc.
  • Memory - Short-term memory, long-term memory, vector storage
  • Skills - Reusable capability packages

4. Infrastructure Layer

  • LLM providers (OpenAI, Anthropic, local)
  • Vector databases
  • Logging and monitoring
  • Security sandbox

Common Design Patterns

ReAct Pattern

Alternating reasoning and acting is the most basic and practical pattern:

Thought: I should read the config file to understand the project structure.
Action: read_file("config.json")
Observation: {"name": "my-app", ...}
Thought: This is a Node.js project. Next...
Action: ...

Plan & Execute Pattern

Make a full plan first, then execute step by step. Best for complex tasks:

Plan:
1. Analyze requirements
2. Design the solution
3. Implement the code
4. Write tests
5. Validate results

Execute: [execute the plan step by step...]

Reflexion Pattern

Reflect after execution and learn from failures:

Execute → Observe Result → Reflect → Improve → Re-execute

Practical Example: Claude Code Architecture

Claude Code's architecture illustrates a production-grade agent design:

┌────────────────────────────────────────┐
│           Terminal Interface           │
├────────────────────────────────────────┤
│          Conversation Manager          │
├────────────────────────────────────────┤
│   System Prompt  │  Tool Definitions   │
├────────────────────────────────────────┤
│              Claude API                │
├────────────────────────────────────────┤
│ FileSystem │ Shell │ Search │ Memory  │
└────────────────────────────────────────┘

Best Practices

  • Modular design - Tools, memory, and skills should be plug-and-play
  • Error recovery - Agents should recover from errors and retry
  • Safety boundaries - Clearly define agent permissions and limits
  • Observability - Record decision processes for debugging and optimization
  • Context management - Use tokens wisely and avoid limit overruns
Hooks System hooks.ts
import { query } from "@anthropic-ai/claude-agent-sdk";

const response = query({
  prompt: "Refactor the auth module",
  options: {
    permissionMode: "plan",  // Plan before execute
    settingSources: ["project", "local"],

    hooks: {
      PreToolUse: async (input) => {
        console.log(`[PRE] ${input.toolName}`, input.toolInput);
        const start = Date.now();
        // Attach timing data for PostToolUse
        return { allow: true, metadata: { start } };
      },

      PostToolUse: async (input) => {
        const duration = Date.now() - input.metadata?.start;
        console.log(`[POST] ${input.toolName} took ${duration}ms`);
        await logToDatabase({
          tool: input.toolName,
          duration,
          success: !input.result?.isError
        });
      },

      Stop: async (input) => {
        console.log("[STOP] Agent finished, cleaning up...");
        // Final logging, resource cleanup
      }
    }
  }
});

SDK Insight: 12 Hook Events

The hooks system provides 12 event points: PreToolUse, PostToolUse, Notification, UserPromptSubmit, SubagentStart, SubagentStop, PreCompact, PermissionRequest, Stop, SessionStart, SessionEnd, Error. Use them for observability, custom approval workflows, and resource management.

Next Steps

Explore the components of agent architecture in more detail:

Try It: Capstone — Mini Agent Framework

Build a complete agent framework using 7+ SDK features.

  1. Tool registry with allowedTools
  2. Agent loop with query() streaming
  3. Observability via hooks.PreToolUse + hooks.PostToolUse
  4. Error recovery with fallbackModel
  5. Structured output with outputFormat
  6. Session management with resume
  7. Settings from settingSources
  8. Write 5 test cases — at least 4/5 must pass
Gate: P6 Complete — L3 Builder Unlocked — Capstone uses hooks, structured output, fallback, sessions, settings. 5 tests with ≥ 4/5 passing.