Main Functionalities of the AI Agent Book: 10 Modular Building Blocks for LLM Agents

The AI Agent Book repository provides ten distinct functionalities—ranging from web search and persistent memory to code generation and multi-agent collaboration—implemented as standalone Python projects that share a unified REACT architecture.

This repository serves as the companion codebase for "AI Agents in Depth: Design Principles and Engineering Practice," offering runnable implementations of every concept covered in the ten-chapter curriculum. Each functionality is delivered as a self-contained module demonstrating specific engineering patterns for production-grade LLM agents.

The Ten Core Functionalities

The repository organizes agent capabilities into ten progressive chapters, each addressing a specific engineering challenge:

  1. Agent Fundamentals – Implements the universal formula Agent = LLM + Context + Tools, featuring minimal agents that showcase multi-round tool use and basic REACT loops through WebSearchAgent and SearchCodegenAgent.

  2. Context Engineering – Provides utilities for KV-cache management, prompt optimization, and context compression via configurable context modes in chapter1/context/quickstart.py.

  3. User Memory & Knowledge Bases – Delivers persistent cross-session storage through UserMemoryAgent, supporting RAG, structured indexes, and knowledge graphs with CRUD operations for facts and enhanced notes.

  4. Tools – Establishes the tool-description → execution → REACT pipeline through methods like _get_tools_description and _execute_tool, enabling async tool calls and proactive discovery.

  5. Coding Agent & Code Generation – Treats LLMs as "tool creators" via CodingAgent in chapter5/coding-agent/agent.py, featuring end-to-end code generation, compilation, and sandboxed execution.

  6. Agent Evaluation – Supplies metrics, statistical testing frameworks, and benchmark suites in chapter6/evaluation/ for running reproducible experiments across 93 distinct test scenarios.

  7. Model Post-Training – Contains SFT and RL pipelines in chapter7/AdaptThink and chapter7/verl for converting base LLMs into tool-aware models through sample-efficient fine-tuning.

  8. Agent Self-Evolution – Implements learning-from-experience loops in chapter8/prompt-distillation/ and chapter8/self-evolution/, where agents bootstrap new tools using their own outputs as training data.

  9. Multimodal & Real-Time Interaction – Connects LLMs to physical devices through voice, GUI, and robotics interfaces located in chapter9/browser-use/ and chapter9/claude-quickstarts/.

  10. Multi-Agent Collaboration – Enables agent societies with shared context and emergent behaviors via frameworks in chapter10/ (including TalkAct and Stanford AI Town implementations).

The Unified REACT Architecture

All ten functionalities share a common execution loop implemented consistently across agent classes. This architecture appears in WebSearchAgent._chat, UserMemoryAgent.execute_task, and the streaming handlers in UserMemoryAgent._chat_stream.

The loop follows four stages:

  • Prompt Construction – A system message encoding the agent's role, context window, and available tools.
  • LLM Inference – The model returns either a final answer or a structured tool call (assistant → tool).
  • Tool Execution – The _execute_tool method runs the requested function, captures observations, and injects results back into context.
  • Iteration Control – The cycle repeats until the LLM emits a final answer or reaches the maximum iteration limit defined in config.py.

This design allows developers to swap LLM providers (Moonshot/Kimi, OpenRouter, SiliconFlow, Doubao) and register new tools without modifying core REACT logic. Provider resolution and API key handling are centralized in config.py, ensuring consistent timeout and retry behavior across all modules.

Implementation Deep Dive

Web Search and Code Generation (Chapter 1)

The foundational agents demonstrate the REACT pattern with builtin tool integration. In chapter1/web-search-agent/agent.py (lines 86-99), the WebSearchAgent class implements the search-and-answer workflow:

from chapter1.web-search-agent.agent import WebSearchAgent

agent = WebSearchAgent(verbose=True)
answer = agent.search_and_answer(
    "What is the latest version of Moonshot's Context Caching technology?"
)
print("Answer:", answer)

The SearchCodegenAgent extends this pattern by adding a code sandbox tool, allowing the agent to generate and execute Python scripts dynamically.

Persistent Memory Management (Chapter 3)

The UserMemoryAgent in chapter3/user-memory/agent.py introduces sophisticated memory handling through the memory_manager module. Key methods include _get_memory_context (lines 90-150) for retrieval and _tool_add_memory (lines 320-400) for persistence:

from chapter3.user-memory.agent import UserMemoryAgent, UserMemoryConfig, MemoryMode

cfg = UserMemoryConfig(
    memory_mode=MemoryMode.ENHANCED_NOTES,
    enable_memory_updates=True,
    enable_conversation_history=True,
)

agent = UserMemoryAgent(
    user_id="user-123",
    provider="kimi",
    config=cfg,
    verbose=True
)

agent.execute_task("Remember that the user works as a senior ML engineer at TechCorp.")
reply = agent.chat("Can you summarise what I do at work?")

The memory system supports three storage modes—facts, JSON cards, and enhanced notes—automatically injecting relevant context into prompts via conversation_history.py.

Coding Agent with Sandbox (Chapter 5)

chapter5/coding-agent/agent.py (lines 70-110) implements a specialized REACT loop that treats generated code as a tool output. The CodingAgent generates Python functions, executes them in a sandboxed environment, and feeds runtime results back into the reasoning chain:

from chapter5.coding-agent.agent import CodingAgent

coder = CodingAgent(
    api_key="your-key",
    model="google/gemini-1.5-flash",
    verbose=True
)

result = coder.execute_task(
    "Write a function `fib(n)` that returns the nth Fibonacci number."
)
print(result["final_answer"])

Key Source Files

Understanding the repository structure requires familiarity with these critical paths:

Summary

  • The AI Agent Book provides ten modular functionalities covering the complete agent lifecycle from basic tool use to multi-agent societies.
  • All modules implement a consistent REACT architecture with standardized prompt handling, tool execution, and iteration control.
  • Provider-agnostic design in config.py enables seamless switching between Moonshot, OpenRouter, and other LLM APIs.
  • Memory and context engineering capabilities in Chapter 3 demonstrate production-grade RAG and persistent user profiling.
  • Evaluation and training frameworks in Chapters 6-8 support quantitative assessment and model self-improvement.

Frequently Asked Questions

What is the AI Agent Book repository?

The AI Agent Book repository is the official companion codebase for the book "AI Agents in Depth: Design Principles and Engineering Practice." It contains ten standalone Python projects, each implementing a specific chapter's concepts—from web search agents to multi-agent collaboration frameworks—allowing developers to run and extend every example discussed in the text.

How does the REACT loop work in the AI Agent Book implementations?

The REACT loop follows a four-stage cycle implemented in classes like WebSearchAgent and UserMemoryAgent: (1) construct a system prompt with available tools and context, (2) call the LLM which returns either an answer or a tool request, (3) execute the tool via _execute_tool and capture observations, and (4) iterate until the LLM produces a final answer or hits the iteration limit defined in config.py.

Can I use different LLM providers with these agent implementations?

Yes. The repository uses a centralized provider resolution system in config.py that supports Moonshot/Kimi, OpenRouter, SiliconFlow, Doubao, and other OpenAI-compatible APIs. You can switch providers by changing the provider parameter in agent constructors (e.g., provider="kimi" or provider="openrouter") without modifying the core REACT logic.

What types of memory systems are available in Chapter 3?

Chapter 3 implements three memory modes through UserMemoryConfig: Facts (simple key-value storage), JSON Cards (structured data schemas), and Enhanced Notes (rich paragraph storage). The UserMemoryAgent automatically retrieves relevant memories using the memory_manager module and injects them into the prompt context during the REACT loop's prompt construction phase.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →