Architecture
OpenClaw is built as a local-first Gateway that connects AI agents to messaging channels. This page explains the system architecture, component relationships, and data flow.High-Level Architecture
Core Components
Gateway
The Gateway is the central control plane that runs atws://127.0.0.1:18789 (by default).
Responsibilities:
- WebSocket server for real-time communication
- HTTP server for Control UI and webhooks
- Session management and routing
- Authentication and authorization
- Channel lifecycle management
- Event broadcasting
- Single WebSocket endpoint for all clients
- Background process with daemon support (launchd/systemd)
- Lock file prevents multiple instances
- Configurable bind modes (loopback, LAN, Tailscale)
- Optional TLS support
Channels
Channels are messaging platform integrations that connect to the Gateway. Supported Channels:- WhatsApp (Baileys library)
- Telegram (grammY library)
- Discord (discord.js)
- Slack (Bolt SDK)
- Signal (signal-cli)
- iMessage (macOS only, AppleScript bridge)
- Google Chat, Microsoft Teams, Matrix, and more via extensions
- Channel initializes and authenticates
- Channel registers with Gateway
- Inbound messages routed to sessions
- Outbound messages delivered through channel
Sessions
Sessions provide conversation context isolation for each sender. Session Key Format:agent:main:telegram:default:user123
Session Features:
- Per-sender isolation
- Group session sharing
- Session pruning and compaction
- Conversation history storage
- Model and settings per session
- Session data:
~/.openclaw/sessions/ - Format: JSONL files (one line per turn)
Agents
Agents are AI model runtimes that process messages and generate responses. Agent Types:- Main Agent: Default agent for direct messages
- Multi-Agent: Route channels/groups to specialized agents
- Sub-Agents: Spawned by sessions for delegation
- Pi agent runtime with RPC mode
- Tool streaming and block streaming
- Workspace isolation
- Skill management
- Model fallbacks
Router
The Router determines which agent handles each message based on channel, account, and peer. Routing Logic:- Extract channel, account ID, peer ID from message
- Check routing bindings for explicit matches
- Fall back to default agent if no match
- Create or load session for agent + routing key
Tools
Tools extend agent capabilities with external actions. Built-in Tools:- bash: Execute shell commands
- read/write/edit: File operations
- browser: Web automation with Playwright
- canvas: Visual workspace control
- nodes: Device-specific actions (camera, screen, etc.)
- cron: Schedule jobs
- sessions_*: Inter-session communication
- gateway: Gateway control
Data Flow
Inbound Message Flow
Outbound Message Flow
Session Lifecycle
Protocol
Gateway WebSocket Protocol
Clients connect tows://127.0.0.1:18789 and use JSON-RPC 2.0 messages.
Authentication:
agent.text: Streaming text responseagent.thinking: Extended thinking eventsagent.tool.start: Tool invocation startedagent.tool.result: Tool completedpresence: Channel presence updatestyping: Typing indicators
Agent RPC Protocol
The Gateway communicates with the Pi agent via JSON-RPC over stdin/stdout. Request:Security Architecture
Default DM Pairing
Unknown senders receive a pairing code and are blocked until approved:~/.openclaw/allowlists/{channel}.json
Sandbox Isolation
Group chats run in Docker containers to isolate tool execution:session: One container per session (most isolated)agent: One container per agent (shared workspace)shared: Single container for all sandboxed sessions
Authentication Modes
none: No authentication (localhost only)token: Bearer token (X-Gateway-Token header)password: Password authenticationtrusted-proxy: Identity from reverse proxy headers
File System Layout
Deployment Patterns
Local Development
VPS Deployment
Split Deployment
Performance Considerations
WebSocket Connection Limits
The Gateway can handle hundreds of simultaneous WebSocket connections. Practical limits:- Channels: 10-20 concurrent (one per messaging platform)
- Platform apps: 5-10 (macOS, iOS, Android, web)
- CLI clients: As needed for automation
Session Scaling
Sessions are loaded on demand and cached in memory:- Cold load: ~5-10ms per session
- Warm cache: less than 1ms
- Recommended: Prune old sessions periodically
Message Throughput
Typical throughput:- Inbound: 100+ messages/second
- Outbound: 50+ messages/second per channel (respecting rate limits)
- Agent processing: Limited by AI provider API
Extension Points
Plugin System
OpenClaw supports plugins for extending functionality: Hook Types:channel: Add new messaging platformstool: Add agent toolsprovider: Add AI model providersgateway: Add Gateway features
Next Steps
Gateway Details
Deep dive into Gateway configuration and operation
Agent System
Learn about AI agent runtime and configuration
Session Management
Understand session lifecycle and storage
Message Routing
Configure multi-agent routing rules

