# How to Fork and Modify the AI Agent Book for Custom Research and Teaching

> Learn to fork and modify the AI agent book for custom research and teaching. Clone the repository, customize agents, add chapters, and rebuild the PDF.

- Repository: [Bojie Li/ai-agent-book](https://github.com/bojieli/ai-agent-book)
- Tags: how-to-guide
- Published: 2026-07-29

---

**To fork and modify the AI agent book, clone the repository from `bojieli/ai-agent-book`, customize the `SystemHintAgent` in [`chapter2/system-hint/agent.py`](https://github.com/bojieli/ai-agent-book/blob/main/chapter2/system-hint/agent.py), add new chapters to the `book/` directory, and rebuild the PDF using [`book/build_pdf.sh`](https://github.com/bojieli/ai-agent-book/blob/main/book/build_pdf.sh).**

The *AI Agents in Depth* repository is an open-source technical book bundled with runnable experiments, allowing you to fork and modify the AI agent book for your own curriculum, research, or product development. Each chapter combines markdown source files in `book/` with standalone experiment packages in `chapter*/` directories that demonstrate the concepts in practice. The modular architecture lets you extend the core `SystemHintAgent` framework or add entirely new chapters without disrupting existing content.

## Forking and Local Setup

Begin by creating your own copy of the repository and preparing the build environment.

1. **Fork the repository** on GitHub to create `https://github.com/<your-user>/ai-agent-book`.

2. **Clone locally:**

```bash
git clone https://github.com/<your-user>/ai-agent-book.git
cd ai-agent-book

```

3. **Install build dependencies.** The project requires `pandoc` and `xelatex` for PDF generation, plus Python dependencies referenced in the documentation. The build script [`book/build_pdf.sh`](https://github.com/bojieli/ai-agent-book/blob/main/book/build_pdf.sh) orchestrates the compilation process using the `ElegantBook` LaTeX class to produce `AI-Agents-in-Depth-en.pdf`.

## Understanding the Repository Structure

The repository separates content from code to support both reading and experimentation.

- **`book/`** – Contains the markdown source files for the book text (e.g., [`chapter1.md`](https://github.com/bojieli/ai-agent-book/blob/main/chapter1.md), [`chapter2.md`](https://github.com/bojieli/ai-agent-book/blob/main/chapter2.md)).

- **`chapter*/`** – Each chapter has a corresponding directory containing runnable experiments. For example, `chapter2/system-hint/` holds the core agent implementation, while `chapter5/coding-agent/` contains coding-specific demonstrations.

- **[`chapter2/system-hint/agent.py`](https://github.com/bojieli/ai-agent-book/blob/main/chapter2/system-hint/agent.py)** – Houses the central `SystemHintAgent` class used across multiple experiments. This fully-featured agent supports tool calling, TODO-list management, timestamps, and trajectory logging according to the source code in `bojieli/ai-agent-book`.

- **[`README.en.md`](https://github.com/bojieli/ai-agent-book/blob/main/README.en.md)** – The landing page at the repository root (lines 49-62) contains the Table of Contents that you must update when adding new chapters.

## Customizing the Core SystemHintAgent

The `SystemHintAgent` serves as the foundation for most experiments in the book. You can modify its behavior by adjusting the `SystemHintConfig` dataclass or extending its tool set.

### Modifying Configuration

The `SystemHintConfig` dataclass (defined around line 74 in [`agent.py`](https://github.com/bojieli/ai-agent-book/blob/main/agent.py)) toggles features via boolean flags:

```python
from chapter2.system_hint.agent import SystemHintAgent, SystemHintConfig

my_config = SystemHintConfig(
    enable_timestamps=False,      # Disable timestamp injection

    enable_tool_counter=True,     # Track tool usage statistics

    enable_todo_list=True,        # Enable task management

    enable_detailed_errors=True,  # Verbose error reporting

)

agent = SystemHintAgent(
    api_key="YOUR_API_KEY",
    provider="openrouter",
    model="gpt-4o-mini",
    config=my_config,
)

```

### Adding Custom Tools

To extend functionality, modify the `_get_tools_description` method (lines 73-94) to append new tool definitions to the list returned to the LLM. Then implement the corresponding handler method using the `_tool_<name>` pattern:

```python

# In _get_tools_description, add:

{
    "type": "function",
    "function": {
        "name": "search_web",
        "description": "Perform a quick web search",
        "parameters": {
            "type": "object",
            "properties": {
                "query": {"type": "string", "description": "Search query"}
            },
            "required": ["query"]
        }
    }
}

# Then implement the handler:

def _tool_search_web(self, query: str) -> Dict[str, Any]:
    import requests, bs4
    resp = requests.get(f"https://html.duckduckgo.com/html/?q={requests.utils.quote(query)}")
    soup = bs4.BeautifulSoup(resp.text, "html.parser")
    first = soup.select_one(".result__title a")
    return {
        "success": bool(first),
        "title": first.text if first else None,
        "url": first["href"] if first else None
    }

```

The agent automatically exposes new tools to the LLM and writes execution details to [`trajectory.json`](https://github.com/bojieli/ai-agent-book/blob/main/trajectory.json) by default, enabling reproducible research and debugging.

## Adding New Chapters

When you fork and modify the AI agent book, you typically add new content by following the established chapter pattern:

1. **Create the markdown source** in [`book/chapter11.md`](https://github.com/bojieli/ai-agent-book/blob/main/book/chapter11.md) (or your next available number).

2. **Create the experiment directory** at `chapter11/` with a [`README.en.md`](https://github.com/bojieli/ai-agent-book/blob/main/README.en.md) that lists companion projects and implementation details.

3. **Update the Table of Contents** in the root [`README.en.md`](https://github.com/bojieli/ai-agent-book/blob/main/README.en.md) to include your new chapter in the overview table.

4. **Add runnable experiments** in subdirectories like [`chapter11/prompt2code/demo.py`](https://github.com/bojieli/ai-agent-book/blob/main/chapter11/prompt2code/demo.py). Each experiment typically imports from `chapter2/system_hint.agent` and sets up environment variables (such as `OPENAI_API_KEY`) via `dotenv` before execution.

For example, to run the coding agent experiment from Chapter 5 after modification:

```bash
export OPENAI_API_KEY="your-key-here"
python chapter5/coding-agent/demo.py

```

## Building the PDF and EPUB

After modifying markdown sources or adding chapters, regenerate the book artifacts:

```bash
cd book
bash build_pdf.sh

```

This script invokes `pandoc` with `xelatex` to compile the markdown into `AI-Agents-in-Depth-en.pdf`. For multilingual EPUB editions, refer to the instructions in [`EPUB.md`](https://github.com/bojieli/ai-agent-book/blob/main/EPUB.md) at the repository root.

## Summary

- **Fork** the `bojieli/ai-agent-book` repository and install `pandoc`/`xelatex` to enable PDF builds.
- **Customize** the `SystemHintAgent` via `SystemHintConfig` or by adding tools to `_get_tools_description` in [`chapter2/system-hint/agent.py`](https://github.com/bojieli/ai-agent-book/blob/main/chapter2/system-hint/agent.py).
- **Extend** by creating new markdown files in `book/` and corresponding experiment folders in `chapter*/`, updating [`README.en.md`](https://github.com/bojieli/ai-agent-book/blob/main/README.en.md) with new entries.
- **Build** using [`book/build_pdf.sh`](https://github.com/bojieli/ai-agent-book/blob/main/book/build_pdf.sh) after modifications to generate the final PDF.

## Frequently Asked Questions

### What dependencies are required to build the book locally?

You need `pandoc` and `xelatex` installed on your system to execute [`book/build_pdf.sh`](https://github.com/bojieli/ai-agent-book/blob/main/book/build_pdf.sh). Python dependencies for running the experiments vary by chapter but typically require `openai` or compatible SDKs, plus `python-dotenv` for environment management. Each experiment directory may contain its own [`requirements.txt`](https://github.com/bojieli/ai-agent-book/blob/main/requirements.txt) or installation instructions.

### Can I use the SystemHintAgent for commercial projects?

The repository is open-source, allowing you to fork and modify the AI agent book for research, teaching, or product development. Check the repository's license file (typically MIT or similar in `bojieli/ai-agent-book`) for specific commercial use terms, though the modular design explicitly supports standalone usage of the agent framework.

### How do I debug agent behavior when adding new tools?

The `SystemHintAgent` automatically writes [`trajectory.json`](https://github.com/bojieli/ai-agent-book/blob/main/trajectory.json) after each iteration, capturing the full conversation history, tool calls, and responses. Examine this file to trace execution paths. You can also enable `enable_detailed_errors=True` in your `SystemHintConfig` to surface more verbose error messages during development.

### Why does my PDF build fail with font errors?

The [`build_pdf.sh`](https://github.com/bojieli/ai-agent-book/blob/main/build_pdf.sh) script uses the `ElegantBook` LaTeX class, which requires specific Chinese and mathematical fonts. Ensure you have a complete TeX Live distribution with CJK (Chinese, Japanese, Korean) support installed, or modify the LaTeX template in `book/` to use standard fonts if you only need English output.