> For the complete documentation index, see [llms.txt](https://www.openclawbook.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.openclawbook.xyz/en/00-preface/preface.md). Agents can also search and read this documentation through its [MCP server](https://www.openclawbook.xyz/~gitbook/mcp).

# Preface

> **Generated by**: Claude Opus 4.6 (initial draft) / Claude Opus 5 (`v2026.3.9` revision) **Token usage**: Input \~130,000 tokens, Output \~12,000 tokens (this chapter) **Source baseline**: `v2026.3.9` (`v2026.3.8-170-g665f67726`)

***

## Why Write This Book

By the end of 2025, the capabilities of Large Language Models (LLMs) have evolved from "can chat" to "can do things." AI Agents are no longer just concepts in research papers—they are now taking over email replies, schedule management, code generation, information retrieval, and other daily tasks. However, most AI assistants on the market run in the cloud, with your conversation history, preferences, and work files all hosted on third-party servers.

OpenClaw offers a different path: **run a complete AI assistant on your own device**. It connects to the messaging platforms you already use (WhatsApp, Telegram, Slack, Discord, Signal, iMessage, etc.), allowing you to chat with it on any platform, while all data—conversation history, memory files, configuration—is saved on your local disk.

OpenClaw is an open-source project (MIT License). As of this book's source baseline, `src/` holds roughly **507,000 lines** of TypeScript (excluding tests), alongside 1,935 test files, 40 extension packages, and 52 bundled skills — covering a complete stack from WebSocket gateway, multi-channel routing, and AI agent runtime to browser automation and native macOS/iOS/Android clients (detailed figures in §43.2).

That scale makes it an unusual learning resource. It doesn't just cover the core architectural patterns of modern AI applications; it shows in full **how a production-grade project organizes code, manages complexity, and draws security boundaries**. And it carries a bonus: a substantial share of its 18,000+ commits were authored by coding agents, and the repository's `AGENTS.md` is a publicly inspectable first-hand record of AI-collaborative development (§43.3).

This book aims to guide readers through the source code to deeply understand OpenClaw's architectural design, core mechanisms, and key implementations. After reading this book, you should be able to:

1. **Understand** the complete architecture of a multi-channel AI assistant
2. **Read** OpenClaw's source code and trace the implementation path of any feature
3. **Build** similar AI assistant products yourself

## Target Audience

This book is intended for readers with some programming foundation. Typical profiles include:

* **Computer Science Undergraduates**: Have studied data structures, operating systems, computer networks, and other foundational courses
* **Junior to Mid-level Developers**: Have JavaScript/TypeScript experience and want to deeply understand AI Agent architecture
* **Technology Enthusiasts**: Interested in local AI assistants and want to understand the engineering implementation behind them

You don't need to be a TypeScript expert, but you should be able to read basic TypeScript code. For concepts not commonly found in computer science textbooks (such as WebSocket Control Plane, vector embeddings search, Chrome DevTools Protocol, etc.), this book will provide derivative explanations when they first appear.

## Book Organization

The book is divided into eleven parts, 45 chapters plus 7 appendices.

### Three layers

The book runs shallow-to-deep, but **you don't have to read it in order**. The three layers stand on their own:

| Layer              | Parts                  | Read it to                                            | Source required?                      |
| ------------------ | ---------------------- | ----------------------------------------------------- | ------------------------------------- |
| **Usage**          | Part I (Ch. 1–10)      | Install and use OpenClaw                              | No                                    |
| **Implementation** | Parts II–X (Ch. 11–42) | Understand how each subsystem works                   | Yes                                   |
| **Insight**        | Part XI (Ch. 43–45)    | Extract transferable design lessons and risk judgment | Better after the implementation layer |

### The parts

| Part        | Content                                                                                                          | Chapters      |
| ----------- | ---------------------------------------------------------------------------------------------------------------- | ------------- |
| Part I      | Getting started: installation, channels, conversation, automation, Skills, model config, multi-agent             | Ch. 1–10      |
| Part II     | Gateway control plane: server, protocol, sessions, routing                                                       | Ch. 11–14     |
| Part III    | AI agent runtime: Pi Agent, model management, prompts, streaming                                                 | Ch. 15–18     |
| Part IV     | Multi-channel messaging: adapter layer, core channels, extension mechanism                                       | Ch. 19–21     |
| Part V      | Tools and automation: tool architecture, Bash, browser, Canvas, Cron, nodes                                      | Ch. 22–27     |
| Part VI     | Memory, skills, ecosystem: vector retrieval, skill platform, hook system                                         | Ch. 28–30     |
| Part VII    | Security, config, infrastructure: config system, security model, CLI, plumbing                                   | Ch. 31–34     |
| Part VIII   | Client apps and Web UI: Lit console, native apps                                                                 | Ch. 35–36     |
| Part IX     | Deployment and operations: deployment options, monitoring and troubleshooting                                    | Ch. 37–38     |
| Part X      | Advanced topics and practice: multi-agent, ACP, TUI, hands-on project                                            | Ch. 39–42     |
| **Part XI** | **Source reading and design insight: engineering panorama, seven design themes, risks and transferable lessons** | **Ch. 43–45** |

> **Edition note**: Part I currently exists in full only in the Chinese edition; the English edition has the two technical introduction chapters. See the README's Edition Status table.

### On "Source Deep Dive" sections

From Chapter 11 onward, many chapters end with a 🔬 **Source Deep Dive** section. These sit closer to the source than the main text and carry higher information density, tracing real implementations line by line along with the issues behind them (many comments cite GitHub issue numbers directly).

> **The main text is for understanding structure; the deep dive is for understanding trade-offs.**
>
> If you only want to know *what this piece does*, the main text suffices. If you're going to modify it, or reproduce it in your own project, read the deep dive.

### Recommended reading paths

* **Just want to use it**: Part I is enough; no source code required.
* **Want to understand the implementation**: start with the four source-reading paths in §43.4, then enter Chapters 11–42 as needed.
* **Want to steal the design**: start directly from Chapter 44 (seven design themes) and §45.1 (ten transferable patterns), and back-reference chapters when a concept is unfamiliar.
* **Hands-on**: Chapter 42 builds a project from scratch — best read while building.

## Source Code Version

This book is based on the source of OpenClaw **`v2026.3.9`**, corresponding to the git descriptor `v2026.3.8-170-g665f67726`. Source repository:

```
https://github.com/openclaw/openclaw
```

Cloning it locally is recommended so you can follow along:

```bash
git clone https://github.com/openclaw/openclaw.git
cd openclaw
git checkout v2026.3.8   # nearest tag to this book's baseline
```

### On version drift

OpenClaw uses CalVer and iterates fast — by the time you read this, many details have probably changed: a file split, a function renamed, a degradation chain grown a level.

The book handles this on two levels:

* **Specific line numbers expire.** Where line numbers appear they are accurate as of `v2026.3.9` and are meant for locating code, not as stable references.
* **Shapes don't expire.** The seven design themes in Chapter 44 and the ten patterns in §45.1 were selected precisely because they hold up outside this project.

When something doesn't match, §43.4 and Appendix D give three verification techniques — in particular, **use `CHANGELOG.md` to check whether a feature is alive**. That's how this revision discovered a feature that had already been removed (see §32.5.2, one of the most instructive cautionary cases in the book).

## Prerequisites

Reading this book requires the following foundational knowledge:

* **TypeScript / JavaScript Basics**: Variables, functions, classes, modules, Promise/async-await
* **Node.js Runtime Concepts**: Event loop, module system (ESM), Buffer, Stream
* **Basic Network Knowledge**: HTTP, WebSocket, TCP/IP
* **Command Line Operations**: Terminal usage, npm/pnpm package management
* **Git Version Control**: Clone, branches, tags

The following knowledge is helpful but not required—this book will explain as needed:

* Docker containerization
* Basic principles of Large Language Models (LLMs)
* Databases (SQLite)
* WebSocket protocol details
* Browser automation (Playwright/CDP)

## Conventions

* Source paths are relative to the OpenClaw repository root, e.g. `src/gateway/server.impl.ts`
* Line-numbered references look like `attempt.ts:774`, accurate as of the `v2026.3.9` baseline
* Code snippets use TypeScript syntax highlighting; **any snippet marked "(simplified)" has been trimmed** — consult the source for the full implementation
* 🔬 marks a "Source Deep Dive" section
* Each document notes the AI model used, token usage, and source baseline

> **A TypeScript gotcha**: the source is `.ts`, but ESM import statements are written with `.js` — `import { x } from "./foo.js"` actually refers to `foo.ts`. Looking for the `.js` file on disk won't find it.

***

> *Let's begin.*
