This page explains how the AIAgent constructs the system prompt, manages contextual inputs from project files such as SOUL.md, dynamically injects user-specified context, and manages LLM context window limits via compression and auxiliary models.
The central function for constructing the system prompt is AIAgent._build_system_prompt(), which delegates assembly to build_system_prompt_parts agent/system_prompt.py146-234 This method assembles a layered prompt including the agent's identity, behavioral instructions, tools, skills, and contextual project files.
self._cached_system_prompt to avoid repeated costly rebuilds. It is invalidated when factors like directory changes or context compression occur agent/system_prompt.py1-9agent/prompt_builder.py and agent/system_prompt.py as functions that return string snippets agent/prompt_builder.py1-5SOUL.md or default), tool guidance, skills prompt, environment/platform hints, and model-specific operational guidance.HERMES.md, .cursorrules) and the initial caller-supplied system_message.MEMORY.md), user profiles (USER.md), external memory provider blocks, and session metadata.| Layer | Source / Function | Purpose |
|---|---|---|
| Agent Identity | load_soul_md() or DEFAULT_AGENT_IDENTITY | Core persona and behavioral tone agent/prompt_builder.py131-147 |
| Help Guidance | HERMES_AGENT_HELP_GUIDANCE | Directs agent to official docs for self-troubleshooting agent/prompt_builder.py149-158 |
| Memory Guidance | MEMORY_GUIDANCE | Instructions on how to use the memory tool effectively agent/prompt_builder.py160-170 |
| Skills Guidance | SKILLS_GUIDANCE | Defines the progressive disclosure pattern for skill usage agent/prompt_builder.py190-195 |
| Tool Enforcement | TOOL_USE_ENFORCEMENT_GUIDANCE | Specialized formatting for models like GPT-4o agent/prompt_builder.py209-225 |
| Platform Hints | _resolve_platform_hint() | Platform-specific instructions (e.g., TUI embedded pane) agent/system_prompt.py67-114 |
Title: "System Prompt Assembly Flow"
Sources: agent/prompt_builder.py127-225 agent/system_prompt.py146-234 agent/system_prompt.py1-22
Hermes Agent discovers context and persona information hierarchically to ground the agent in the user's project environment:
SOUL.md): Defines the agent's personality. Located at ${HERMES_HOME}/SOUL.md. If missing, the agent falls back to DEFAULT_AGENT_IDENTITY agent/prompt_builder.py131-147_find_hermes_md() which searches agent/prompt_builder.py93-114:
.hermes.md or HERMES.md: Native project instructions (searches up to git root).AGENTS.md: Common agent instruction file..cursorrules: Compatibility with Cursor IDE rules._scan_context_content() which utilizes _scan_for_threats (scope="context") to block prompt injection patterns agent/prompt_builder.py50-74When running in a code workspace (detected by _PROJECT_MARKERS), the agent adopts a coding posture. This injects a workspace snapshot into the system prompt via build_coding_workspace_block(), including the current git branch, dirty status, and project facts agent/coding_context.py32-37
Sources: agent/prompt_builder.py50-74 agent/prompt_builder.py93-114 agent/coding_context.py1-50
Long conversations are managed by the ContextCompressor class, which summarizes conversation history to fit within model limits while protecting recent turns agent/context_compressor.py1-17
Before initiating compression, the agent performs checks in build_turn_context agent/turn_context.py1-23:
ContextCompressor checks should_compress() based on last_prompt_tokens vs threshold_tokens agent/context_compressor.py84-100SUMMARY_PREFIX and HISTORICAL_TASK_HEADING to ensure the agent treats them as reference only agent/context_compressor.py92-122The build_turn_context function performs per-turn setup, including runtime wiring and preflight context compression agent/turn_context.py1-15 This produces a TurnContext object containing the sanitized messages and the active system prompt for the conversation loop.
Title: "Context Compression Pipeline"
Sources: agent/context_compressor.py1-122 agent/turn_context.py1-15 agent/context_compressor.py84-100
Hermes uses an api_content sidecar pattern to inject ephemeral context into the API copy of a message while keeping the stored transcript clean agent/turn_context.py52-71
compose_user_api_content appends memory prefetch and plugin context to the user message agent/turn_context.py72-84substitute_api_content swaps the clean content for the sidecar right before the API call to maintain prompt-cache stability agent/turn_context.py87-108Large tool outputs are managed via enforce_turn_budget to prevent a single tool response from exhausting the context window agent/tool_executor.py47-51
_budget_for_agent resolves a BudgetConfig scaled to the model's context window agent/tool_executor.py78-92_summarize_tool_result prunes large tool outputs (like web_extract content) to a text summary agent/context_compressor.py14-17Sources: agent/turn_context.py52-108 agent/tool_executor.py47-92 agent/context_compressor.py14-17
Title: "Context Management - NL to Code Mapping"
Sources: agent/prompt_builder.py1-147 agent/system_prompt.py146-234 agent/context_compressor.py1-40 agent/turn_context.py52-84
Refresh this wiki
This wiki was recently refreshed. Please wait 4 days to refresh again.