Skip to main content

提示词组装

Hermes 明确区分了:

  • 缓存的系统提示状态
  • 仅在 API 调用时临时添加的内容

这是该项目最重要的设计决策之一,因为它影响到:

  • Token 使用量
  • 提示词缓存效率
  • 会话连续性
  • 内存正确性

主要文件:

  • run_agent.py
  • agent/prompt_builder.py
  • tools/memory_tool.py

缓存的系统提示层

缓存的系统提示按以下顺序组装:

  1. 代理身份 —— 从 SOUL.md 加载(若可用),否则回退至 DEFAULT_AGENT_IDENTITY 中的默认值,位于 prompt_builder.py
  2. 工具感知的行为指导
  3. Honcho 静态块(启用时)
  4. 可选的系统消息
  5. 冻结的 MEMORY 快照
  6. 冻结的 USER 配置文件快照
  7. 技能索引
  8. 上下文文件(AGENTS.md.cursorrules.cursor/rules/*.mdc)—— 若 SOUL.md 已作为身份加载,则不再在此处包含
  9. 时间戳 / 可选会话 ID
  10. 平台提示

skip_context_files 被设置时(例如子代理委派),SOUL.md 不会被加载,而是使用硬编码的 DEFAULT_AGENT_IDENTITY 代替。

实际示例:组装后的系统提示

以下是所有层级均存在时最终系统提示的简化视图(注释标明各部分来源):

# Layer 1: Agent Identity (from ~/.hermes/SOUL.md)
You are Hermes, an AI assistant created by Nous Research.
You are an expert software engineer and researcher.
You value correctness, clarity, and efficiency.
...

# Layer 2: Tool-aware behavior guidance
You have persistent memory across sessions. Save durable facts using
the memory tool: user preferences, environment details, tool quirks,
and stable conventions. Memory is injected into every turn, so keep
it compact and focused on facts that will still matter later.
...
When the user references something from a past conversation or you
suspect relevant cross-session context exists, use session_search
to recall it before asking them to repeat themselves.

# Tool-use enforcement (for GPT/Codex models only)
You MUST use your tools to take action — do not describe what you
would do or plan to do without actually doing it.
...

# Layer 3: Honcho static block (when active)
[Honcho personality/context data]

# Layer 4: Optional system message (from config or API)
[User-configured system message override]

# Layer 5: Frozen MEMORY snapshot
## Persistent Memory
- User prefers Python 3.12, uses pyproject.toml
- Default editor is nvim
- Working on project "atlas" in ~/code/atlas
- Timezone: US/Pacific

# Layer 6: Frozen USER profile snapshot
## User Profile
- Name: Alice
- GitHub: alice-dev

# Layer 7: Skills index
## Skills (mandatory)
Before replying, scan the skills below. If one clearly matches
your task, load it with skill_view(name) and follow its instructions.
...
<available_skills>
software-development:
- code-review: Structured code review workflow
- test-driven-development: TDD methodology
research:
- arxiv: Search and summarize arXiv papers
</available_skills>

# Layer 8: Context files (from project directory)
# Project Context
The following project context files have been loaded and should be followed:

## AGENTS.md
This is the atlas project. Use pytest for testing. The main
entry point is src/atlas/main.py. Always run `make lint` before
committing.

# Layer 9: Timestamp + session
Current time: 2026-03-30T14:30:00-07:00
Session: abc123

# Layer 10: Platform hint
You are a CLI AI Agent. Try not to use markdown but simple text
renderable inside a terminal.

SOUL.md 在提示中的呈现方式

SOUL.md 位于 ~/.hermes/SOUL.md,作为代理的身份标识——即系统提示的第一部分。prompt_builder.py 中的加载逻辑如下:

# From agent/prompt_builder.py (simplified)
def load_soul_md() -> Optional[str]:
soul_path = get_hermes_home() / "SOUL.md"
if not soul_path.exists():
return None
content = soul_path.read_text(encoding="utf-8").strip()
content = _scan_context_content(content, "SOUL.md") # Security scan
content = _truncate_content(content, "SOUL.md") # Cap at 20k chars
return content

load_soul_md() 返回内容时,它将替换硬编码的 DEFAULT_AGENT_IDENTITY。随后调用 build_context_files_prompt() 函数,并传入 skip_soul=True,以防止 SOUL.md 被重复出现(一次作为身份,一次作为上下文文件)。

如果 SOUL.md 不存在,则系统回退至:

You are Hermes Agent, an intelligent AI assistant created by Nous Research.
You are helpful, knowledgeable, and direct. You assist users with a wide
range of tasks including answering questions, writing and editing code,
analyzing information, creative work, and executing actions via your tools.
You communicate clearly, admit uncertainty when appropriate, and prioritize
being genuinely useful over being verbose unless otherwise directed below.
Be targeted and efficient in your exploration and investigations.

上下文文件的注入方式

build_context_files_prompt() 使用一种优先级机制——仅加载一种项目上下文类型(首个匹配项胜出):

# From agent/prompt_builder.py (simplified)
def build_context_files_prompt(cwd=None, skip_soul=False):
cwd_path = Path(cwd).resolve()

# Priority: first match wins — only ONE project context loaded
project_context = (
_load_hermes_md(cwd_path) # 1. .hermes.md / HERMES.md (walks to git root)
or _load_agents_md(cwd_path) # 2. AGENTS.md (cwd only)
or _load_claude_md(cwd_path) # 3. CLAUDE.md (cwd only)
or _load_cursorrules(cwd_path) # 4. .cursorrules / .cursor/rules/*.mdc
)

sections = []
if project_context:
sections.append(project_context)

# SOUL.md from HERMES_HOME (independent of project context)
if not skip_soul:
soul_content = load_soul_md()
if soul_content:
sections.append(soul_content)

if not sections:
return ""

return (
"# Project Context\n\n"
"The following project context files have been loaded "
"and should be followed:\n\n"
+ "\n".join(sections)
)

上下文文件发现细节

优先级文件搜索范围备注
1.hermes.mdHERMES.md当前工作目录至 Git 根目录Hermes 原生项目配置
2AGENTS.md仅当前工作目录常见的代理指令文件
3CLAUDE.md仅当前工作目录Claude Code 兼容性
4.cursorrules.cursor/rules/*.mdc仅当前工作目录Cursor 兼容性

所有上下文文件均经过:

  • 安全扫描 —— 检查提示注入模式(不可见 Unicode、"忽略之前指令"、凭据泄露尝试等)
  • 截断处理 —— 使用 70/20 的头部/尾部比例,限制在 20,000 字符以内,并添加截断标记
  • 移除 YAML 前置元数据 —— 移除 .hermes.md 的前置元数据(保留用于未来配置覆盖)

仅在 API 调用时存在的层

这些内容不会被持久化为缓存系统提示的一部分:

  • ephemeral_system_prompt
  • 预填充消息
  • 网关生成的会话上下文叠加
  • 当前轮次用户消息中注入的后期 Honcho 回忆

这种分离确保了稳定前缀的稳定性,便于缓存。

内存快照

本地内存和用户配置文件数据在会话开始时以冻结快照形式注入。会话中段的写入会更新磁盘状态,但不会修改已构建的系统提示,直到新会话或强制重建发生。

上下文文件

agent/prompt_builder.py 使用优先级系统扫描并清理项目上下文文件——仅加载一种类型(首个匹配项胜出):

  1. .hermes.md / HERMES.md(遍历至 Git 根目录)
  2. AGENTS.md(启动时在 CWD;后续通过 agent/subdirectory_hints.py 在会话中逐步发现子目录)
  3. CLAUDE.md(仅在 CWD)
  4. .cursorrules / .cursor/rules/*.mdc(仅在 CWD)

SOUL.md 通过 load_soul_md() 单独加载至身份槽位。加载成功后,build_context_files_prompt(skip_soul=True) 会阻止其重复出现。

长文件在注入前会被截断。

技能索引

当具备技能工具支持时,技能系统会向提示中加入一个紧凑的技能索引。

为何采用此提示组装方式

该架构有意优化以实现:

  • 保留提供商端的提示词缓存
  • 避免不必要的历史记录变更
  • 保持内存语义清晰可理解
  • 允许网关/ACP/CLI 添加上下文,而不污染持久化的提示状态

相关文档