Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

14.3 Architecture: Gateway / Agent Loop / Skills

🦞 "Good architecture is the kind you can fork into six languages without breaking."


The Four-Layer Architecture

[Channel Adapters] ─► [Gateway] ─► [Agent Loop] ─► [Toolbox] ─► reply back
   (IncomingMessage)     (session)    (reasoning)     (execution)

Layer 1: Channel Adapters

Each channel implements a uniform interface:

interface ChannelAdapter {
  start(): Promise<void>;
  stop(): Promise<void>;
  onMessage(handler: (msg: IncomingMessage) => void): void;
  send(to: string, reply: OutgoingMessage): Promise<void>;
}

Adding a channel = writing one new file; the core doesn't change.

Layer 2: Gateway

Resolves sessions, pushes messages into history, triggers the Agent loop, and routes replies back through the original channel. It handles cross-channel identity, group-chat isolation, and interruption of long tasks.

Layer 3: Agent Loop

async function agentLoop(input) {
  let context = assembleContext(input.session, input.userMessage);
  for (let step = 0; step < MAX_STEPS; step++) {
    const parsed = parseLlmOutput(await llm.stream(context));
    if (parsed.kind === 'final_answer') return parsed.text;
    if (parsed.kind === 'tool_call') {
      const decision = await permissions.check(parsed.toolCall);
      if (!decision.allowed) { context.appendError(...); continue; }
      await hooks.run('PreToolUse', parsed.toolCall);
      const result = await toolbox.run(parsed.toolCall);
      context.appendToolResult(parsed.toolCall.id, result);
    }
  }
}

Key points: streaming inference, context compression, permission + Hooks checks, error-tolerant retry (errors are written back so the LLM decides next step).

Layer 4: Toolbox & Sandbox

Three sandbox tiers:

TierImplementationUse
Default (no sandbox)tools run in the main processpersonal
Restricted shellallow-list + arg validationmedium security
Dockerisolated containerproduction

Mapping to Chapter 8's Six Pillars

Harness pillarOpenClaw location
Agent loopsrc/agent/loop.ts
Toolssrc/agent/tools/
SkillsSkills Registry + ClawHub
Memorysrc/memory/ (SQLite + summary)
Sandboxdefault / restricted shell / Docker
Permissionspermissions/ + Hooks + plan mode

The overlap is not coincidence — the "six pillars" are the minimal engineering form of an open-source harness like OpenClaw.


Section Summary

TopicKey point
Architecture4 layers: Channel Adapters → Gateway → Agent Loop → Toolbox
Protocolall channels output IncomingMessage, input OutgoingMessage
Agent Loopstreaming + compression + permission + Hooks + error write-back
Sandbox3 tiers: none / restricted shell / Docker
Abstractionstable enough to fork into 6 languages

Next section: 14.4 Multi-Channel Routing