# openbmb/clawxrouter architecture

> Architecture diagram of the GitHub repository openbmb/clawxrouter, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/openbmb/clawxrouter
- Repository: https://github.com/openbmb/clawxrouter

## Overview

1. **Project role and runtime boundary**
   ClawXRouter is a TypeScript plugin for the OpenClaw agent framework, not a standalone app. Its job is to intercept OpenClaw lifecycle events, classify each interaction, and choose whether the request goes to a cloud model, a local edge model, a redaction proxy, or a guard agent.

2. **Main integration point**
   The plugin entry is `clawxrouter/index.ts`, backed by `openclaw.plugin.json`. From there, `src/hooks.ts` wires ClawXRouter into OpenClaw’s hook system across the full agent lifecycle: model resolution, prompt building, tool calls, message persistence, session end, and sub-agent start. This hook layer is the architectural “spine” of the repo.

3. **Composable routing pipeline**
   `src/router-pipeline.ts` is the central decision engine. It combines multiple routers, applies weights, and supports short-circuiting. The repo’s core design is “security first”: privacy routing runs with higher priority, and cost-aware routing only optimizes requests that are already safe enough to leave the machine.

4. **Privacy-routing subsystem**
   The privacy path is implemented mainly in `src/routers/privacy.ts`, `src/detector.ts`, `src/rules.ts`, `src/local-model.ts`, `src/sync-detect.ts`, and `src/sync-desensitize.ts`. It classifies content into S1/S2/S3 using two detectors:
   - fast rule-based detection via keywords, regexes, tools, and paths
   - semantic detection via a local LLM worker
   The outputs drive three behaviors: passthrough to cloud, redact-and-forward, or fully local processing.

5. **Cost-aware routing subsystem**
   `src/routers/token-saver.ts` implements the “token saver” router. It uses a local judge model to classify task complexity into tiers such as SIMPLE, MEDIUM, COMPLEX, RESEARCH, and REASONING, then selects an appropriate cloud target model. This router is lower priority than privacy routing and is meant to reduce spend only after privacy constraints are satisfied.

6. **Local model and worker architecture**
   The expensive or latency-sensitive semantic work is pushed out of the main thread into workers: `src/llm-detect-worker.ts`, `src/llm-desensitize-worker.ts`, and the worker wrappers in `src/sync-detect.ts` and `src/sync-desensitize.ts`. `src/worker-loader.mjs` suggests custom worker module loading. This keeps detection/redaction from blocking the main plugin flow.

7. **Provider abstraction and edge backends**
   `src/provider.ts` and `src/local-model.ts` abstract edge inference backends. The README indicates support for OpenAI-compatible endpoints plus Ollama-native mode, which means the local decision engines can run against Ollama, vLLM, LM Studio, SGLang, LocalAI, or any compatible API. This is the main runtime boundary between the plugin and edge model infrastructure.

8. **Privacy proxy and data redaction flow**
   `src/privacy-proxy.ts` is the boundary for S2 data. The documented flow is: detect sensitive entities locally, replace them with redaction markers, send the sanitized request through a localhost proxy, then strip or normalize markers before the cloud call. This makes the cloud-visible payload intentionally incomplete while preserving task utility.

9. **Guard agent for fully local tasks**
   `src/guard-agent.ts` handles S3-classified content. Instead of forwarding to cloud services, the request is routed to a local guard agent, which keeps confidential data entirely on-device. In architecture terms, this is the hard security boundary: S3 content never enters the cloud path.

10. **Session and memory isolation**
   `src/session-manager.ts`, `src/session-state.ts`, and `src/memory-isolation.ts` implement dual-track persistence. The repo distinguishes between clean cloud-visible state and full local state, including separate memory files and session histories. The hooks layer enforces that the cloud side never sees full private transcripts.

11. **Configuration, live reload, and dashboard**
   Configuration is defined by `src/config-schema.ts`, loaded from `config.example.json`, and kept live by `src/live-config.ts`. `src/dashboard-config-io.ts` and `src/stats-dashboard.ts` back the web dashboard exposed under `/plugins/clawxrouter/stats`. The dashboard is not just observability; it is the primary control plane for editing routing rules, weights, prompts, and execution order without restart.

12. **Custom routers and prompt-driven extensibility**
   `src/routers/configurable.ts` supports user-defined routers loaded from config, while `src/prompt-loader.ts` reads prompt templates from `clawxrouter/prompts/`. This makes the routing system extensible at two levels: code-defined custom routers and editable markdown prompts for detection/judging behavior.

13. **Operational telemetry**
   `src/token-stats.ts` and related dashboard wiring indicate per-level token usage tracking and runtime statistics. This supports the cost-routing objective by making the routing decisions visible and measurable, rather than opaque.

14. **End-to-end data flow summary**
   A request enters through OpenClaw hooks, gets classified by the router pipeline, and then follows one of three paths:
   - **S1**: direct cloud forwarding
   - **S2**: redact locally, then forward through the privacy proxy
   - **S3**: stay local through the guard agent and isolated session/memory stores
   Cost-aware routing only affects the cloud path after privacy checks pass.

15. **What to draw in an architecture diagram**
   The key boxes are: OpenClaw hook entry, router pipeline, privacy detector stack, token-saver judge, privacy proxy, guard agent, dual-track session/memory storage, provider/model adapters, live config/dashboard, and worker threads. The main arrows should show that privacy routing has precedence, cost routing is conditional, and persistence is split into clean vs full local tracks.

Main entry points: [`clawxrouter/src/routers/privacy.ts`](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/routers/privacy.ts) (Privacy router), [`clawxrouter/src/stats-dashboard.ts`](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/stats-dashboard.ts) (Stats dashboard), [`clawxrouter/src/hooks.ts`](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/hooks.ts) (Hooks), [`clawxrouter/src/router-pipeline.ts`](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/router-pipeline.ts) (Router pipeline)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_integration["OpenClaw integration"]
  node_entry["Plugin entry<br/>[index.ts]"]
  node_hooks["Hooks<br/>[hooks.ts]"]
end

subgraph group_routing["Routing core"]
  node_pipeline["Router pipeline<br/>decision pipeline<br/>[router-pipeline.ts]"]
  node_privacy_router["Privacy router<br/>security router<br/>[privacy.ts]"]
  node_token_saver["Token saver<br/>cost router<br/>[token-saver.ts]"]
  node_configurable_router["Custom routers<br/>extensible router<br/>[configurable.ts]"]
end

subgraph group_privacy["Privacy stack"]
  node_detector["Detector<br/>[detector.ts]"]
  node_rules["Rules<br/>[rules.ts]"]
  node_sync_detect["Sync detect<br/>worker wrapper<br/>[sync-detect.ts]"]
  node_sync_desensitize["Sync redact<br/>worker wrapper"]
  node_privacy_proxy["Privacy proxy<br/>redaction proxy<br/>[privacy-proxy.ts]"]
  node_guard_agent["Guard agent<br/>local guard<br/>[guard-agent.ts]"]
end

subgraph group_local["Local inference"]
  node_local_model["Local model<br/>edge model adapter<br/>[local-model.ts]"]
  node_provider["Provider adapter<br/>backend adapter<br/>[provider.ts]"]
  node_detect_worker(("Detect worker"))
  node_desensitize_worker(("Redact worker"))
  node_worker_loader["Worker loader<br/>[worker-loader.mjs]"]
end

subgraph group_control["Control plane"]
  node_config["Config<br/>schema<br/>[config-schema.ts]"]
  node_live_config["Live config<br/>runtime config<br/>[live-config.ts]"]
  node_dashboard_io["Dashboard I/O<br/>dashboard backend"]
  node_dashboard["Stats dashboard<br/>control dashboard<br/>[stats-dashboard.ts]"]
  node_prompt_loader["Prompt loader<br/>prompt templates<br/>[prompt-loader.ts]"]
end

subgraph group_state["State and telemetry"]
  node_session_manager["Session manager<br/>session state<br/>[session-manager.ts]"]
  node_session_state[("Session state<br/>dual-track state<br/>[session-state.ts]")]
  node_memory_isolation[("Memory isolation<br/>state isolation")]
  node_token_stats["Token stats<br/>telemetry<br/>[token-stats.ts]"]
end

node_entry -->|"registers"| node_hooks
node_hooks -->|"routes through"| node_pipeline
node_pipeline -->|"first priority"| node_privacy_router
node_pipeline -->|"conditional"| node_token_saver
node_pipeline -.->|"extends with"| node_configurable_router
node_privacy_router -->|"classifies with"| node_detector
node_detector -->|"fast path"| node_rules
node_detector -->|"semantic path"| node_sync_detect
node_sync_detect -->|"executes in"| node_detect_worker
node_privacy_router -->|"redacts with"| node_sync_desensitize
node_sync_desensitize -->|"executes in"| node_desensitize_worker
node_sync_detect -->|"uses"| node_local_model
node_sync_desensitize -->|"uses"| node_local_model
node_local_model -->|"backed by"| node_provider
node_worker_loader -.->|"loads"| node_detect_worker
node_worker_loader -.->|"loads"| node_desensitize_worker
node_privacy_router -->|"S2 path"| node_privacy_proxy
node_privacy_router -->|"S3 path"| node_guard_agent
node_token_saver -->|"judges with"| node_local_model
node_hooks -->|"persists via"| node_session_manager
node_session_manager -->|"tracks"| node_session_state
node_session_state -->|"separates"| node_memory_isolation
node_hooks -->|"observes"| node_dashboard
node_dashboard -->|"reads/writes"| node_dashboard_io
node_dashboard -->|"controls"| node_live_config
node_live_config -->|"validates against"| node_config
node_dashboard -->|"edits prompts"| node_prompt_loader
node_dashboard -->|"shows"| node_token_stats
node_token_saver -->|"measures"| node_token_stats

click node_entry "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/index.ts"
click node_hooks "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/hooks.ts"
click node_pipeline "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/router-pipeline.ts"
click node_privacy_router "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/routers/privacy.ts"
click node_token_saver "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/routers/token-saver.ts"
click node_configurable_router "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/routers/configurable.ts"
click node_detector "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/detector.ts"
click node_rules "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/rules.ts"
click node_sync_detect "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/sync-detect.ts"
click node_sync_desensitize "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/sync-desensitize.ts"
click node_privacy_proxy "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/privacy-proxy.ts"
click node_guard_agent "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/guard-agent.ts"
click node_local_model "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/local-model.ts"
click node_provider "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/provider.ts"
click node_detect_worker "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/llm-detect-worker.ts"
click node_desensitize_worker "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/llm-desensitize-worker.ts"
click node_config "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/config-schema.ts"
click node_live_config "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/live-config.ts"
click node_dashboard_io "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/dashboard-config-io.ts"
click node_dashboard "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/stats-dashboard.ts"
click node_prompt_loader "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/prompt-loader.ts"
click node_session_manager "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/session-manager.ts"
click node_session_state "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/session-state.ts"
click node_memory_isolation "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/memory-isolation.ts"
click node_token_stats "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/token-stats.ts"
click node_worker_loader "https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/worker-loader.mjs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_entry,node_hooks toneBlue
class node_pipeline,node_privacy_router,node_token_saver,node_configurable_router toneAmber
class node_detector,node_rules,node_sync_detect,node_sync_desensitize,node_privacy_proxy,node_guard_agent toneMint
class node_local_model,node_provider,node_detect_worker,node_desensitize_worker,node_worker_loader toneRose
class node_config,node_live_config,node_dashboard_io,node_dashboard,node_prompt_loader toneIndigo
class node_session_manager,node_session_state,node_memory_isolation,node_token_stats toneTeal
```

## Components

### OpenClaw integration

Plugin entry and lifecycle hooks into the host agent framework.

- [Plugin entry](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/index.ts): plugin entry · OpenClaw plugin registration and startup boundary. · `clawxrouter/index.ts`
- [Hooks](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/hooks.ts): lifecycle hooks · Wires routing into model resolution, prompts, tool calls, persistence, and session events. · `clawxrouter/src/hooks.ts`

### Routing core

Decision pipeline and router implementations for privacy and cost.

- [Router pipeline](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/router-pipeline.ts): decision pipeline · Composes routers, applies priority and weights, and short-circuits routing decisions. · `clawxrouter/src/router-pipeline.ts`
- [Privacy router](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/routers/privacy.ts): security router · Classifies requests by sensitivity and chooses cloud, redact-and-forward, or local handling. · `clawxrouter/src/routers/privacy.ts`
- [Token saver](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/routers/token-saver.ts): cost router · Uses a local judge to pick an appropriate cloud model only after privacy constraints pass. · `clawxrouter/src/routers/token-saver.ts`
- [Custom routers](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/routers/configurable.ts): extensible router · Loads user-defined routing behavior from configuration. · `clawxrouter/src/routers/configurable.ts`

### Privacy stack

Detection, redaction, proxying, and guard-agent handling for sensitive content.

- [Detector](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/detector.ts): sensitivity detector · Combines rule-based and semantic detection inputs for sensitivity classification. · `clawxrouter/src/detector.ts`
- [Rules](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/rules.ts): pattern rules · Keywords, regexes, tools, and path signals for fast privacy classification. · `clawxrouter/src/rules.ts`
- [Sync detect](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/sync-detect.ts): worker wrapper · Synchronous-facing wrapper around the detection worker. · `clawxrouter/src/sync-detect.ts`
- [Sync redact](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/sync-desensitize.ts): worker wrapper · Synchronous-facing wrapper around the desensitization worker. · `clawxrouter/src/sync-desensitize.ts`
- [Privacy proxy](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/privacy-proxy.ts): redaction proxy · Sanitizes S2 payloads before cloud forwarding and normalizes markers on return. · `clawxrouter/src/privacy-proxy.ts`
- [Guard agent](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/guard-agent.ts): local guard · Keeps S3-classified requests fully on-device. · `clawxrouter/src/guard-agent.ts`

### Local inference

Edge-model abstraction and worker-based semantic processing.

- [Local model](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/local-model.ts): edge model adapter · Abstracts local inference backends used for semantic detection and judging. · `clawxrouter/src/local-model.ts`
- [Provider adapter](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/provider.ts): backend adapter · Supports OpenAI-compatible and Ollama-native local backend access. · `clawxrouter/src/provider.ts`
- [Detect worker](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/llm-detect-worker.ts): worker · Runs semantic sensitivity detection off the main thread. · `clawxrouter/src/llm-detect-worker.ts`
- [Redact worker](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/llm-desensitize-worker.ts): worker · Runs entity redaction and desensitization off the main thread. · `clawxrouter/src/llm-desensitize-worker.ts`
- [Worker loader](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/worker-loader.mjs): loader · Custom module loading for worker execution. · `clawxrouter/src/worker-loader.mjs`

### Control plane

Configuration, dashboard, and prompt-driven extensibility.

- [Config](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/config-schema.ts): schema · Defines routing, model, and control-plane configuration. · `clawxrouter/src/config-schema.ts`
- [Live config](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/live-config.ts): runtime config · Loads and updates configuration without restart. · `clawxrouter/src/live-config.ts`
- [Dashboard I/O](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/dashboard-config-io.ts): dashboard backend · Reads and writes dashboard state and settings. · `clawxrouter/src/dashboard-config-io.ts`
- [Stats dashboard](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/stats-dashboard.ts): control dashboard · Web control plane for routing rules, weights, prompts, and runtime stats. · `clawxrouter/src/stats-dashboard.ts`
- [Prompt loader](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/prompt-loader.ts): prompt templates · Loads editable prompt templates for detection and judging behavior. · `clawxrouter/src/prompt-loader.ts`

### State and telemetry

Session isolation, persistence, and usage tracking.

- [Session manager](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/session-manager.ts): session state · Coordinates session lifecycle and persistence boundaries. · `clawxrouter/src/session-manager.ts`
- [Session state](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/session-state.ts): dual-track state · Maintains clean cloud-visible state alongside full local state. · `clawxrouter/src/session-state.ts`
- [Memory isolation](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/memory-isolation.ts): state isolation · Separates cloud-safe memory from private local memory. · `clawxrouter/src/memory-isolation.ts`
- [Token stats](https://github.com/openbmb/clawxrouter/blob/main/clawxrouter/src/token-stats.ts): telemetry · Tracks token usage and runtime cost signals. · `clawxrouter/src/token-stats.ts`

## Connections

- Plugin entry → Hooks: registers · Plugin entry hands control to the OpenClaw hook layer.
- Hooks → Router pipeline: routes through · Lifecycle events are classified and dispatched by the router pipeline.
- Router pipeline → Privacy router: first priority · Privacy routing runs before cost optimization.
- Router pipeline → Token saver: conditional · Cost-aware selection applies only after content is safe enough to leave the machine.
- Router pipeline → Custom routers: extends with · Custom routers can be injected from configuration.
- Privacy router → Detector: classifies with · Sensitivity decisions use fast and semantic detection.
- Detector → Rules: fast path · Rule signals provide immediate classification.
- Detector → Sync detect: semantic path · Semantic detection is offloaded to the worker wrapper.
- Sync detect → Detect worker: executes in · Detection runs in a separate worker thread.
- Privacy router → Sync redact: redacts with · Sensitive entities are desensitized before forwarding.
- Sync redact → Redact worker: executes in · Redaction runs in a separate worker thread.
- Sync detect → Local model: uses · Semantic detection relies on local inference.
- Sync redact → Local model: uses · Redaction and judging can reuse local inference backends.
- Local model → Provider adapter: backed by · Local inference is abstracted over edge model providers.
- Worker loader → Detect worker: loads · Custom worker loading supports the detection worker.
- Worker loader → Redact worker: loads · Custom worker loading supports the redaction worker.
- Privacy router → Privacy proxy: S2 path · Redacted requests are forwarded through the privacy proxy.
- Privacy router → Guard agent: S3 path · Highly sensitive requests stay local via the guard agent.
- Token saver → Local model: judges with · Task complexity is classified by a local judge model.
- Hooks → Session manager: persists via · Hook events update session lifecycle and storage.
- Session manager → Session state: tracks · Session manager coordinates clean and full state tracks.
- Session state → Memory isolation: separates · Memory isolation keeps cloud-visible and private memory apart.
- Hooks → Stats dashboard: observes · Runtime state is exposed through the dashboard control plane.
- Stats dashboard → Dashboard I/O: reads/writes · Dashboard state is mediated by a dedicated I/O layer.
- Stats dashboard → Live config: controls · Dashboard changes can update live configuration without restart.
- Live config → Config: validates against · Live updates are checked against the config schema.
- Stats dashboard → Prompt loader: edits prompts · Prompt templates are part of the control plane.
- Stats dashboard → Token stats: shows · Token usage and runtime telemetry feed the dashboard.
- Token saver → Token stats: measures · Cost-routing decisions are informed by token usage.
