Context
“Context(上下文)”指的是 Moltbot 在一次 run 中发送给模型的全部内容。它受模型的 context window(token 上限)约束。
初学者心智模型:
- System prompt(由 Moltbot 构建):规则、工具、skills 列表、时间/运行时信息,以及注入的 workspace files。
- 对话历史:本 session 中你发的消息 + assistant 的消息。
- 工具调用/结果 + 附件:命令输出、文件读取、图片/音频等。
Context 不等同于 “memory”:memory 可以写入磁盘并在之后重新加载;context 是模型当前窗口内的内容。
快速开始(检查 context)
/status→ 快速查看“窗口用了多少?”以及 session 设置。/context list→ 注入了什么 + 粗略大小(按文件与总量)。/context detail→ 更深入的拆解:按文件、按 tool schema 大小、按 skill entry 大小,以及 system prompt 大小。/usage tokens→ 在正常回复末尾追加每条回复的用量页脚。/compact→ 将更旧历史总结为紧凑条目以释放窗口空间。
另见:Slash commands、Token use & costs、Compaction。
示例输出
实际数值会随模型、provider、tool policy 与 workspace 内容变化。
/context list
🧠 Context breakdown
Workspace: <workspaceDir>
Bootstrap max/file: 20,000 chars
Sandbox: mode=non-main sandboxed=false
System prompt (run): 38,412 chars (~9,603 tok) (Project Context 23,901 chars (~5,976 tok))
Injected workspace files:
- AGENTS.md: OK | raw 1,742 chars (~436 tok) | injected 1,742 chars (~436 tok)
- SOUL.md: OK | raw 912 chars (~228 tok) | injected 912 chars (~228 tok)
- TOOLS.md: TRUNCATED | raw 54,210 chars (~13,553 tok) | injected 20,962 chars (~5,241 tok)
- IDENTITY.md: OK | raw 211 chars (~53 tok) | injected 211 chars (~53 tok)
- USER.md: OK | raw 388 chars (~97 tok) | injected 388 chars (~97 tok)
- HEARTBEAT.md: MISSING | raw 0 | injected 0
- BOOTSTRAP.md: OK | raw 0 chars (~0 tok) | injected 0 chars (~0 tok)
Skills list (system prompt text): 2,184 chars (~546 tok) (12 skills)
Tools: read, edit, write, exec, process, browser, message, sessions_send, …
Tool list (system prompt text): 1,032 chars (~258 tok)
Tool schemas (JSON): 31,988 chars (~7,997 tok) (counts toward context; not shown as text)
Tools: (same as above)
Session tokens (cached): 14,250 total / ctx=32,000/context detail
🧠 Context breakdown (detailed)
…
Top skills (prompt entry size):
- frontend-design: 412 chars (~103 tok)
- oracle: 401 chars (~101 tok)
… (+10 more skills)
Top tools (schema size):
- browser: 9,812 chars (~2,453 tok)
- exec: 6,240 chars (~1,560 tok)
… (+N more tools)什么会计入 context window
模型收到的一切都会计入,包括:
- System prompt(全部分区)。
- 对话历史。
- Tool calls + tool results。
- Attachments/transcripts(images/audio/files)。
- compaction summaries 与 pruning 产生的内容。
- Provider 的“包装层”或隐藏 headers(不可见但仍计入)。
Moltbot 如何构建 system prompt
system prompt 由 Moltbot 持有,并在每次 run 重新构建。它包含:
- 工具列表 + 简短描述。
- skills 列表(仅元数据;见下文)。
- workspace 位置。
- 时间(UTC + 若配置则包含转换后的用户本地时间)。
- 运行时元数据(host/OS/model/thinking)。
- 在 Project Context 下注入的 workspace bootstrap files。
完整拆解参见:System Prompt。
注入的 workspace files(Project Context)
默认情况下,Moltbot 会注入一组固定的 workspace files(若存在):
AGENTS.mdSOUL.mdTOOLS.mdIDENTITY.mdUSER.mdHEARTBEAT.mdBOOTSTRAP.md(仅首次运行)
大文件会按单文件上限截断,配置项为 agents.defaults.bootstrapMaxChars(默认 20000 chars)。/context 会展示 raw vs injected 的大小以及是否发生截断。
Skills:注入了什么 vs 何时按需加载
system prompt 会包含一个紧凑的 skills list(name + description + location)。这份列表有真实的 context 开销。
默认不会包含 skill 的具体指令内容。模型应当只在需要时 read 对应 skill 的 SKILL.md。
Tools:两类成本
Tools 会通过两种方式影响 context:
- system prompt 里的 tool list 文本(你看到的 “Tooling”)。
- tool schemas(JSON)。它们会被发送给模型以便调用工具;即使你看不到明文文本,它们仍会计入 context。
/context detail 会拆解最大的 tool schemas,便于定位主要开销来源。
Commands、directives 与“内联快捷方式”
Slash commands 由 Gateway 处理,存在几种不同的行为:
- Standalone commands:一条只包含
/...的消息会作为命令执行。 - Directives:
/think、/verbose、/reasoning、/elevated、/model、/queue会在模型看到消息之前被剥离。- 只有 directive 的消息会持久化 session 设置。
- 普通消息中的内联 directives 会作为“逐条消息的提示(hint)”。
- Inline shortcuts(仅 allowlisted senders):普通消息中的某些
/...token 可以立即执行(例如 “hey /status”),并在模型看到剩余文本之前被剥离。
细节参见:Slash commands。
Sessions、compaction 与 pruning(哪些会持久)
跨消息持久化的内容取决于机制:
- 普通历史会在 session transcript 中持久化,直到被策略触发 compaction/pruning。
- Compaction 会把 summary 写入 transcript,并保留近期消息。
- Pruning 会在一次 run 的 内存 prompt 中移除旧的 tool results,但不会重写 transcript。
文档: Session、Compaction、Session pruning。
/context 实际在报告什么
当可用时,/context 会优先使用最近一次 run-built 的 system prompt report:
System prompt (run):从最近一次嵌入式(可用工具)run 捕获并持久化到 session store。System prompt (estimate):当不存在 run report(或通过不生成 report 的 CLI backend 运行)时现场估算。
无论哪种,它都会报告大小与主要贡献者,但不会把完整 system prompt 或 tool schemas 原样打印出来。