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 (6 backends) โ
โ hermes_state.py โ โ Browser (5 backends) โ
โ gateway/session.pyโ โ Web (4 backends) โ
โโโโโโโโโโโโโโโโโโโโโ โ MCP (dynamic) โ
โ File, Vision, etc. โ
โโโโโโโโโโโโโโโโโโโโโโโโ
Directory Structureโ
hermes-agent/
โโโ run_agent.py # AIAgent โ core conversation loop (large file)
โโโ cli.py # HermesCLI โ interactive terminal UI (large file)
โโโ 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 (large file)
โ โโโ 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 (large file)
โ โโโ 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 (large file)
โ โโโ 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 (large file)
โ โโโ 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/ # Extension point for always-registered hooks (none shipped)
โ โโโ platforms/ # 20 adapters: telegram, discord, slack, whatsapp,
โ # signal, matrix, mattermost, email, sms,
โ # dingtalk, feishu, wecom, wecom_callback, weixin,
โ # bluebubbles, qqbot, homeassistant, webhook, api_server,
โ # yuanbao
โ
โโโ 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? โ model_tools.handle_function_call() โ loop
โ final response โ display โ save to SessionDB
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 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.
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 backends (local, Docker, SSH, Daytona, Modal, 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 20 platform adapters, 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)
โ
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.