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โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 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. โ โ 70+ tools โ โ
โ โ โ โ anthropic โ โ 28 toolsets โ โ
โ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโดโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โผ โผ
โโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโ
โ Session Storage โ โ Tool Backends โ
โ (SQLite + FTS5) โ โ Terminal (7 backends) โ
โ persistence/ โ โ Browser (5 backends) โ
โ gateway/session.pyโ โ Web (4 backends) โ
โโโโโโโโโโโโโโโโโโโโโ โ MCP (dynamic) โ
โ File, Vision, etc. โ
โโโโโโโโโโโโโโโโโโโโโโโโ
Directory Structureโ
hermes-agent/
โโโ run_agent.py # AIAgent facade โ loop lives in agent/conversation_loop.py + agent/turn_*.py
โโโ cli.py # HermesCLI facade โ mixins in hermes_cli/cli_*_mixin.py
โโโ persistence/ # Storage contracts; persistence/sqlite/ is the SQLite session/state backend
โโโ persistence_assembly.py # open_persistence(): the one entry point to a profile's store
โโโ 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 โ `hermes` subcommands (parsers in subcommands/, main_*.py)
โ โโโ config.py # DEFAULT_CONFIG, OPTIONAL_ENV_VARS, migration
โ โโโ commands.py # COMMAND_REGISTRY โ central slash command definitions
โ โโโ auth.py # PROVIDER_REGISTRY, credential resolution (+ auth_*.py siblings)
โ โโโ 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 (+ setup_*.py siblings)
โ โโโ 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_use_cli.py # browser_exec โ Python over CDP (browser-harness)
โ โโโ code_execution_tool.py # execute_code sandbox
โ โโโ delegate_task_tool.py # The delegate_task tool (runtime: agent/subagents/)
โ โโโ mcp/ # MCP client package (client.py + sibling modules)
โ โโโ 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, vercel_sandbox)
โ
โโโ browser/ # Browser sessions, Chromium launch and the CDP supervisor
โ โโโ session.py # Per-task browser sessions (+ session_*.py siblings)
โ โโโ launcher.py # Local Chromium launch, reuse and orphan reaping
โ โโโ supervisor.py # Persistent CDP supervisor (dialogs, frames)
โ
โโโ gateway/ # Messaging platform gateway
โ โโโ run.py # GatewayRunner facade โ message dispatch (+ run_*.py siblings)
โ โโโ 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
โ โโโ platforms/ # Core ingress adapters: api_server, webhook,
โ # msgraph_webhook
โ
โโโ plugins/platforms/ # Bundled platform plugins: telegram, discord, slack,
โ # whatsapp, matrix, mattermost, email, sms,
โ # feishu, irc, line, teams, google_chat,
โ # buzz, ntfy, photon, raft, simplex, whatsapp_cloud,
โ # signal, weixin, bluebubbles
โ # (Home Assistant: `homeassistant` catalog plugin;
โ # QQ Bot: official `qqbot` plugin, installed by default;
โ # Yuanbao, DingTalk, WeCom: Hermes Labs plugins)
โ
โโโ 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
โโโ skills/ # Bundled skills (always available)
โโโ optional-skills/ # Official optional skills (install explicitly)
โโโ website/ # Docusaurus documentation site
โโโ tests/ # Pytest suite (~25,000 tests across ~1,250 files)
Data Flowโ
CLI Sessionโ
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? โ tools.calls.handle_function_call() โ loop
โ final response โ display โ save to the session store
Gateway Messageโ
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โ
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:
- This page โ orient yourself
- Agent Loop Internals โ how AIAgent works
- Prompt Assembly โ system prompt construction
- Provider Runtime Resolution โ how providers are selected
- Adding Providers โ practical guide to adding a new provider
- Tools Runtime โ tool registry, dispatch, environments
- Session Storage โ SQLite schema, FTS5, session lineage
- Gateway Internals โ messaging platform gateway
- Context Compression & Prompt Caching โ compression and caching
- ACP Internals โ IDE integration
Major Subsystemsโ
Agent Loopโ
The synchronous orchestration engine (AIAgent, exposed by the run_agent.py facade; the loop lives in agent/conversation_loop.py and agent/turn_*.py). Handles provider selection, prompt construction, tool execution, retries, fallback, callbacks, compression, and persistence. Supports three API modes for different provider backends.
Prompt Systemโ
Prompt construction and maintenance across the conversation lifecycle:
system_prompt.py+prompt_builder.pyโ assembles the ordered system-prompt tiers (stableโcontextโvolatile): identity/tool guidance/skills, context files, then memory/profile/timestamp blocksprompt_caching.pyโ Applies Anthropic cache breakpoints for prefix cachingcontext_compressor.pyโ Summarizes middle conversation turns when context exceeds thresholds
โ Prompt Assembly, Context Compression & Prompt Caching
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
Tool Systemโ
Central tool registry (tools/registry.py) with 70+ registered tools across ~28 toolsets. Each tool file self-registers at import time. The registry handles schema collection, dispatch, availability checking, and error wrapping. Terminal tools support 6 built-in backends (local, Docker, SSH, Daytona, Modal, Vercel Sandbox) plus plugin backends (e.g. Singularity).
โ Tools Runtime
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
Messaging Gatewayโ
Long-running process with 25+ platform adapters (built-in + bundled plugins), unified session routing, user authorization (allowlists + DM pairing), slash command dispatch, hook system, cron ticking, and background maintenance.
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, Memory Provider Plugin
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
ACP Integrationโ
Exposes Hermes as an editor-native agent over stdio/JSON-RPC for VS Code, Zed, and JetBrains.
โ ACP Internals
Trajectoriesโ
Generates ShareGPT-format trajectories from agent sessions for training data generation.
โ Trajectories & Training Format
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โ
tools/registry.py (no deps โ imported by all tool files)
โ
tools/*.py (each calls registry.register() at import time)
โ
tools/schemas.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. discover_builtin_tools() imports the tool modules named in _TOOL_MODULES in tools/registry.py; just check fails when a file that registers a tool is missing from that list.