Skip to content

Architecture Overview

OpenClaw's architecture is built around a single core principle: all messages from every messaging platform converge into one AI Agent runtime. The Gateway serves as the central hub -- responsible for connections, routing, and management.

Full Architecture Diagram

Core System Layers

OpenClaw is organized into five main layers:

1. Gateway Control Plane

The Gateway is the nerve center of the entire system. It runs a WebSocket service through which all clients (CLI, native apps, Web UI) and channels (Telegram, Discord, etc.) communicate.

Key responsibilities:

ResponsibilitySource FileDescription
WebSocket Serversrc/gateway/server.impl.tsWS server built on the ws library
Authenticationsrc/gateway/auth.tsToken/Password/Tailscale auth
Method Dispatchsrc/gateway/server-methods.tsRoute requests to appropriate handlers
Session Managementsrc/gateway/session-utils.tsSession state, persistence, patching
Config Hot-Reloadsrc/gateway/config-reload.tschokidar watching + incremental reload

2. Channel Abstraction Layer

Each messaging platform is a "channel," unified through the ChannelPlugin interface and complemented by lightweight ChannelDock metadata.

3. Agent Runtime

The Agent runtime handles interaction with AI models, processes tool calls, and manages streaming output. The core entry point is subscribeEmbeddedPiSession().

Execution flow:

4. Plugin System

The plugin system manages the registration of tools, channels, hooks, and HTTP routes through a central registry. It supports hot-loading and lifecycle management.

Plugin registration types:

TypeDescription
PluginToolRegistrationRegister Agent tools (factory pattern)
PluginChannelRegistrationRegister channel plugin + dock metadata
PluginHookRegistrationRegister lifecycle hooks
PluginHttpRegistrationRegister HTTP routes
PluginServiceRegistrationRegister background services

5. CLI

The CLI is built with Commander.js and uses a dependency injection pattern (createDefaultDeps()) to inject channel send functions into commands.

Request-Response Protocol

The Gateway defines a standard request-response-event frame format:

Authorization check levels:

  1. Role check -- operator vs node
  2. Scope check -- admin, read, write, approvals, pairing
  3. Method-level permission -- each method declares required scopes

Data Flow: Sending a Message

The complete message-sending flow spans multiple layers:

Hot-Reload Strategy

The Gateway watches for configuration file changes via chokidar and determines how to handle them according to predefined rules:

Reload TypeDescriptionExamples
restartFull Gateway restart requiredPlugin config, Gateway core config
hotRuntime hot-reloadHooks, Cron, Browser, Channel config
noneIgnore changeIdentity config, Wizard, Logging, Models

Changes are debounced with a 300ms delay to batch rapid, successive file modifications.

Summary

  • Gateway is the hub: WebSocket control plane + method dispatch + authentication
  • Channels unify different platforms through a Plugin + Dock dual-layer abstraction
  • Agent runtime handles AI interaction, tool calls, and streaming output
  • Plugins provide pluggable tools, channels, hooks, and HTTP routes
  • CLI uses Commander.js + dependency injection as the primary user-facing interface

Next: Project Structure

OpenClaw Source Code Tutorial