| --- |
| sidebar_position: 1 |
| title: "Architecture" |
| description: "Hermes Agent internals β major subsystems, execution paths, data flow, and where to read next" |
| --- |
| |
| # Architecture |
|
|
| This page is the top-level map of Hermes Agent internals. Use it to orient yourself in the codebase, then dive into subsystem-specific docs for implementation details. |
|
|
| ## System Overview |
|
|
| ```text |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
| β Entry Points β |
| β β |
| β CLI (cli.py) Gateway (gateway/run.py) ACP (acp_adapter/) β |
| β Batch Runner API Server Python Library β |
| ββββββββββββ¬βββββββββββββββ¬ββββββββββββββββββββββββ¬ββββββββββββββββββββ |
| β β β |
| βΌ βΌ βΌ |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
| β AIAgent (run_agent.py) β |
| β β |
| β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β |
| β β Prompt β β Provider β β Tool β β |
| β β Builder β β Resolution β β Dispatch β β |
| β β (prompt_ β β (runtime_ β β (model_ β β |
| β β builder.py) β β provider.py)β β tools.py) β β |
| β ββββββββ¬ββββββββ ββββββββ¬ββββββββ ββββββββ¬ββββββββ β |
| β β β β β |
| β ββββββββ΄ββββββββ ββββββββ΄ββββββββ ββββββββ΄ββββββββ β |
| β β Compression β β 3 API Modes β β Tool Registryβ β |
| β β & Caching β β chat_compl. β β (registry.py)β β |
| β β β β codex_resp. β β 47 tools β β |
| β β β β anthropic β β 19 toolsets β β |
| β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
| β β |
| βΌ βΌ |
| βββββββββββββββββββββ ββββββββββββββββββββββββ |
| β Session Storage β β Tool Backends β |
| β (SQLite + FTS5) β β Terminal (6 backends) β |
| β hermes_state.py β β Browser (5 backends) β |
| β gateway/session.pyβ β Web (4 backends) β |
| βββββββββββββββββββββ β MCP (dynamic) β |
| β File, Vision, etc. β |
| ββββββββββββββββββββββββ |
| ``` |
|
|
| ## Directory Structure |
|
|
| ```text |
| hermes-agent/ |
| βββ run_agent.py # AIAgent β core conversation loop (~10,700 lines) |
| βββ cli.py # HermesCLI β interactive terminal UI (~10,000 lines) |
| βββ model_tools.py # Tool discovery, schema collection, dispatch |
| βββ toolsets.py # Tool groupings and platform presets |
| βββ hermes_state.py # SQLite session/state database with FTS5 |
| βββ hermes_constants.py # HERMES_HOME, profile-aware paths |
| βββ batch_runner.py # Batch trajectory generation |
| β |
| βββ agent/ # Agent internals |
| β βββ prompt_builder.py # System prompt assembly |
| β βββ context_engine.py # ContextEngine ABC (pluggable) |
| β βββ context_compressor.py # Default engine β lossy summarization |
| β βββ prompt_caching.py # Anthropic prompt caching |
| β βββ auxiliary_client.py # Auxiliary LLM for side tasks (vision, summarization) |
| β βββ model_metadata.py # Model context lengths, token estimation |
| β βββ models_dev.py # models.dev registry integration |
| β βββ anthropic_adapter.py # Anthropic Messages API format conversion |
| β βββ display.py # KawaiiSpinner, tool preview formatting |
| β βββ skill_commands.py # Skill slash commands |
| β βββ memory_manager.py # Memory manager orchestration |
| β βββ memory_provider.py # Memory provider ABC |
| β βββ trajectory.py # Trajectory saving helpers |
| β |
| βββ hermes_cli/ # CLI subcommands and setup |
| β βββ main.py # Entry point β all `hermes` subcommands (~6,000 lines) |
| β βββ config.py # DEFAULT_CONFIG, OPTIONAL_ENV_VARS, migration |
| β βββ commands.py # COMMAND_REGISTRY β central slash command definitions |
| β βββ auth.py # PROVIDER_REGISTRY, credential resolution |
| β βββ runtime_provider.py # Provider β api_mode + credentials |
| β βββ models.py # Model catalog, provider model lists |
| β βββ model_switch.py # /model command logic (CLI + gateway shared) |
| β βββ setup.py # Interactive setup wizard (~3,100 lines) |
| β βββ skin_engine.py # CLI theming engine |
| β βββ skills_config.py # hermes skills β enable/disable per platform |
| β βββ skills_hub.py # /skills slash command |
| β βββ tools_config.py # hermes tools β enable/disable per platform |
| β βββ plugins.py # PluginManager β discovery, loading, hooks |
| β βββ callbacks.py # Terminal callbacks (clarify, sudo, approval) |
| β βββ gateway.py # hermes gateway start/stop |
| β |
| βββ tools/ # Tool implementations (one file per tool) |
| β βββ registry.py # Central tool registry |
| β βββ approval.py # Dangerous command detection |
| β βββ terminal_tool.py # Terminal orchestration |
| β βββ process_registry.py # Background process management |
| β βββ file_tools.py # read_file, write_file, patch, search_files |
| β βββ web_tools.py # web_search, web_extract |
| β βββ browser_tool.py # 10 browser automation tools |
| β βββ code_execution_tool.py # execute_code sandbox |
| β βββ delegate_tool.py # Subagent delegation |
| β βββ mcp_tool.py # MCP client (~2,200 lines) |
| β βββ credential_files.py # File-based credential passthrough |
| β βββ env_passthrough.py # Env var passthrough for sandboxes |
| β βββ ansi_strip.py # ANSI escape stripping |
| β βββ environments/ # Terminal backends (local, docker, ssh, modal, daytona, singularity) |
| β |
| βββ gateway/ # Messaging platform gateway |
| β βββ run.py # GatewayRunner β message dispatch (~9,000 lines) |
| β βββ session.py # SessionStore β conversation persistence |
| β βββ delivery.py # Outbound message delivery |
| β βββ pairing.py # DM pairing authorization |
| β βββ hooks.py # Hook discovery and lifecycle events |
| β βββ mirror.py # Cross-session message mirroring |
| β βββ status.py # Token locks, profile-scoped process tracking |
| β βββ builtin_hooks/ # Always-registered hooks |
| β βββ platforms/ # 18 adapters: telegram, discord, slack, whatsapp, |
| β # signal, matrix, mattermost, email, sms, |
| β # dingtalk, feishu, wecom, wecom_callback, weixin, |
| β # bluebubbles, qqbot, homeassistant, webhook, api_server |
| β |
| βββ acp_adapter/ # ACP server (VS Code / Zed / JetBrains) |
| βββ cron/ # Scheduler (jobs.py, scheduler.py) |
| βββ plugins/memory/ # Memory provider plugins |
| βββ plugins/context_engine/ # Context engine plugins |
| βββ environments/ # RL training environments (Atropos) |
| βββ skills/ # Bundled skills (always available) |
| βββ optional-skills/ # Official optional skills (install explicitly) |
| βββ website/ # Docusaurus documentation site |
| βββ tests/ # Pytest suite (~3,000+ tests) |
| ``` |
|
|
| ## Data Flow |
|
|
| ### CLI Session |
|
|
| ```text |
| User input β HermesCLI.process_input() |
| β AIAgent.run_conversation() |
| β prompt_builder.build_system_prompt() |
| β runtime_provider.resolve_runtime_provider() |
| β API call (chat_completions / codex_responses / anthropic_messages) |
| β tool_calls? β model_tools.handle_function_call() β loop |
| β final response β display β save to SessionDB |
| ``` |
|
|
| ### Gateway Message |
|
|
| ```text |
| Platform event β Adapter.on_message() β MessageEvent |
| β GatewayRunner._handle_message() |
| β authorize user |
| β resolve session key |
| β create AIAgent with session history |
| β AIAgent.run_conversation() |
| β deliver response back through adapter |
| ``` |
|
|
| ### Cron Job |
|
|
| ```text |
| Scheduler tick β load due jobs from jobs.json |
| β create fresh AIAgent (no history) |
| β inject attached skills as context |
| β run job prompt |
| β deliver response to target platform |
| β update job state and next_run |
| ``` |
|
|
| ## Recommended Reading Order |
|
|
| If you are new to the codebase: |
|
|
| 1. **This page** β orient yourself |
| 2. **[Agent Loop Internals](./agent-loop.md)** β how AIAgent works |
| 3. **[Prompt Assembly](./prompt-assembly.md)** β system prompt construction |
| 4. **[Provider Runtime Resolution](./provider-runtime.md)** β how providers are selected |
| 5. **[Adding Providers](./adding-providers.md)** β practical guide to adding a new provider |
| 6. **[Tools Runtime](./tools-runtime.md)** β tool registry, dispatch, environments |
| 7. **[Session Storage](./session-storage.md)** β SQLite schema, FTS5, session lineage |
| 8. **[Gateway Internals](./gateway-internals.md)** β messaging platform gateway |
| 9. **[Context Compression & Prompt Caching](./context-compression-and-caching.md)** β compression and caching |
| 10. **[ACP Internals](./acp-internals.md)** β IDE integration |
| 11. **[Environments, Benchmarks & Data Generation](./environments.md)** β RL training |
|
|
| ## Major Subsystems |
|
|
| ### Agent Loop |
|
|
| The synchronous orchestration engine (`AIAgent` in `run_agent.py`). Handles provider selection, prompt construction, tool execution, retries, fallback, callbacks, compression, and persistence. Supports three API modes for different provider backends. |
|
|
| β [Agent Loop Internals](./agent-loop.md) |
|
|
| ### Prompt System |
|
|
| Prompt construction and maintenance across the conversation lifecycle: |
|
|
| - **`prompt_builder.py`** β Assembles the system prompt from: personality (SOUL.md), memory (MEMORY.md, USER.md), skills, context files (AGENTS.md, .hermes.md), tool-use guidance, and model-specific instructions |
| - **`prompt_caching.py`** β Applies Anthropic cache breakpoints for prefix caching |
| - **`context_compressor.py`** β Summarizes middle conversation turns when context exceeds thresholds |
| |
| β [Prompt Assembly](./prompt-assembly.md), [Context Compression & Prompt Caching](./context-compression-and-caching.md) |
| |
| ### Provider Resolution |
| |
| A shared runtime resolver used by CLI, gateway, cron, ACP, and auxiliary calls. Maps `(provider, model)` tuples to `(api_mode, api_key, base_url)`. Handles 18+ providers, OAuth flows, credential pools, and alias resolution. |
| |
| β [Provider Runtime Resolution](./provider-runtime.md) |
| |
| ### Tool System |
| |
| Central tool registry (`tools/registry.py`) with 47 registered tools across 19 toolsets. Each tool file self-registers at import time. The registry handles schema collection, dispatch, availability checking, and error wrapping. Terminal tools support 6 backends (local, Docker, SSH, Daytona, Modal, Singularity). |
| |
| β [Tools Runtime](./tools-runtime.md) |
| |
| ### Session Persistence |
| |
| SQLite-based session storage with FTS5 full-text search. Sessions have lineage tracking (parent/child across compressions), per-platform isolation, and atomic writes with contention handling. |
| |
| β [Session Storage](./session-storage.md) |
| |
| ### Messaging Gateway |
| |
| Long-running process with 18 platform adapters, unified session routing, user authorization (allowlists + DM pairing), slash command dispatch, hook system, cron ticking, and background maintenance. |
| |
| β [Gateway Internals](./gateway-internals.md) |
| |
| ### Plugin System |
| |
| Three discovery sources: `~/.hermes/plugins/` (user), `.hermes/plugins/` (project), and pip entry points. Plugins register tools, hooks, and CLI commands through a context API. Two specialized plugin types exist: memory providers (`plugins/memory/`) and context engines (`plugins/context_engine/`). Both are single-select β only one of each can be active at a time, configured via `hermes plugins` or `config.yaml`. |
| |
| β [Plugin Guide](/docs/guides/build-a-hermes-plugin), [Memory Provider Plugin](./memory-provider-plugin.md) |
| |
| ### Cron |
| |
| First-class agent tasks (not shell tasks). Jobs store in JSON, support multiple schedule formats, can attach skills and scripts, and deliver to any platform. |
| |
| β [Cron Internals](./cron-internals.md) |
| |
| ### ACP Integration |
| |
| Exposes Hermes as an editor-native agent over stdio/JSON-RPC for VS Code, Zed, and JetBrains. |
| |
| β [ACP Internals](./acp-internals.md) |
| |
| ### RL / Environments / Trajectories |
| |
| Full environment framework for evaluation and RL training. Integrates with Atropos, supports multiple tool-call parsers, and generates ShareGPT-format trajectories. |
| |
| β [Environments, Benchmarks & Data Generation](./environments.md), [Trajectories & Training Format](./trajectory-format.md) |
| |
| ## Design Principles |
| |
| | Principle | What it means in practice | |
| |-----------|--------------------------| |
| | **Prompt stability** | System prompt doesn't change mid-conversation. No cache-breaking mutations except explicit user actions (`/model`). | |
| | **Observable execution** | Every tool call is visible to the user via callbacks. Progress updates in CLI (spinner) and gateway (chat messages). | |
| | **Interruptible** | API calls and tool execution can be cancelled mid-flight by user input or signals. | |
| | **Platform-agnostic core** | One AIAgent class serves CLI, gateway, ACP, batch, and API server. Platform differences live in the entry point, not the agent. | |
| | **Loose coupling** | Optional subsystems (MCP, plugins, memory providers, RL environments) use registry patterns and check_fn gating, not hard dependencies. | |
| | **Profile isolation** | Each profile (`hermes -p <name>`) gets its own HERMES_HOME, config, memory, sessions, and gateway PID. Multiple profiles run concurrently. | |
| |
| ## File Dependency Chain |
| |
| ```text |
| tools/registry.py (no deps β imported by all tool files) |
| β |
| tools/*.py (each calls registry.register() at import time) |
| β |
| model_tools.py (imports tools/registry + triggers tool discovery) |
| β |
| run_agent.py, cli.py, batch_runner.py, environments/ |
| ``` |
| |
| This chain means tool registration happens at import time, before any agent instance is created. Any `tools/*.py` file with a top-level `registry.register()` call is auto-discovered β no manual import list needed. |
|
|