How openbmb/clawxrouter works
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: privacy.ts Privacy router · stats-dashboard.ts Stats dashboard · hooks.ts Hooks · router-pipeline.ts Router pipeline
Components 26
OpenClaw integration
Plugin entry and lifecycle hooks into the host agent framework.
- Plugin entry · OpenClaw plugin registration and startup boundary.
clawxrouter/index.ts - 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 · Composes routers, applies priority and weights, and short-circuits routing decisions.
clawxrouter/src/router-pipeline.ts - Privacy router · Classifies requests by sensitivity and chooses cloud, redact-and-forward, or local handling.
clawxrouter/src/routers/privacy.ts - Token saver · Uses a local judge to pick an appropriate cloud model only after privacy constraints pass.
clawxrouter/src/routers/token-saver.ts - Custom routers · 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 · Combines rule-based and semantic detection inputs for sensitivity classification.
clawxrouter/src/detector.ts - Rules · Keywords, regexes, tools, and path signals for fast privacy classification.
clawxrouter/src/rules.ts - Sync detect · Synchronous-facing wrapper around the detection worker.
clawxrouter/src/sync-detect.ts - Sync redact · Synchronous-facing wrapper around the desensitization worker.
clawxrouter/src/sync-desensitize.ts - Privacy proxy · Sanitizes S2 payloads before cloud forwarding and normalizes markers on return.
clawxrouter/src/privacy-proxy.ts - Guard agent · Keeps S3-classified requests fully on-device.
clawxrouter/src/guard-agent.ts
Local inference
Edge-model abstraction and worker-based semantic processing.
- Local model · Abstracts local inference backends used for semantic detection and judging.
clawxrouter/src/local-model.ts - Provider adapter · Supports OpenAI-compatible and Ollama-native local backend access.
clawxrouter/src/provider.ts - Detect worker · Runs semantic sensitivity detection off the main thread.
clawxrouter/src/llm-detect-worker.ts - Redact worker · Runs entity redaction and desensitization off the main thread.
clawxrouter/src/llm-desensitize-worker.ts - Worker loader · Custom module loading for worker execution.
clawxrouter/src/worker-loader.mjs
Control plane
Configuration, dashboard, and prompt-driven extensibility.
- Config · Defines routing, model, and control-plane configuration.
clawxrouter/src/config-schema.ts - Live config · Loads and updates configuration without restart.
clawxrouter/src/live-config.ts - Dashboard I/O · Reads and writes dashboard state and settings.
clawxrouter/src/dashboard-config-io.ts - Stats dashboard · Web control plane for routing rules, weights, prompts, and runtime stats.
clawxrouter/src/stats-dashboard.ts - Prompt loader · 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 · Coordinates session lifecycle and persistence boundaries.
clawxrouter/src/session-manager.ts - Session state · Maintains clean cloud-visible state alongside full local state.
clawxrouter/src/session-state.ts - Memory isolation · Separates cloud-safe memory from private local memory.
clawxrouter/src/memory-isolation.ts - Token stats · Tracks token usage and runtime cost signals.
clawxrouter/src/token-stats.ts
Connections 29
- 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.