Openlegion

作者 @openlegion-ai

OpenLegion securely runs autonomous AI agent fleets in isolated Docker containers with built-in cost controls—ideal for production as a secure OpenClaw alter...

README

openlegion-logo-new

The AI agent framework built for builders who can't afford a security incident.

License: BSL 1.1 Python 3.10+ Tests: 2240 Discord Twitter LiteLLM Docker

Autonomous AI agent fleets — isolated, auditable, and production-ready. Every agent runs in its own Docker container. API keys never leave the vault. Chat via Telegram, Discord, Slack, or WhatsApp. Built-in cost controls. 100+ LLM providers.

Quick Start · Full Setup Guide · Why Not OpenClaw? · Docs


Demo

https://github.com/user-attachments/assets/8bd3fe95-5734-474d-92f0-40616daf91ad

openlegion start → inline setup → multiple agents running. Live cost tracking. No configuration files edited by hand. Connect Telegram, WhatsApp, Slack, and Discord.

Table of Contents


Quick Start

Requirements: Python 3.10+, Docker (running), an LLM API key (Anthropic / Moonshot / OpenAI)

macOS / Linux:

git clone https://github.com/openlegion-ai/openlegion.git && cd openlegion
./install.sh                     # checks deps, creates venv, makes CLI global
openlegion start                 # inline setup on first run, then launch agents

Windows (PowerShell):

git clone https://github.com/openlegion-ai/openlegion.git
cd openlegion
powershell -ExecutionPolicy Bypass -File install.ps1
openlegion start

First install downloads ~70 packages and takes 2-3 minutes. Subsequent installs are fast.

Need help? See the full setup guide for platform-specific instructions and troubleshooting.

# Add more agents from the REPL
/add

# Run in background
openlegion start -d
openlegion chat researcher   # connect from another terminal
openlegion stop              # clean shutdown

Why Not OpenClaw?

OpenClaw is the most popular personal AI assistant framework — 200K+ GitHub stars, brilliant for single-user use. For production workloads and team deployments, it has documented problems:

  • 42,000+ exposed instances with no authentication (Bitsight, Feb 2026)
  • 341 malicious skills found stealing user data (Koi Security / The Hacker News)
  • CVE-2026-25253: one-click remote code execution
  • No per-agent cost controls — runaway spend is a real risk
  • No deterministic routing — a CEO agent (LLM) decides what runs next
  • API keys stored directly in agent config

OpenLegion was designed from day one assuming agents will be compromised.

OpenClawOpenLegion
API key storageAgent config filesVault proxy — agents never see keys
Agent isolationProcess-levelDocker container per agent + microVM option
Cost controlsNonePer-agent daily + monthly budget caps
Multi-agent routingLLM CEO agentFleet model — blackboard + pub/sub coordination
LLM providersBroad100+ via LiteLLM with health-tracked failover
Test coverageMinimal2240 tests including full Docker E2E
Codebase size430,000+ lines~32,000 lines — auditable in a day

What It Does

OpenLegion is an autonomous AI agent framework for running multi-agent fleets in isolated Docker containers. Each agent gets its own memory, tools, schedule, and budget — coordinated through blackboard shared state and pub/sub events with no LLM routing layer.

Chat with your agent fleet via Telegram, Discord, Slack, WhatsApp, or CLI. Agents act autonomously via cron schedules, webhooks, heartbeat monitoring, and file watchers — without being prompted.

2240 tests passing across ~32,000 lines of application code. Fully auditable in a day. No LangChain. No Redis. No Kubernetes. No CEO agent. BSL License.

  1. Security by architecture — every agent runs in an isolated Docker container (microVM when available). API keys live in the credential vault — agents call through a proxy and never handle credentials directly. Defense-in-depth with 6 security layers.

  2. Production-grade cost control — per-agent LLM token tracking with enforced daily and monthly budget caps at the vault layer. Agents physically cannot spend what you haven't authorized. View live spend with /costs in the REPL.

  3. Acts autonomously — cron schedules, heartbeat probes, webhook triggers, and file watchers let agents work without being prompted.

  4. Self-aware and self-improving — agents understand their own permissions, budget, fleet topology, and system architecture via auto-generated SYSTEM.md and live runtime context. They learn from tool failures and user corrections, injecting past learnings into future sessions.

  5. Self-extends — agents write their own Python skills at runtime and hot-reload them. Agents can also spawn sub-agents for specialized work.

  6. Multi-channel — connect agents to Telegram, Discord, Slack, and WhatsApp. Also accessible via CLI and API.

  7. Real-time dashboard — web-based fleet observability with consolidated navigation, slide-over chat panels, keyboard command palette, grouped request traces, live event streaming, streaming broadcast with real-time per-agent responses, LLM prompt/response previews, agent management, agent settings editor (personality, instructions, preferences, heartbeat rules, memory, activity logs, learnings), cost charts, cron management, and embedded KasmVNC viewer for persistent browser agents.

  8. Tracks and caps spend — per-agent LLM cost tracking with daily and monthly budget enforcement.

  9. Fails over across providers — configurable model failover chains cascade across LLM providers with per-model health tracking and exponential cooldown.

  10. Token-level streaming — real-time token-by-token LLM responses across CLI, dashboard, Telegram, Discord, and Slack with progressive message editing and graceful non-streaming fallback.


Architecture

OpenLegion's architecture separates concerns across three trust zones: untrusted external input, sandboxed agent containers, and a trusted mesh host that holds credentials and coordinates the fleet. All inter-agent communication flows through the mesh — no agent has direct network access or peer-to-peer connections.

┌──────────────────────────────────────────────────────────────────────────┐
│                           User Interface                                │
│                                                                         │
│   CLI (click)          Webhooks            Cron Scheduler               │
│   - setup              - POST /webhook/    - "0 9 * * 1-5"             │
│   - start (REPL)         hook/{id}         - "every 30m"               │
│   - stop / status      - Trigger agents    - Heartbeat pattern          │
│   - chat / status                                                       │
└──────────────┬──────────────────┬──────────────────┬────────────────────┘
               │                  │                  │
               ▼                  ▼                  ▼
┌──────────────────────────────────────────────────────────────────────────┐
│                         Mesh Host (FastAPI)                              │
│                         Port 8420 (default)                              │
│                                                                         │
│  ┌────────────┐ ┌─────────┐ ┌───────────┐ ┌────────────────────────┐   │
│  │ Blackboard │ │ PubSub  │ │  Message   │ │   Credential Vault     │   │
│  │ (SQLite)   │ │         │ │  Router    │ │   (API Proxy)          │   │
│  │            │ │ Topics, │ │            │ │                        │   │
│  │ Key-value, │ │ subs,   │ │ Permission │ │ LLM, Anthropic,       │   │
│  │ versioned, │ │ notify  │ │ enforced   │ │ OpenAI, Apollo,        │   │
│  │ TTL, GC    │ │         │ │ routing    │ │ Hunter, Brave Search   │   │
│  └────────────┘ └─────────┘ └───────────┘ └────────────────────────┘   │
│                                                                         │
│  ┌──────────────┐ ┌──────────────┐ ┌──────────────┐                    │
│  │  Permission  │ │  Container   │ │    Cost      │                    │
│  │  Matrix      │ │  Manager     │ │   Tracker    │                    │
│  │              │ │              │ │              │                    │
│  │ Per-agent    │ │ Docker life- │ │ Per-agent    │                    │
│  │ ACLs, globs, │ │ cycle, nets, │ │ token/cost,  │                    │
│  │ default deny │ │ volumes      │ │ budgets      │                    │
│  └──────────────┘ └──────────────┘ └──────────────┘                    │
└──────────────────────────────────────────────────────────────────────────┘
               │
               │  Docker Network (bridge / host)
               │
     ┌─────────┼──────────┬──────────────────────┐
     ▼         ▼          ▼                      ▼
┌─────────┐ ┌─────────┐ ┌─────────┐       ┌─────────┐
│ Agent A │ │ Agent B │ │ Agent C │  ...  │ Agent N │
│ :8401   │ │ :8402   │ │ :8403   │       │ :840N   │
└─────────┘ └─────────┘ └─────────┘       └─────────┘
  Each agent: isolated Docker container, own /data volume,
  own memory DB, own workspace, 384MB RAM, 0.15 CPU

Trust Zones

LevelZoneDescription
0UntrustedExternal input (webhooks, user prompts). Sanitized before reaching agents.
1SandboxedAgent containers. Isolated filesystem, no external network, no credentials.
2TrustedMesh host. Holds credentials, manages containers, routes messages.

Mesh Host

The mesh host is the central coordination layer. It runs on the host machine as a single FastAPI process.

Blackboard (Shared State Store)

SQLite-backed key-value store with versioning, TTL, and garbage collection. Project agents' blackboard access is automatically scoped to projects/{name}/* — agents use natural keys (e.g. tasks/research_abc123) while the MeshClient transparently namespaces them under the project. Standalone agents have no blackboard access.

NamespacePurposeExample
tasks/*Task assignmentstasks/research_abc123
context/*Shared agent contextcontext/prospect_acme
signals/*Inter-agent signalssignals/research_complete
history/*Append-only audit loghistory/action_xyz

Credential Vault (API Proxy)

Agents never hold API keys. All external API calls route through the mesh. The vault uses a two-tier prefix system: OPENLEGION_SYSTEM_* for LLM provider keys (never agent-accessible) and OPENLEGION_CRED_* for agent-tier tool/service keys. Budget limits are enforced before dispatching LLM calls and token usage is recorded after each response.

Model Failover

Configurable failover chains cascade across LLM providers transparently. ModelHealthTracker applies exponential cooldown per model (transient errors: 60s → 300s → 1500s, billing/auth errors: 1h). Streaming failover is supported — if a connection fails mid-stream, the next model in the chain picks up.

Permission Matrix

Every inter-agent operation is checked against per-agent ACLs:

{
  "researcher": {
    "can_message": ["*"],
    "can_publish": ["research_complete"],
    "can_subscribe": ["new_lead"],
    "blackboard_read": ["projects/myproject/*"],
    "blackboard_write": ["projects/myproject/*"],
    "allowed_apis": ["llm", "brave_search"],
    "allowed_credentials": ["brightdata_*"]
  }
}

Blackboard patterns use the projects/{name}/* namespace. When an agent joins a project, it receives read/write access to that namespace. Standalone agents get empty blackboard permissions.

Container Manager

Agent containers are slim — no browser. Browsing is handled by a shared browser service container (Camoufox + KasmVNC).

Agent container:

  • Image: openlegion-agent:latest (Python 3.12, system tools — no browser)
  • Network: Bridge with port mapping (macOS/Windows) or host network (Linux)
  • Volume: openlegion_data_{agent_id} mounted at /data (agent names with spaces/special chars are sanitized)
  • Resources: 384MB RAM, 0.15 CPU (agents are I/O-bound — waiting on LLM APIs)
  • Security: no-new-privileges, runs as non-root agent user (UID 1000)
  • Port: 8400 (FastAPI)

Browser service container (shared across all agents):

  • Image: openlegion-browser:latest (Camoufox stealth browser + KasmVNC)
  • Resources: 2–8GB RAM (scaled by fleet size), 1 CPU, 512MB shared memory
  • Ports: 8500 (browser API), 6080 (KasmVNC web client)
  • Capacity: 1–10 concurrent browser sessions (scaled by fleet size)

Agent Architecture

Each agent container runs a FastAPI server with endpoints for task assignment, chat, status, capabilities, and results.

┌─────────────────────────────────────────────────────────────┐
│                    Agent Container                           │
│                                                              │
│  FastAPI Server (:8400)                                      │
│    POST /task    POST /chat    POST /chat/reset               │
│    GET /status   GET /result   GET /capabilities              │
│    GET /workspace  GET|PUT /workspace/{file}                  │
│    GET /heartbeat-context                                     │
│                                                              │
│  ┌──────────────────────────────────────────────────────┐    │
│  │                     AgentLoop                         │    │
│  │                                                       │    │
│  │  Task Mode: bounded 20-iteration loop                 │    │
│  │  Chat Mode: conversational with tool use              │    │
│  │                                                       │    │
│  │  Both: LLM call → tool execution → context mgmt      │    │
│  └──┬──────────┬──────────┬──────────┬──────────┬───────┘    │
│     │          │          │          │          │             │
│  ┌──▼───┐  ┌──▼───┐  ┌──▼──────┐ ┌─▼──────┐ ┌─▼─────────┐ │
│  │ LLM  │  │ Mesh │  │ Skill   │ │Work-   │ │ Context   │ │
│  │Client│  │Client│  │Registry │ │space   │ │ Manager   │ │
│  │(mesh │  │(HTTP)│  │(builtins│ │Manager │ │(token     │ │
│  │proxy)│  │      │  │+custom) │ │(/data/ │ │tracking,  │ │
│  └──────┘  └──────┘  └─────────┘ │workspace│ │compact)   │ │
│                                   └─────────┘ └───────────┘ │
└─────────────────────────────────────────────────────────────┘

Task Mode (execute_task)

Accepts a TaskAssignment for task execution. Runs a bounded loop (max 20 iterations) of decide → act → learn. Returns a TaskResult with structured output and optional blackboard promotions.

Chat Mode (chat)

Accepts a user message. On the first message, loads workspace context (PROJECT.md if in a project, INSTRUCTIONS.md, SOUL.md, USER.md, MEMORY.md, SYSTEM.md) into the system prompt, injects a live Runtime Context block (permissions, budget, fleet, cron), and searches memory for relevant facts. Executes tool calls in a bounded loop (max 30 rounds per turn, auto-compaction every 200 rounds with seamless continuation) and runs context compaction when needed.

Tool Loop Detection

Both modes include automatic detection of stuck tool-call loops. A sliding window tracks recent (tool_name, params_hash, result_hash) tuples and escalates through three levels:

LevelTriggerAction
Warn2nd identical callSystem message: "Try a different approach"
Block4th identical callTool skipped, error returned to agent
Terminate9th call with same paramsLoop terminated with failure status

memory_search is exempt since repeated searches are legitimate. Detection uses SHA-256 hashes of canonicalized parameters and results over a 15-call sliding window.

Built-in Tools

ToolPurpose
run_commandShell command execution with timeout
read_fileRead file contents from /data
write_fileWrite/append file in /data
list_filesList/glob files in /data
http_requestHTTP GET/POST/PUT/DELETE/PATCH
browser_navigateOpen URL, extract page text via shared browser service
browser_get_elementsAccessibility tree snapshot with element refs (e1, e2, ...)
browser_screenshotCapture page screenshot
browser_clickClick element by ref or CSS selector
browser_typeFill input by ref or CSS selector (supports $CRED{} handles)
browser_hoverHover over element to trigger dropdowns/tooltips
browser_scrollScroll page up/down or scroll element into view
browser_wait_forWait for CSS selector to appear/disappear
browser_press_keyPress keyboard key or shortcut (Escape, Enter, Control+a)
browser_go_backNavigate back in browser history
browser_go_forwardNavigate forward in browser history
browser_switch_tabList open tabs or switch to a specific tab
browser_resetReset browser session (profile preserved)
browser_solve_captchaManual CAPTCHA detection and solving
memory_searchHybrid search across workspace files and structured DB
memory_saveSave fact to workspace and structured memory DB
web_searchSearch the web via DuckDuckGo (no API key)
notify_userSend notification to user across all connected channels
list_agentsDiscover agents in your project (standalone agents see only themselves)
read_shared_stateRead from the shared blackboard
write_shared_stateWrite to the shared blackboard
list_shared_stateBrowse blackboard entries by prefix
publish_eventPublish event to mesh pub/sub
subscribe_eventSubscribe to a pub/sub topic at runtime
watch_blackboardWatch blackboard keys matching a glob pattern
claim_taskAtomically claim a task from the shared blackboard
save_artifactSave deliverable file and register on blackboard
update_workspaceUpdate identity files (SOUL.md, INSTRUCTIONS.md, USER.md, HEARTBEAT.md)
set_cronSchedule a recurring job (set heartbeat=true for autonomous wakeups)
list_cron / remove_cronManage scheduled jobs
create_skillWrite a new Python skill at runtime
reload_skillsHot-reload all skills
spawn_fleet_agentSpawn an ephemeral sub-agent in a new container
spawn_subagentSpawn a lightweight in-container subagent for parallel subtasks
list_subagentsList active subagents and their status
wait_for_subagentWait for a subagent to complete and return its result
vault_generate_secretGenerate and store a random secret (returns opaque handle)
vault_listList credential names (names only, never values)
get_system_statusQuery own runtime state: permissions, budget, fleet, cron, health
read_agent_historyRead another agent's conversation logs

Custom skills are Python functions decorated with @skill, auto-discovered from the agent's skills_dir at startup. Agents can also create new skills at runtime and hot-reload them.

Agents also support MCP (Model Context Protocol) — any MCP-compatible tool server can be plugged in via config, giving agents access to databases, filesystems, APIs, and more without writing custom skills.


Memory System

Five layers give agents persistent, self-improving memory:

Layer 5: Context Manager          ← Manages the LLM's context window
  │  Monitors token usage
  │  Proactive flush at 60% capacity
  │  Auto-compacts at 70% capacity
  │  Extracts facts before discarding messages
  │
Layer 4: Learnings                ← Self-improvement through failure tracking
  │  learnings/errors.md         (tool failures with context)
  │  learnings/corrections.md   (user corrections and preferences)
  │  Auto-injected into system prompt each session
  │
Layer 3: Workspace Files          ← Durable, human-readable storage
  │  INSTRUCTIONS.md, SOUL.md, USER.md  (loaded into system prompt)
  │  SYSTEM.md                    (auto-generated architecture guide + runtime snapshot)
  │  MEMORY.md                    (curated long-term facts)
  │  HEARTBEAT.md                 (autonomous monitoring rules)
  │  memory/YYYY-MM-DD.md         (daily session logs)
  │  BM25 search across all files
  │
Layer 2: Structured Memory DB     ← Hierarchical vector database
  │  SQLite + sqlite-vec + FTS5
  │  Facts with embeddings (KNN similarity search)
  │  Auto-categorization with category-scoped search
  │  3-tier retrieval: categories → scoped facts → flat fallback
  │  Reinforcement scoring with access-count boost + recency decay
  │
Layer 1: Salience Tracking        ← Prioritizes important facts
     Access count, decay score, last accessed timestamp
     High-salience facts auto-surface in initial context

Write-Then-Compact Pattern

Before the context manager discards messages, it:

  1. Asks the LLM to extract important facts from the conversation
  2. Stores facts in both MEMORY.md and the structured memory DB
  3. Summarizes the conversation
  4. Replaces message history with: summary + last 4 messages

Nothing is permanently lost during compaction.

Cross-Session Memory

Facts saved with memory_save are stored in both the workspace (daily log) and the structured SQLite database. After a reset or restart, memory_search retrieves them via hybrid search:

Session 1: User says "My cat's name is Whiskerino"
           Agent saves to daily log + structured DB

  ═══ Chat Reset ═══

Session 2: User asks "What is my cat's name?"
           Agent recalls "Whiskerino" via memory_search

Triggering & Automation

Agents act autonomously through trigger mechanisms running in the mesh host (not inside containers, so they survive container restarts).

Cron Scheduler

Persistent cron jobs that dispatch messages to agents on a schedule. Agents can schedule their own jobs using the set_cron tool.

Supports 5-field cron expressions (minute hour dom month dow), interval shorthand (every 30m, every 2h), and state persisted to config/cron.json.

Heartbeat System

Cost-efficient autonomous monitoring. Heartbeat jobs run cheap deterministic probes first — disk usage, pending signals, pending tasks — and only dispatch to the agent (costing LLM tokens) when probes detect something actionable.

When a heartbeat fires, the agent receives enriched context: its HEARTBEAT.md rules, recent daily logs, probe alerts, and actual pending signal/task content — all in a single message. If HEARTBEAT.md is the default scaffold, no recent activity exists, and no probes triggered, the dispatch is skipped entirely (zero LLM cost).

This 5-stage architecture (scheduler → probes → context → policy → action) makes always-on agents economically viable.

Webhook Endpoints

Named webhook URLs that dispatch payloads to agents:

curl -X POST http://localhost:8420/webhook/hook/hook_a1b2c3d4 \
  -H "Content-Type: application/json" \
  -d '{"event": "push", "repo": "myproject"}'

File Watchers

Poll directories for new/modified files matching glob patterns. Uses polling (not inotify) for Docker volume compatibility.

File watchers are configured programmatically via the FileWatcher.watch() method, specifying a directory path, glob pattern, target agent, and message template (supports {filepath} and {filename} placeholders).


Cost Tracking & Budgets

Every LLM call is tracked at the Credential Vault layer. Per-agent budgets prevent runaway spend. View costs from the interactive REPL (/costs) or configure budgets in config/agents.yaml:

agents:
  researcher:
    budget:
      daily_usd: 5.00
      monthly_usd: 100.00

When an agent exceeds its budget, the vault rejects LLM calls with an error instead of forwarding them to the provider.


Security Model

Defense-in-depth with six layers:

LayerMechanismWhat It Prevents
Runtime isolationDocker Sandbox microVMs when available; falls back to Docker containersAgent escape, kernel exploits
Container hardeningNon-root user, no-new-privileges, memory/CPU limitsPrivilege escalation, resource abuse
Credential separationVault holds keys, agents call via proxyKey leakage, unauthorized API use
Permission enforcementPer-agent ACLs for messaging, blackboard, pub/sub, APIsUnauthorized data access
Input validationPath traversal prevention, SSRF blocking, safe condition eval (no eval()), token budgets, iteration limits, rate limitingInjection, runaway loops, network abuse
Unicode sanitizationInvisible character stripping at five choke points (user input, tool results, workspace, mesh tools, dashboard)Prompt injection via hidden Unicode

Dual Runtime Backend

OpenLegion supports two isolation levels:

Docker Containers (default)Docker Sandbox microVMs
IsolationShared kernel, namespace separationOwn kernel per agent (hypervisor)
Escape riskKernel exploit could escapeHypervisor boundary — much harder
PerformanceNative speedNear-native (Rosetta 2 on Apple Silicon)
RequirementsAny Docker installDocker Desktop 4.58+
Enableopenlegion startopenlegion start --sandbox

Docker containers (default) run agents as non-root with no-new-privileges, 384MB memory limit, 0.15 CPU cap, and no host filesystem access. Browser operations are handled by a shared browser service container (2–8GB RAM scaled by fleet size, 1 CPU). This is secure for most use cases.

Docker Sandbox microVMs give each agent its own Linux kernel via Apple Virtualization.framework (macOS) or Hyper-V (Windows). Even if an agent achieves code execution, it's trapped inside a lightweight VM with no visibility into other agents or the host. Use this when running untrusted code or when compliance requires hypervisor isolation.

# Default: container isolation (works everywhere)
openlegion start

# Maximum security: microVM isolation (Docker Desktop 4.58+ required)
openlegion start --sandbox

Check compatibility: Run docker sandbox version — if it returns a version number, your Docker Desktop supports sandboxes. If not, update Docker Desktop to 4.58+.


CLI Reference

openlegion [--verbose/-v] [--quiet/-q] [--json]
├── start [--config PATH] [-d] [--sandbox]     # Start runtime + interactive REPL (inline setup on first run)
├── stop                                       # Stop all containers
├── chat [name] [--port PORT]                  # Connect to a running agent
├── status [--port PORT] [--wide/-w] [--watch N] [--json]  # Show agent status
├── version [--verbose/-v]                     # Show version and environment info
└── wallet                                     # Manage agent wallets
    ├── init                                   # Generate master wallet seed
    └── show [agent_id]                        # Show wallet addresses

Agent management, credentials, blackboard, cron, projects, and channels are managed via REPL commands (below) inside a running session, or via the web dashboard at http://localhost:8420.

Interactive REPL Commands

@agent <message>                     Send message to a specific agent
/use <agent>                         Switch active agent
/agents                              List all running agents
/add                                 Add a new agent (hot-adds to running system)
/agent [edit|view]                   Agent overview, config editing, workspace files
/edit [name]                         Edit agent settings (model, browser, budget)
/remove [name]                       Remove an agent
/restart [name]                      Restart an agent container
/status                              Show agent health
/broadcast <msg>                     Send message to all agents
/steer <msg>                         Inject message into busy agent's context
/history [agent]                     Show recent conversation messages
/costs                               Show today's LLM spend + context usage + model health
/blackboard [list|get|set|del]       View/edit shared blackboard entries
/queue                               Show agent task queue status
/cron [list|del|pause|resume|run]    Manage cron jobs
/project [list|use|info]              Manage multi-project namespaces
/credential [add|list|remove]        Manage API credentials
/debug [trace]                       Show recent request traces
/logs [--level LEVEL]                Show recent runtime logs
/addkey <svc> [key]                  Add an API credential to the vault
/removekey [name]                    Remove a credential from the vault
/reset                               Clear conversation with active agent
/quit                                Exit and stop runtime

Aliases: /exit = /quit, /agents = /status, /traces = /debug

Team Templates

Templates are offered during first-run setup (via openlegion start):

TemplateAgentsDescription
starterassistantSingle general-purpose agent
salesresearcher, qualifier, outreachSales pipeline team
devteampm, engineer, reviewerSoftware development team
contentresearcher, writer, editorContent creation team
deep-researchresearcher, analystDeep research and analysis team
monitormonitorAutonomous monitoring agent
competitive-intelresearcher, analyst, reporterMarket and competitor analysis
lead-enrichmentenricherLead data enrichment
price-intelligencemonitor, analystPrice monitoring and analysis
review-opsmonitor, responderReview and feedback management
social-listeninglistenerSocial media monitoring

Configuration

PROJECT.md — Per-Project Context

Each project has its own PROJECT.md stored in config/projects/{name}/project.md. It is mounted into project member agents' containers and loaded into their system prompts. Standalone agents (not in a project) do not receive any PROJECT.md.

# PROJECT.md

## What We're Building
SaaS platform for automated lead qualification

## Current Priority
Ship the email personalization pipeline this week

## Hard Constraints
- Budget: $50/day total across all agents
- No cold outreach to .edu or .gov domains

config/mesh.yaml — Framework Settings

mesh:
  host: "0.0.0.0"
  port: 8420

llm:
  default_model: "openai/gpt-4o-mini"
  embedding_model: "text-embedding-3-small"   # "none" to disable vector search

collaboration: true                           # allow agents to message each other (default: true for new agents)

config/agents.yaml — Agent Definitions

Created automatically by openlegion start (inline setup) or the /add REPL command.

agents:
  researcher:
    role: "research"
    model: "openai/gpt-4.1-mini"
    skills_dir: "./skills/researcher"
    initial_instructions: "You are a research specialist..."
    thinking: "medium"                   # off (default), low, medium, or high
    budget:
      daily_usd: 5.00
      monthly_usd: 100.00

config/permissions.json — Agent Permissions

Per-agent access control with glob patterns for blackboard paths and explicit allowlists for messaging, pub/sub, and API access.

.env — API Keys

Managed automatically by openlegion start (setup wizard) and the /addkey REPL command. You can also edit directly. Uses a two-tier prefix system:

# System tier — LLM provider keys (never accessible by agents)
OPENLEGION_SYSTEM_ANTHROPIC_API_KEY=sk-ant-...
OPENLEGION_SYSTEM_OPENAI_API_KEY=sk-...
OPENLEGION_SYSTEM_MOONSHOT_API_KEY=sk-...

# Agent tier — tool/service keys (access controlled per-agent)
OPENLEGION_CRED_BRAVE_SEARCH_API_KEY=BSA...

# Channel tokens
OPENLEGION_CRED_TELEGRAM_BOT_TOKEN=123456:ABC...
OPENLEGION_CRED_DISCORD_BOT_TOKEN=MTIz...
OPENLEGION_CRED_SLACK_BOT_TOKEN=xoxb-...
OPENLEGION_CRED_SLACK_APP_TOKEN=xapp-...
OPENLEGION_CRED_WHATSAPP_ACCESS_TOKEN=EAAx...
OPENLEGION_CRED_WHATSAPP_PHONE_NUMBER_ID=1234...

# Log format: "json" (default) or "text" (human-readable)
OPENLEGION_LOG_FORMAT=text

Connecting Channels

Channels are configured via the setup wizard during openlegion start, or by adding the appropriate tokens to .env directly:

# Telegram
OPENLEGION_CRED_TELEGRAM_BOT_TOKEN=123456:ABC...

# Discord
OPENLEGION_CRED_DISCORD_BOT_TOKEN=MTIz...

# Slack (both required)
OPENLEGION_CRED_SLACK_BOT_TOKEN=xoxb-...
OPENLEGION_CRED_SLACK_APP_TOKEN=xapp-...

# WhatsApp (both required)
OPENLEGION_CRED_WHATSAPP_ACCESS_TOKEN=EAAx...
OPENLEGION_CRED_WHATSAPP_PHONE_NUMBER_ID=1234...

On next openlegion start, a pairing code appears — send it to your bot to link.


MCP Tool Support

OpenLegion supports the Model Context Protocol (MCP) — the emerging standard for LLM tool interoperability. Any MCP-compatible tool server can be plugged into an agent via config, with tools automatically discovered and exposed to the LLM alongside built-in skills.

Configuration

Add mcp_servers to any agent in config/agents.yaml:

agents:
  researcher:
    role: "research"
    model: "openai/gpt-4.1-mini"
    mcp_servers:
      - name: filesystem
        command: mcp-server-filesystem
        args: ["/data"]
      - name: database
        command: mcp-server-sqlite
        args: ["--db", "/data/research.db"]

Each server is launched as a subprocess inside the agent container using stdio transport. Tools are discovered automatically via the MCP protocol and appear in the LLM's tool list alongside built-in skills.

How It Works

  1. Agent container reads MCP_SERVERS from environment (set by the runtime)
  2. MCPClient launches each server subprocess via stdio transport
  3. MCP protocol handshake discovers available tools and their schemas
  4. Tools are registered in SkillRegistry with OpenAI function-calling format
  5. LLM tool calls route through MCPClient.call_tool() to the correct server
  6. Name conflicts with built-in skills are resolved by prefixing (mcp_{server}_{tool})

Server Config Options

FieldTypeDescription
namestringServer identifier (used for logging and conflict prefixes)
commandstringCommand to launch the server
argslistCommand-line arguments (optional)
envdictEnvironment variables for the server process (optional)

See the full MCP Integration Guide for advanced usage, custom server setup, and troubleshooting.


Testing

# Unit and integration tests (fast, no Docker needed)
pytest tests/ --ignore=tests/test_e2e.py --ignore=tests/test_e2e_chat.py \
  --ignore=tests/test_e2e_memory.py --ignore=tests/test_e2e_triggering.py

# E2E tests (requires Docker + API key)
pytest tests/test_e2e.py tests/test_e2e_chat.py \
  tests/test_e2e_memory.py tests/test_e2e_triggering.py

# Everything
pytest tests/

Test Coverage

CategoryTestsWhat's Tested
Dashboard215Fleet management, blackboard, costs, traces, queues, cron, settings, config, streaming broadcast, workspace proxy, projects
Built-in Tools167run_command, file, browser tools, memory, mesh, vault, get_system_status, path traversal, discovery
Browser Service137Camoufox sessions, screenshots, reset/recovery, tab switching, anti-detection
Credentials110Vault, API proxy, provider detection, two-tier system, credential lifecycle
CLI99Agent add/list/edit/remove, chat, REPL commands, cron management, version
Workspace90File scaffold, loading, BM25 search, daily logs, learnings, heartbeat, identity files, SYSTEM.md
Agent Loop82Task execution, tool calling, cancellation, tool memory, chat helpers, daily log enrichment, task logging
Integration68Multi-component mesh operations, notifications
Mesh65Blackboard, PubSub, MessageRouter, permissions
Channels (base)62Abstract channel, commands, per-user routing, chunking, steer, debug, addkey normalization, parallel broadcast
Cron58Cron expressions, intervals, dispatch, persistence, enriched heartbeat, skip-LLM, concurrent mutations
Templates54Template loading, agent creation, model interpolation, all 11 templates
Runtime Backend54DockerBackend, SandboxBackend, extra_env, name sanitization, detection, VNC allocation
Projects42Multi-project CRUD, config, agent membership, blackboard key scoping, cross-project permission isolation
Context Manager41Token estimation (tiktoken + model-aware), compaction, flushing, flush reset
Sanitization38Invisible Unicode stripping, bidi overrides, tag chars, zero-width
Discord Channel36Slash commands, message routing, pairing, chunking, embed formatting
Agent Server35Workspace API, heartbeat-context endpoint, content sanitization, file allowlist
Skills34Discovery, execution, injection, MCP integration
Memory Store34SQLite ops, vector search, categories, hierarchical search, tool outcomes
Events31Event streaming, filtering, WebSocket, notification events
Traces30Trace recording, grouping, summaries, prompt preview extraction
Setup Wizard29Quickstart, full setup, API key validation, templates, inline setup, two-tier credentials
Chat28Chat mode, streaming, workspace integration
Models24Model cost registry, context windows, provider detection
Transcript24Transcript formatting, safety, round-trip fidelity
WhatsApp Channel22Cloud API, webhook verification, message chunking, non-text reply
Slack Channel22Socket Mode, thread routing, pairing, command translation
Attachments21Image base64 encoding, PDF text extraction, multimodal content blocks
Marketplace20Install, manifest parsing, validation, path traversal, remove
Costs20Usage recording, budgets, vault integration, budget overrun warnings
Dashboard Workspace19Workspace proxy endpoints, filename validation, transport forwarding, sanitization
Chat Workspace19Cross-session memory, corrections, learnings
Transport18HttpTransport, SandboxTransport, resolve_url
Subagent17Spawn, depth/concurrent limits, TTL timeout, skill cloning, memory isolation
LLM Params17Parameter allowlisting, model-specific options
Failover15Health tracking, chain cascade, cooldown
Loop Detector14Agent loop detection and intervention
Lanes14Per-agent FIFO task queues
Vault13Credential storage, generation, capture, budget enforcement
Dashboard Auth10Session cookies, HMAC verification, SSO flow
MCP Client10Tool discovery, routing, conflicts, lifecycle
Embedding Fallback10Graceful degradation when embeddings fail
Types9Pydantic validation, serialization
Health Monitor8Ephemeral cleanup, TTL expiry, event emission, restart with missing config
MCP E2E7Real MCP protocol with live server subprocess
Webhooks7Add/remove, persistence, dispatch
File Watchers7Polling, dispatch, pattern matching
Memory Tools6memory_search, memory_save
Memory Integration6Vector search, cross-task recall, salience
E2E17Container health, workflow, chat, memory, triggering
Web Search2DuckDuckGo search tool
Total2240

Dependencies

PackagePurpose
fastapiHTTP servers (mesh + agent + browser service)
uvicornASGI server
httpxAsync HTTP client
pydanticData validation
litellmMulti-provider LLM interface (100+ providers)
sqlite-vecVector search in SQLite
pyyamlYAML config parsing
clickCLI framework
dockerDocker API client
python-dotenv.env file loading
camoufoxStealth browser automation (in browser service container only)
mcpMCP tool server client (in agent container only, optional)
slack-boltSlack channel adapter (optional)

Dev: pytest, pytest-asyncio, pytest-cov, ruff.

No LangChain. No Redis. No Kubernetes. Real-time web dashboard. Optional channels: python-telegram-bot, discord.py, slack-bolt.


Project Structure

src/
├── cli/
│   ├── main.py                         # Click commands and entry point
│   ├── config.py                       # Config loading, Docker helpers, agent management
│   ├── runtime.py                      # RuntimeContext — full lifecycle management
│   ├── repl.py                         # REPLSession — interactive command dispatch
│   ├── channels.py                     # ChannelManager — messaging channel lifecycle
│   └── formatting.py                   # Tool display, styled output, response rendering
├── agent/
│   ├── __main__.py                     # Container entry
│   ├── loop.py                         # Execution loop (task + chat)
│   ├── loop_detector.py                # Tool loop detection (warn/block/terminate)
│   ├── skills.py                       # Skill registry + discovery
│   ├── mcp_client.py                   # MCP server lifecycle + tool routing
│   ├── memory.py                       # Hierarchical memory (SQLite + sqlite-vec + FTS5)
│   ├── workspace.py                    # Persistent workspace + BM25 search
│   ├── context.py                      # Context manager (token tracking, compaction)
│   ├── llm.py                          # LLM client (routes through mesh proxy)
│   ├── mesh_client.py                  # Mesh HTTP client
│   ├── server.py                       # Agent FastAPI server
│   └── builtins/
│       ├── exec_tool.py                # Shell execution
│       ├── file_tool.py                # File I/O (read, write, list)
│       ├── http_tool.py                # HTTP requests
│       ├── browser_tool.py             # Browser automation via shared Camoufox service
│       ├── web_search_tool.py          # Web search via DuckDuckGo
│       ├── memory_tool.py              # Memory search and save
│       ├── mesh_tool.py                # Shared state, fleet awareness, artifacts
│       ├── vault_tool.py               # Credential vault operations
│       ├── skill_tool.py               # Runtime skill creation + hot-reload
│       ├── introspect_tool.py          # Live runtime state queries
│       └── subagent_tool.py            # Spawn in-process subagents
├── host/
│   ├── server.py                       # Mesh FastAPI server
│   ├── mesh.py                         # Blackboard, PubSub, MessageRouter
│   ├── permissions.py                  # Permission matrix
│   ├── credentials.py                  # Credential vault + API proxy
│   ├── failover.py                     # Model health tracking + failover chains
│   ├── runtime.py                      # RuntimeBackend ABC + Docker/Sandbox backends
│   ├── transport.py                    # Transport ABC + Http/Sandbox transports
│   ├── containers.py                   # Backward-compat alias for DockerBackend
│   ├── cron.py                         # Cron scheduler + heartbeats
│   ├── webhooks.py                     # Named webhook endpoints
│   ├── watchers.py                     # File watchers (polling)
│   ├── costs.py                        # Cost tracking + budgets (SQLite)
│   ├── health.py                       # Health monitor + auto-restart
│   ├── lanes.py                        # Per-agent FIFO task queues
│   ├── traces.py                       # Request tracing + grouped summaries
│   └── transcript.py                   # Provider-specific transcript sanitization
├── shared/
│   ├── types.py                        # All Pydantic models (the contract)
│   ├── utils.py                        # ID generation, logging, sanitization
│   └── trace.py                        # Trace ID generation + correlation
├── browser/
│   ├── server.py                       # Browser service FastAPI server
│   ├── service.py                      # Camoufox session management
│   ├── stealth.py                      # Anti-detection configuration
│   └── redaction.py                    # Credential redaction for browser content
├── channels/
│   ├── base.py                         # Abstract channel with unified UX
│   ├── telegram.py                     # Telegram adapter
│   ├── discord.py                      # Discord adapter
│   ├── slack.py                        # Slack adapter (Socket Mode)
│   └── whatsapp.py                     # WhatsApp Cloud API adapter
├── dashboard/
│   ├── server.py                       # Dashboard FastAPI router + API
│   ├── events.py                       # EventBus for real-time streaming
│   ├── templates/index.html            # Dashboard UI (Alpine.js + Tailwind)
│   └── static/                         # CSS + JS assets
├── setup_wizard.py                    # Guided setup wizard
├── marketplace.py                     # Skill marketplace (git-based install/remove)
└── templates/
    ├── starter.yaml                    # Single-agent template
    ├── sales.yaml                      # Sales pipeline team
    ├── devteam.yaml                    # Dev team template
    ├── content.yaml                    # Content creation team
    ├── deep-research.yaml              # Deep research and analysis team
    ├── monitor.yaml                    # Autonomous monitoring agent
    ├── competitive-intel.yaml          # Competitive intelligence team
    ├── lead-enrichment.yaml            # Lead data enrichment
    ├── price-intelligence.yaml         # Price monitoring and analysis
    ├── review-ops.yaml                 # Review and feedback management
    └── social-listening.yaml           # Social media monitoring

config/
├── mesh.yaml                           # Framework settings
├── agents.yaml                         # Agent definitions (per-project)
├── permissions.json                    # Per-agent ACLs
└── projects/                           # Multi-project namespaces

Design Principles

PrincipleRationale
Messages, not method callsAgents communicate through HTTP/JSON. Never shared memory or direct invocation.
The mesh is the only doorNo agent has network access except through the mesh. No agent holds credentials.
Private by default, shared by promotionAgents keep knowledge private. Facts are explicitly promoted to the blackboard.
Explicit failure handlingDomain-specific exceptions propagated with context. No silent error swallowing.
Small enough to audit~32,000 total lines. The entire codebase is auditable in a day.
Skills over featuresNew capabilities are agent skills, not mesh code.
SQLite for all stateSingle-file databases. No external services. WAL mode for concurrent reads.
Zero vendor lock-inLiteLLM supports 100+ providers. Markdown workspace files. No proprietary formats.

License

OpenLegion.ai is source-available under the Business Source License 1.1 (BSL).

You may view, modify, and self-host the software.

You may NOT offer it as a competing hosted or SaaS product.

See LICENSE for details.


Related Projects & Comparisons

Looking for alternatives? OpenLegion is often compared to:

  • OpenClaw — personal AI assistant, 200K+ stars, not designed for production security
  • nanobot — ultra-lightweight Python agent (~4K lines), limited multi-agent support
  • ZeroClaw — Rust-based AI agent runtime, extreme resource efficiency, early-stage
  • NanoClaw — container-isolated, Claude-only, no cost tracking
  • LangChain Agents — feature-rich but complex, heavy framework overhead
  • CrewAI — multi-agent framework, no built-in container isolation or cost controls
  • AutoGen — Microsoft's multi-agent framework, requires Azure/OpenAI, no self-hosting

OpenLegion differs from all of these in combining fleet orchestration, Docker isolation, credential vaulting, and cost enforcement in a single ~32,000 line auditable codebase.

Keywords: autonomous AI agents, multi-agent framework, LLM agent orchestration, self-hosted AI agents, Docker AI agents, OpenClaw alternative, AI agent security, agent cost tracking, Telegram AI bot, Python AI agent framework

Workspace

这里展示的是当前已发布快照。新的发布会覆盖这个视图。

下载 .zip
18 文件数更新时间 2026/03/18 00:57:48 UTC
发布方式 clawlodge-cli/0.1.8
AGENTS.mdtext · 9 B

社区

还没有评论。

相关推荐