sidebar position: 5 title: "提示词组装" description: "Hermes 如何构建系统提示词、保持缓存稳定性和注入临时层" Hermes 刻意将以下两者分离: - 缓存的系统提示词状态 - 临时的 API 调用时添加内容 这是项目中最关键的设计决策之一,因为它影响:

> 📖 本文档翻译自 Prompt Assembly > 最后更新:2026-04-16


sidebar_position: 5 title: "提示词组装" description: "Hermes 如何构建系统提示词、保持缓存稳定性和注入临时层"

提示词组装

Hermes 刻意将以下两者分离:

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

这是项目中最关键的设计决策之一,因为它影响:

  • Token(令牌)用量
  • 提示词缓存效果
  • 会话连续性
  • 内存正确性

主要文件:

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

缓存系统提示词层

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

  1. Agent 身份——来自 HERMES_HOMESOUL.md(可用时),否则回退到 prompt_builder.py 中的 DEFAULT_AGENT_IDENTITY
  2. 工具感知行为指导
  3. Honcho 静态块(激活时)
  4. 可选的系统消息
  5. 冻结的 MEMORY 快照
  6. 冻结的 USER 配置快照
  7. Skills(技能)索引
  8. 上下文文件(AGENTS.md.cursorrules.cursor/rules/*.mdc)——当 SOUL.md 已在步骤 1 中作为身份加载时,不会再次包含在此处
  9. 时间戳 / 可选会话 ID
  10. 平台提示

当设置 skip_context_files 时(例如子 Agent 委托),SOUL.md 不会被加载,而是使用硬编码的 DEFAULT_AGENT_IDENTITY

具体示例:组装后的系统提示词

以下是所有层都存在时最终系统提示词的简化视图(注释显示每部分的来源):

# 第 1 层:Agent 身份(来自 ~/.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.
...

# 第 2 层:工具感知行为指导
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.

# 工具使用强制(仅限 GPT/Codex 模型)
You MUST use your tools to take action — do not describe what you
would do or plan to do without actually doing it.
...

# 第 3 层:Honcho 静态块(激活时)
[Honcho personality/context data]

# 第 4 层:可选系统消息(来自配置或 API)
[用户配置的系统消息覆盖]

# 第 5 层:冻结的 MEMORY 快照
## Persistent Memory
- User prefers Python 3.12, uses pyproject.toml
- Default editor is nvim
- Working on project "atlas" in ~/code/atlas
- Timezone: US/Pacific

# 第 6 层:冻结的 USER 配置快照
## User Profile
- Name: Alice
- GitHub: alice-dev

# 第 7 层:技能索引
## 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>

# 第 8 层:上下文文件(来自项目目录)
# 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.

# 第 9 层:时间戳 + 会话
Current time: 2026-03-30T14:30:00-07:00
Session: abc123

# 第 10 层:平台提示
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,作为 Agent 的身份——系统提示词的最开头部分。prompt_builder.py 中的加载逻辑如下:

# 来自 agent/prompt_builder.py(简化版)
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")  # 安全扫描
    content = _truncate_content(content, "SOUL.md")       # 限制为 2 万字符
    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() 使用优先级系统——只加载一种项目上下文类型(首次匹配优先):

# 来自 agent/prompt_builder.py(简化版)
def build_context_files_prompt(cwd=None, skip_soul=False):
    cwd_path = Path(cwd).resolve()

    # 优先级:首次匹配优先 — 只加载一个项目上下文
    project_context = (
        _load_hermes_md(cwd_path)       # 1. .hermes.md / HERMES.md(遍历到 git 根目录)
        or _load_agents_md(cwd_path)    # 2. AGENTS.md(仅当前目录)
        or _load_claude_md(cwd_path)    # 3. CLAUDE.md(仅当前目录)
        or _load_cursorrules(cwd_path)  # 4. .cursorrules / .cursor/rules/*.mdc
    )

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

    # 来自 HERMES_HOME 的 SOUL.md(独立于项目上下文)
    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仅当前目录通用 Agent 指令文件
3CLAUDE.md仅当前目录Claude Code 兼容性
4.cursorrules.cursor/rules/*.mdc仅当前目录Cursor 兼容性

所有上下文文件都经过:

  • 安全扫描——检查提示词注入模式(不可见 unicode、"ignore previous instructions"、凭据外泄尝试)
  • 截断——使用 70/20 头尾比例限制为 20,000 字符,并添加截断标记
  • YAML frontmatter 剥离——.hermes.md 的 frontmatter 被移除(保留供未来配置覆盖使用)

仅 API 调用时的层

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

  • ephemeral_system_prompt
  • prefill 消息
  • Gateway 派生的会话上下文覆盖
  • 后续轮次的 Honcho recall 注入到当前轮次的用户消息中

这种分离保持稳定前缀对缓存的稳定性。

内存快照

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

上下文文件

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

  1. .hermes.md / HERMES.md(遍历到 git 根目录)
  2. AGENTS.md(启动时当前目录;子目录在会话期间通过 agent/subdirectory_hints.py 渐进发现)
  3. CLAUDE.md(仅当前目录)
  4. .cursorrules / .cursor/rules/*.mdc(仅当前目录)

SOUL.md 通过 load_soul_md() 单独加载到身份插槽。成功加载后,build_context_files_prompt(skip_soul=True) 防止它出现两次。

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

技能索引

当技能工具可用时,技能系统向提示词贡献一个紧凑的技能索引。

为什么提示词组装这样拆分

这种架构有意优化以:

  • 保持 Provider 端的提示词缓存
  • 避免不必要地修改历史
  • 保持内存语义可理解
  • 让 Gateway/ACP/CLI 添加上下文而不污染持久化提示词状态

相关文档

Continue Exploring

继续探索

这不是课程式的上一篇下一篇,而是从当前节点向外继续漫游。

开发者指南

上下文压缩与缓存

Hermes Agent 使用双重压缩系统和 Anthropic 提示词缓存,在长对话中高效管理上下文窗口的使用。 源文件:agent/context engine.py(ABC)、agent/context compressor.py(默认引擎)、agent/prompt caching.py、gateway/run

开发者指南

会话存储

Hermes Agent 使用 SQLite 数据库( /.hermes/state.db)来持久化会话元数据、完整消息历史和模型配置,覆盖 CLI 和网关会话。这取代了早期基于每个会话 JSONL 文件的方式。 源文件:hermes state.py 关键设计决策: - WAL 模式 用于并发读取 + 单个写入器(网

开发者指南

网关内部机制

sidebar position: 7 title: "网关内部机制" description: "消息网关如何启动、授权用户、路由会话和投递消息" 消息网关是一个长运行进程,通过统一架构将 Hermes 连接到 14+ 个外部消息平台。 当消息从任何平台到达时: 1. 平台适配器 接收原始事件,将其规范化为 Mess

开发者指南

架构

本页面是 Hermes Agent 内部结构的顶层地图. 使用它来了解代码库的整体结构,然后深入子系统文档获取实现细节。 如果你是首次接触此代码库: 1. 本页面 — 了解整体结构 2. Agent 循环内部机制 — AIAgent 如何工作 3. 提示词组装 — 系统提示词构建

开发者指南

贡献指南

感谢你对 Hermes Agent 的贡献!本指南涵盖开发环境设置、理解代码库以及如何让你的 PR 被合并。 我们按以下顺序重视贡献: 1. Bug 修复 — 崩溃、不正确行为、数据丢失 2. 跨平台兼容性 — macOS、不同 Linux 发行版、WSL2 3. 安全加固 — Shell 注入、Prompt 注入、路

开发者指南

Agent 循环内部机制

sidebar position: 3 title: "Agent 循环内部机制" description: "AIAgent 执行、API 模式、工具、回调和回退行为的详细解析" 核心编排引擎是 run agent.py 中的 AIAgent 类——大约 10,700 行代码,负责从 Prompt(提示词)组装到工具

Developer Guide

开发者指南

面向二次开发者,解释架构、运行时、上下文引擎、插件、工具与扩展机制。

20 篇文档20 个节点

当前节点

提示词组装

同主题继续探索

架构

本页面是 Hermes Agent 内部结构的顶层地图. 使用它来了解代码库的整体结构,然后深入子系统文档获取实现细节。 如果你是首次接触此代码库: 1. 本页面 — 了解整体结构 2. Agent 循环内部机制 — AIAgent 如何工作 3. 提示词组装 — 系统提示词构建

贡献指南

感谢你对 Hermes Agent 的贡献!本指南涵盖开发环境设置、理解代码库以及如何让你的 PR 被合并。 我们按以下顺序重视贡献: 1. Bug 修复 — 崩溃、不正确行为、数据丢失 2. 跨平台兼容性 — macOS、不同 Linux 发行版、WSL2 3. 安全加固 — Shell 注入、Prompt 注入、路

Agent 循环内部机制

sidebar position: 3 title: "Agent 循环内部机制" description: "AIAgent 执行、API 模式、工具、回调和回退行为的详细解析" 核心编排引擎是 run agent.py 中的 AIAgent 类——大约 10,700 行代码,负责从 Prompt(提示词)组装到工具

上下文压缩与缓存

Hermes Agent 使用双重压缩系统和 Anthropic 提示词缓存,在长对话中高效管理上下文窗口的使用。 源文件:agent/context engine.py(ABC)、agent/context compressor.py(默认引擎)、agent/prompt caching.py、gateway/run

网关内部机制

sidebar position: 7 title: "网关内部机制" description: "消息网关如何启动、授权用户、路由会话和投递消息" 消息网关是一个长运行进程,通过统一架构将 Hermes 连接到 14+ 个外部消息平台。 当消息从任何平台到达时: 1. 平台适配器 接收原始事件,将其规范化为 Mess

会话存储

Hermes Agent 使用 SQLite 数据库( /.hermes/state.db)来持久化会话元数据、完整消息历史和模型配置,覆盖 CLI 和网关会话。这取代了早期基于每个会话 JSONL 文件的方式。 源文件:hermes state.py 关键设计决策: - WAL 模式 用于并发读取 + 单个写入器(网

相关节点

上下文压缩与缓存

Hermes Agent 使用双重压缩系统和 Anthropic 提示词缓存,在长对话中高效管理上下文窗口的使用。 源文件:agent/context engine.py(ABC)、agent/context compressor.py(默认引擎)、agent/prompt caching.py、gateway/run

会话存储

Hermes Agent 使用 SQLite 数据库( /.hermes/state.db)来持久化会话元数据、完整消息历史和模型配置,覆盖 CLI 和网关会话。这取代了早期基于每个会话 JSONL 文件的方式。 源文件:hermes state.py 关键设计决策: - WAL 模式 用于并发读取 + 单个写入器(网

网关内部机制

sidebar position: 7 title: "网关内部机制" description: "消息网关如何启动、授权用户、路由会话和投递消息" 消息网关是一个长运行进程,通过统一架构将 Hermes 连接到 14+ 个外部消息平台。 当消息从任何平台到达时: 1. 平台适配器 接收原始事件,将其规范化为 Mess

架构

本页面是 Hermes Agent 内部结构的顶层地图. 使用它来了解代码库的整体结构,然后深入子系统文档获取实现细节。 如果你是首次接触此代码库: 1. 本页面 — 了解整体结构 2. Agent 循环内部机制 — AIAgent 如何工作 3. 提示词组装 — 系统提示词构建

贡献指南

感谢你对 Hermes Agent 的贡献!本指南涵盖开发环境设置、理解代码库以及如何让你的 PR 被合并。 我们按以下顺序重视贡献: 1. Bug 修复 — 崩溃、不正确行为、数据丢失 2. 跨平台兼容性 — macOS、不同 Linux 发行版、WSL2 3. 安全加固 — Shell 注入、Prompt 注入、路

Agent 循环内部机制

sidebar position: 3 title: "Agent 循环内部机制" description: "AIAgent 执行、API 模式、工具、回调和回退行为的详细解析" 核心编排引擎是 run agent.py 中的 AIAgent 类——大约 10,700 行代码,负责从 Prompt(提示词)组装到工具