MDX Limo
OpenClaw: A First-Principles Deep Dive

OpenClaw: A First-Principles Deep Dive

Everything you need to understand the system design, architecture, and philosophy of the fastest-growing open-source AI agent framework.


What Is OpenClaw, Really?

Forget the hype for a moment. At its core, OpenClaw answers one question:

What if your AI assistant wasn't a website you visit, but a background process on your computer — always running, connected to your real messaging apps, able to read files, run commands, and take action on your behalf?

That's it. OpenClaw is a persistent daemon that connects large language models (Claude, GPT, Gemini, local models) to the places you already communicate — WhatsApp, Telegram, Slack, Discord, Signal, iMessage — and gives the AI the ability to do things, not just talk.

The key mental model: OpenClaw is closer to an operating system than a chatbot. It runs continuously, monitors things proactively, manages its own memory, and uses "skills" the way your phone uses apps.


The Big Picture: How Everything Fits Together

Before we go deep on any single piece, here's the full system at a glance:

Everything orbits one central piece: the Gateway.


Part 1: The Gateway — One Process To Rule Them All

Why a Single Process?

Most modern software breaks things into microservices — separate programs for separate concerns, talking over networks. OpenClaw does the opposite. One Node.js process handles everything:

  • Connecting to messaging platforms
  • Managing conversation state
  • Running the AI agent loop
  • Executing tools (shell commands, web searches, file operations)
  • Serving the control API for client apps
  • Persisting memory to disk

Why? Simplicity and reliability. No container orchestration, no service discovery, no distributed state bugs. You run openclaw gateway and the whole system comes alive. You stop it and everything stops cleanly.

The Five Internal Subsystems

Inside that single process, five subsystems work in concert:

Let's understand each one.

1. Channel Adapters take messages from WhatsApp, Telegram, Discord, etc. and normalize them into a common format. Each platform has its own quirks — WhatsApp uses the Baileys library, Telegram uses grammY, Discord uses discord.js — but the rest of the system sees a uniform message shape. Think of this as a universal translator.

2. The Session Manager figures out who is talking and which conversation this belongs to. If you DM the agent on WhatsApp and then DM it on Telegram, should those be the same conversation? That depends on your config. The session manager resolves this using hierarchical session keys (more on this later).

3. The Lane Queue is a deliberate design trade-off: one conversation runs one agent turn at a time. If three messages arrive quickly for the same conversation, they don't spawn three parallel agent runs (which could race and produce incoherent results). Instead, they're queued. This sacrifices speed for correctness.

4. The Agent Runtime is the brain. It assembles context from your markdown config files, conversation history, and memory — then calls the LLM, handles tool calls, feeds results back, and repeats until the model is done. This is the same loop pattern as Claude Code: input → context → model → tools → repeat → reply.

5. The Control Plane exposes a WebSocket API on port 18789. Every client — the CLI, the macOS menu bar app, the web UI, the iOS/Android apps — connects here. It uses typed JSON frames with a challenge-response handshake for authentication.

Gateway Startup: What Happens When You Run It

When you execute openclaw gateway run, here's the sequence:

Notice step 8 — delivery recovery. If the gateway crashed while sending a message, it picks up where it left off. This is a daemon that's designed to be always-on and resilient.

How the Gateway Multiplexes One Port

A clever trick: the Gateway serves both HTTP and WebSocket on the same port (18789). When a connection comes in:

  • If it's a regular HTTP request → route to the HTTP API (OpenAI-compatible endpoints, tool invocation, control UI)
  • If it's an HTTP Upgrade request → hand off to the WebSocket server (control plane for CLI/apps)

This means you only need to open one port, which simplifies firewall rules and deployment.


Part 2: Markdown-First Configuration — The Radical Idea

Why Markdown Instead of Code?

This is OpenClaw's most distinctive philosophical choice. In other frameworks:

FrameworkHow you configure agent behavior
LangChainWrite Python pipeline code
CrewAIDefine roles in Python classes
AutoGenCode conversation patterns
OpenClawEdit a text file

OpenClaw's insight: language models think in natural language, so you should configure them in natural language.

Your agent's entire personality, rules, and behavior are defined in a handful of Markdown files that live in ~/.openclaw/workspace/:

1~/.openclaw/workspace/ 2├── SOUL.md ← Who the agent IS (personality, tone, values) 3├── AGENTS.md ← Operating instructions 4├── USER.md ← Who YOU are (preferences, context) 5├── HEARTBEAT.md ← What to proactively monitor 6├── MEMORY.md ← Long-term curated knowledge 7├── IDENTITY.md ← Name, emoji, vibe 8├── TOOLS.md ← Notes about available tools 9├── BOOTSTRAP.md ← One-time first-run setup 10└── memory/ 11 ├── 2026-02-20-project-kickoff.md 12 └── 2026-02-24-budget-review.md

SOUL.md might look like:

1You are a direct, efficient assistant. You prefer action over discussion. 2 3Rules: 4- Never send emails without explicit confirmation 5- Always use metric units unless I specify otherwise 6- When scheduling meetings, default to 30 minutes 7- Respond in the same language I message you in

HEARTBEAT.md might look like:

1Every 30 minutes, check: 2- Are there any new emails from my boss? 3- Has the CI pipeline status changed? 4- Any Slack messages mentioning my name that I haven't seen? 5 6Only notify me if something actually needs my attention.

The consequence: a non-developer can meaningfully customize their AI agent by editing text files. No Python, no JSON schemas, no API contracts. This is a fundamental accessibility shift.

How Markdown Files Become a System Prompt

At agent execution time, these files are assembled into the system prompt the LLM receives:

There are hard limits to prevent prompt bloat: 20,000 characters per file, 150,000 characters total. If your MEMORY.md grows too large, it gets truncated.

Important detail: Subagent and cron sessions get a leaner prompt — they receive AGENTS.md, TOOLS.md, SOUL.md, IDENTITY.md, and USER.md, but NOT MEMORY.md. This keeps subagent prompts focused.


Part 3: The Message Lifecycle — From "Hey" to Response

Let's trace what happens when you send a WhatsApp message to your OpenClaw agent. This is the most important flow to understand.

Key Steps in Detail

Authorization check: Not everyone who messages your agent gets a response. Unknown senders receive a pairing code — a one-time code that an authorized user must approve. This prevents strangers from using your agent.

Session resolution: The session manager converts the incoming message context (channel + sender + group) into a deterministic session key:

1DM on WhatsApp → agent:main:main (shared main session) 2DM per-peer → agent:main:direct:+15551234567 3Group chat → agent:main:whatsapp:group:120363xxxxx 4Thread in group → agent:main:whatsapp:group:120363xxxxx:thread:abc123

This key determines which conversation history gets loaded and which queue lane the message enters.

The queue decision: If another agent turn is already running for this session, the new message is queued. The queue mode (configurable per-channel or globally) determines what happens:

Queue ModeWhat Happens
collectAll queued messages merge into one next turn
followupEach queued message runs as a separate turn
steerInject the new message into the currently running turn
interruptAbort the current run, start fresh with the new message

The default is collect, which is the safest — if you send three rapid messages, they all get combined into one agent turn instead of spawning three.

The agent retry loop: The agent runtime doesn't just call the LLM once. It has a sophisticated retry loop that handles:

  • Context overflow: If the conversation history is too long, it compacts (summarizes old messages) and retries
  • Auth failures: If an API key is rate-limited, it rotates to the next configured profile
  • Tool failures: Tool errors get fed back to the model so it can try a different approach

The loop runs up to 24–160 iterations depending on how many auth profiles are configured.


Part 4: Sessions — Durable Conversations on Disk

Sessions aren't ephemeral chat windows. They're persisted JSONL files — one line per event — that form a complete audit trail.

Session Storage

1~/.openclaw/sessions/ 2├── sessions.json ← Index of all sessions 3├── a1b2c3d4-main.jsonl ← Main DM transcript 4├── e5f6g7h8-whatsapp-group-120363xx.jsonl ← WhatsApp group transcript 5└── i9j0k1l2-telegram-direct-alice.jsonl ← Telegram DM transcript

Each .jsonl file contains timestamped entries: user messages, assistant responses, tool calls, tool results, system events. This means you can cat a session file and read exactly what happened.

Session Lifecycle Management

Sessions don't grow forever. Lifecycle policies keep them manageable:

When a session resets (via /new command or the daily auto-reset), OpenClaw generates a daily memory file — an LLM-generated summary saved to memory/YYYY-MM-DD-<slug>.md. This is how short-term conversation context graduates into long-term memory.


Part 5: Memory — The File-First Philosophy

OpenClaw's memory system has two layers, and understanding both is crucial.

Layer 1: Markdown Files (The Source of Truth)

All long-term memory is stored as plain Markdown files in your workspace. This is deliberately human-readable:

1<!-- memory/2026-02-20-car-negotiation.md --> 2# Car Purchase Negotiation 3**Session:** 2026-02-20 4**Key facts:** 5- Negotiating with dealer at Springfield Auto 6- Target price: $28,000 for the Honda CR-V 7- Dealer's initial offer: $32,200 8- I countered at $27,500 9- Dealer came down to $30,100 10 11**Next steps:** 12- Wait 48 hours before responding 13- Research comparable listings to strengthen position

You can edit these files. You can version them with git. You can delete ones you don't want the agent to remember. There's no opaque database — it's just text files.

Layer 2: Hybrid Search Index (Finding What Matters)

When the agent needs to recall something, it doesn't scan every file linearly. A hybrid search system combines two approaches:

Vector search understands meaning — it knows "car negotiation" and "vehicle purchase discussion" are about the same thing. Keyword search catches exact matches — names, dollar amounts, code symbols.

The 70/30 weighting means semantic understanding dominates, but exact matches still get found. Temporal decay means recent memories rank higher. MMR (Maximal Marginal Relevance) re-ranking ensures you get diverse results rather than five near-identical matches.

Fallback behavior: If no embedding provider is configured (maybe you're running fully offline), the system gracefully degrades to keyword-only search. It still works — just without semantic understanding.


Part 6: Skills — Apps for Your AI Agent

The Concept

Skills are OpenClaw's answer to app stores. Each skill is a directory containing a SKILL.md file — a Markdown document that teaches the agent how to use a specific capability.

This is not traditional code. A skill is literally an instruction manual that the agent reads when it decides a skill is relevant.

1~/.openclaw/skills/ 2├── github/ 3│ └── SKILL.md ← "Here's how to use the gh CLI..." 4├── gmail/ 5│ └── SKILL.md ← "Here's how to search and send email..." 6├── home-assistant/ 7│ └── SKILL.md ← "Here's how to control smart home devices..." 8└── nano-banana-pro/ 9 └── SKILL.md ← "Here's how to generate images with Gemini..."

How Skills Stay Out of the Way

Here's a critical design insight: only skill metadata goes into the system prompt, not the full content.

1<!-- What the LLM actually sees in its system prompt --> 2<available_skills> 3 <skill> 4 <name>github</name> 5 <description>GitHub operations via gh CLI: issues, PRs, CI...</description> 6 <location>~/.openclaw/skills/github/SKILL.md</location> 7 </skill> 8 <skill> 9 <name>gmail</name> 10 <description>Search, read, compose, and send Gmail messages</description> 11 <location>~/.openclaw/skills/gmail/SKILL.md</location> 12 </skill> 13 <!-- ... up to 150 skills, ~97 chars each --> 14</available_skills>

When the agent decides "I need to use GitHub," it actively reads the full SKILL.md file using the read tool. This means you can have hundreds of skills installed without bloating every single prompt. Brilliant context window management.

The Three-Tier Precedence System

Skills come from multiple sources, and higher-precedence sources override lower ones:

If a bundled skill called github exists and you create your own github skill in your workspace, yours wins. This lets you customize any built-in behavior without forking the project.

Eligibility Gating

Before a skill becomes available, it passes through a gating system:

1# Inside a SKILL.md frontmatter 2metadata: 3 openclaw: 4 os: ["darwin", "linux"] # Only on macOS and Linux 5 requires: 6 bins: ["gh"] # gh CLI must be installed 7 env: ["GITHUB_TOKEN"] # This env var must be set 8 config: ["browser.enabled"] # This config flag must be true

If any requirement isn't met, the skill silently disappears from the agent's available list. The agent never sees capabilities it can't actually use.

ClawHub — The Skill Marketplace

The community has built 5,700+ skills. Installing one is a single command:

1clawhub install home-assistant

The agent can even auto-search for and install skills at runtime based on what you ask it to do, and it can write its own skills — creating new SKILL.md files to teach itself capabilities it doesn't have yet. This creates a self-extending feedback loop.

Security caveat: This openness has attracted malicious actors. Hundreds of skills have been found containing malware, data exfiltration code, and prompt injection. ClawHub now partners with VirusTotal for scanning, and community tools like SecureClaw provide audit checks.


Part 7: The Heartbeat — From Reactive to Ambient

Most AI assistants are reactive: they wait for you to say something, then respond. OpenClaw introduces proactive autonomy through its heartbeat system.

The agent reads HEARTBEAT.md, uses its judgment to decide what to check, and only contacts you if something actually needs your attention. If everything's fine, it responds with HEARTBEAT_OK — a special token that means "nothing to report" — and you never hear from it.

This transforms the agent from a tool you invoke into an ambient layer of computing that continuously monitors and acts. The car negotiation story — where an agent negotiated $4,200 off a purchase via email while the owner slept — was powered by this heartbeat system.


Part 8: Tools — What the Agent Can Actually Do

The agent has a rich set of built-in tools, organized by category:

Tool Policy: Controlling What's Allowed

Not every agent session should have access to every tool. OpenClaw uses a layered deny-wins policy system:

Deny always wins. If any layer blocks a tool, it's blocked — period. An empty allow list means "allow all." This is defense-in-depth.

Tool groups provide bulk control: group:runtime covers exec, bash, and process. You can deny group:runtime to prevent all code execution with a single rule.

Elevated Exec: The Escape Hatch

Normally, when sandbox mode is on, tool execution happens inside a Docker container with no network access and dropped capabilities. But sometimes you need to run something on the actual host — installing a package, managing a service.

Elevated exec provides this escape hatch with three modes:

ModeBehavior
offNo elevated execution ever
askAgent must request approval; you confirm manually
fullAuto-approved (owner senders only)

Even in full mode, only verified owner senders can trigger elevated commands. The system is designed so that every relaxation of security is a deliberate choice.


Part 9: Channel Architecture — Speaking Every Platform's Language

The Adapter Pattern

Each messaging platform has wildly different APIs, message limits, formatting rules, and capabilities. OpenClaw abstracts this through channel adapters:

Each adapter declares its capabilities — does it support threads? Reactions? Media? Polls? Edit? Unsend? The system adapts its behavior accordingly. For example, replies on IRC are short and plain; replies on Telegram can include inline buttons and formatted HTML.

Streaming and Chunking

When the agent generates a long response, it needs to stream it back without spamming the chat:

Coalescing buffers streaming chunks: it waits until 1,500 characters have accumulated (or 1 second of idle time), then sends. This prevents the "message per sentence" effect.

Chunking splits long responses respecting channel limits. A 6,000-character response on Discord (2,000 char limit) gets split into 3 messages — but never in the middle of a code block. The system parses Markdown fences to find safe split points.

Draft streaming (Telegram-specific): instead of sending multiple messages, it sends one message and keeps editing it as more content streams in, creating a smooth typing effect.

Security: Pairing, Allowlists, Mention Gating

Three layers protect who can interact with your agent:

  1. DM Pairing: Unknown senders get a pairing code. An authorized user must approve them before the agent responds. This is on by default.

  2. Allowlists: Per-channel lists of approved senders. WhatsApp uses E.164 phone numbers, Discord uses user IDs.

  3. Mention Gating: In group chats, the agent only responds when mentioned (e.g., @agent). This prevents it from jumping into every conversation.


Part 10: Multi-Agent Routing

A single Gateway can run multiple agents, each with its own personality, workspace, and credentials.

Binding Resolution

When a message arrives, the system needs to decide which agent should handle it. Bindings are rules that match on channel, account, peer, guild, team, or roles:

The most specific match wins. Your boss could talk to "Work Agent" while your friends talk to "Casual Agent" — same Gateway, different personalities and tool access.

Each agent has its own:

  • Workspace directory (separate SOUL.md, MEMORY.md, etc.)
  • Session store (isolated conversation histories)
  • Auth profile (different API keys)

No credential sharing across agents.


Part 11: Security Architecture

The Trust Model

OpenClaw's security model is built around a single trusted operator assumption:

If someone has access to ~/.openclaw/ config files, they are a trusted operator. The Gateway treats authenticated callers as trusted.

This means OpenClaw is NOT designed for multi-tenant use — if you need separate users, run separate Gateways. This is an explicit, documented design decision.

Defense in Depth

Docker Sandbox Hardening

When sandbox mode is enabled, tool execution runs inside Docker containers with aggressive restrictions:

  • --cap-drop=ALL — removes all Linux capabilities
  • --security-opt no-new-privileges — prevents privilege escalation
  • Read-only root filesystem with tmpfs for /tmp
  • Default network mode: none (no internet access)
  • Workspace mounts validated against allowed roots

The sandbox prevents a hallucinated rm -rf ~ from doing real damage. The agent thinks it's running on a normal system, but destructive commands affect only the disposable container.

The Harness Concept

A broader concept has emerged around OpenClaw: the agent harness. This is the software infrastructure that wraps an AI model to manage its lifecycle, tools, memory, safety, and interactions with the world.

openclaw-harness (a Rust-based community tool) acts as a firewall for AI agent actions:

  • Intercepts every tool call before execution
  • Checks against 35 built-in safety rules
  • Blocks destructive commands, SSH key theft, API key exposure
  • Has a 6-layer self-protection system that prevents the AI from disabling the harness itself

The key insight: AI agents make 100+ tool calls per session. Manual "are you sure?" confirmations don't scale. You need automated guardrails.


Part 12: The Plugin System

Plugins extend OpenClaw without touching core code. They live in extensions/ and declare capabilities via a manifest file:

1{ 2 "id": "msteams", 3 "configSchema": { ... }, 4 "skills": ["skills/teams-skill"], 5 "tools": ["teams_send", "teams_read"] 6}

A plugin can provide:

  • New channels (MS Teams, Matrix, Zalo)
  • New tools (registered via the plugin SDK)
  • New skills (SKILL.md directories)
  • Lifecycle hooks (intercept gateway events)
  • HTTP handlers (custom API endpoints)
  • CLI commands (extend the command line)

Plugins go through a strict lifecycle:

Critical safety detail: Plugins are part of the Trusted Computing Base (TCB). An installed plugin runs with full gateway privileges. This is by design — but it means you should only install plugins you trust, just like you only install apps you trust.


Part 13: What Makes OpenClaw Architecturally Distinct

Let's crystallize the key differentiators in a comparison:

DimensionTraditional Frameworks
(LangChain, CrewAI, AutoGen)
OpenClaw
What it isDeveloper library (building blocks)Ready-to-use agent runtime (operating system)
ConfigurationCode (Python classes, pipelines)Markdown files (natural language)
MessagingNone built-in; you build integrations15+ platforms native
StateStateless or custom persistenceDurable JSONL sessions with lifecycle management
MemoryExternal vector DB (Pinecone, Chroma)Local Markdown files + hybrid SQLite search
AutonomyReactive only (waits for input)Proactive via heartbeat system
DeploymentLibrary in your appPersistent daemon on your machine
Extension modelCode packagesSkills (Markdown) + Plugins (code)
SecurityRoll your ownLayered: pairing, allowlists, tool policy, sandbox
Data locationUsually cloudAlways local (your machine, your files)

The Philosophical Bets

OpenClaw stakes out five positions about the future of personal AI:

  1. Markdown over code. Agent behavior should be described in prose, not programmed.
  2. Local over cloud. Your data stays on your machine. No vendor lock-in.
  3. Agent as OS. Not a tool you invoke, but a persistent layer of computing.
  4. Skills as apps. The ClawHub marketplace is the next app store.
  5. Bounded autonomy. Agents get clear limits, mandatory escalation paths, and comprehensive audit trails.

Part 14: The Honest Assessment

OpenClaw is impressive engineering, but it has real limitations worth understanding.

Setup friction is real. Despite the "10 minute" claim, you need Docker, API keys, channel auth tokens, and gateway configuration. A Hacker News thread found many users gave up during setup.

It's only as smart as the model. Ben Goertzel called it "amazing hands for a brain that doesn't yet exist." The orchestration infrastructure is sophisticated, but current LLMs still lack true abstraction, flexible long-term memory, and persistent episodic reasoning. OpenClaw can wire up tools beautifully — but the model driving those tools still hallucinates.

Security is hard. 41 security advisories. The ClawHavoc malware campaign. Hundreds of malicious skills on ClawHub. A viral incident where an agent deleted a Meta researcher's emails. Giving an AI agent access to your shell, files, and messages creates a large attack surface.

The harness paradox. As models get smarter, the scaffolding around them should get simpler. Manus refactored its harness five times in six months. LangChain re-architected Open Deep Research three times in a year. OpenClaw's Gateway is powerful — but much of it may become unnecessary as frontier models improve.


Conclusion: The Architecture in One Sentence

OpenClaw is a single-process TypeScript daemon that connects language models to messaging platforms and local tools via markdown-configured agents with durable session state, file-based memory, on-demand skills, proactive heartbeats, and layered security — creating a persistent, autonomous AI assistant that runs on your machine and acts through the apps you already use.

That's the whole thing. Everything else is implementation detail.