In-history system-prompt updates
历史内系统提示词更新
Rewriting the leading prompt when plan guidance, tools, persona or model variables change invalidates the provider prefix from its first token. Later instruction messages can preserve the preceding conversation prefix, but their representation depends on model capabilities. The [surface-node decision](../architecture/2026-09-02-system-prompt-as-surface-node.md) makes their original positions durable.
English
Problem
Rewriting the leading prompt when plan guidance, tools, persona or model variables change invalidates the provider prefix from its first token. Later instruction messages can preserve the preceding conversation prefix, but their representation depends on model capabilities. The surface-node decision makes their original positions durable.
Decision
The cache-preserving choice remains: log instructions when they enter the conversation instead of routinely moving them to its beginning. Section-based updates supersede the original route-gated whole-prompt rule. They append changed source text and explicit notices on every route; the loop does not select the latest system message as the entire prompt or consolidate because native updates are unavailable. Compaction provides the explicit consolidation point, while unchanged series starts preserve retained instruction positions.
systemPromptUpdate = 'in-history' remains a declared model capability on resolved model information and prepared calls, recorded in request context. Catalogs validate the value; model names or protocol families do not imply support. The declaration describes native representation, not permission to admit section deltas. Adapters preserve all instructions using native messages, a combined system field or the pi-ai reminder fallback. The bound prepared call, rather than a preceding request-context snapshot, determines serialization.
Instruction and user commitment follows successful request middleware and preparation inside an open step. Cancelling either preparation phase leaves them uncommitted. New instructions precede newly admitted users. Retries do not rerun assembly, pre-step selection or user admission; after compaction, the next attempt rebuilds the instructions from the same captured sections. Resume retains committed instructions and appends changes, because a process restart alone need not invalidate provider caches.
Presentation and accounting
Chat shows a later nonempty system node as a collapsed update card and avoids repeating it in a same-step request header. Trajectory can represent it as a synthetic request-header fact. Header configuration and tools remain independent of instruction changes; comparing only real headers would miss an intermediate system update. If the loaded window contains no earlier system node, the node appears as an initial prompt. Human transcripts omit system messages.
Request-prompt inspection uses immutable prefix states ordered by surviving conversation positions, not simply event sequence or source citations. Head rewrites can be logged after later-position updates; replacements can remove system nodes. Empty nodes remain addressable without erasing other retained text. When a replacement endpoint predates the known window and its position is unknown, inspection withholds later prompt text until prepend replay resolves the missing prefix. Numeric event order cannot safely guess the position. Historical cards retain their own prefix state.
Token breakdown counts every surviving system message as system tokens. The compact accounting state keeps system nodes and a running total rather than per-message accounting entries. Provider cacheReadTokens remains the observable cache signal; neither additive admission nor native-support metadata guarantees a cache hit.
Alternatives considered
Route-gated whole-prompt replacement. The original design treated the latest system message as the complete prompt and rejected section deltas under that assumption. Canonical per-source records and explicit replacement or retirement notices make additions meaningful without that interpretation; adapter fallback preserves them on other routes. Its former series-start and incapable-route consolidation rules are therefore not current behavior.
A deployment switch instead of declared native capability. A flag could misrepresent what the selected model accepts. Capability remains model-owned even though it no longer gates section admission.
Always append, or consolidate on every series start or resume. Unlimited appends retain stale head text and consume context; compaction supplies consolidation. A local replacement can leave most of the prefix valid, and restart can retain a warm provider cache, so neither alone justifies moving instructions. Model swaps likewise need no special consolidation rule beyond the recorded route change.
Commit before pre-step and request preparation, or put updates after new users. Early commitment can admit cancelled content or miss a compaction that removes the instructions an update was compared against. Putting updates after users reverses the established instruction-before-input order. Capture can happen earlier, but comparison and commitment must follow any compaction and precede request derivation.
Use prior request context to decide the current representation. It describes a previous call and can be wrong for the first call, resumed sessions or route changes. The actual bound adapter owns current serialization.
Clear only the latest system node during consolidation. Older instructions would survive and could become effective again. Rebuilding clears retained additions before replacing the initial head, preserving intervening conversation.
Keep only scalar accounting totals. Replacement prices cannot identify which system nodes disappeared. Retaining only those nodes with shadow-price metadata supports classification without a per-message checkpoint.
Consequences
Appending an update can preserve the provider prefix, but every retained update consumes input tokens until consolidation or removal. Frequently changing facts remain better suited to runtime context. Adapters that flatten instructions can invalidate the leading prompt, and user-role reminders do not acquire system authority. Proxies that rewrite or reorder messages can also defeat the intended instruction order or cache reuse.
Verification
Loop and request-reconstruction tests cover route changes, resume, cancellation, fixed retry assemblies, rebuilding after compaction and unchanged prefixes. Client tests cover update cards, header deduplication, historical prefix inspection and incomplete-history withholding. Accounting tests cover retained system nodes, replacements and replay. TypeScript and Python SDK expected outputs include appended typed system events, while keyless recorded sessions pin instructions in conversation order. Real-API adapter coverage uses DEEPSEEK_IN_HISTORY_MODEL to check following an appended update and compare provider cache reuse with a rewritten leading prompt; it skips without that setting. These live-provider checks remain necessary because metadata cannot establish model behavior.
中文
问题
计划指导、工具、persona 或模型变量变化时重写开头提示词,会从第一个 token 起使提供方前缀失效。后续指令消息可以保留此前的对话前缀,但其表示取决于模型能力。表面节点决策让其原始位置持久保存。
决策
保留缓存的选择保持不变:在指令进入对话时记录它们,而非例行移到对话开头。按段更新取代了原先按路由限制的整份提示词规则。它在所有路由上追加变化的来源文本与显式通知;循环不会把最新系统消息视为整份提示词,也不会因为不支持原生更新而合并。压缩提供显式合并时机,而未变指令的新序列保留既有指令位置。
systemPromptUpdate = 'in-history' 仍是解析后模型信息和已准备调用声明的模型能力,并记录在请求上下文中。目录校验该值;模型名称或协议族不隐含支持。此声明描述原生表示,而非是否允许准入分段增量。适配器通过原生消息、合并系统字段或 pi-ai 提醒回退保留全部指令。实际绑定的已准备调用决定序列化,而非此前的请求上下文快照。
指令和用户提交发生在开放步骤内,成功完成请求中间件及准备之后。取消任一准备阶段都会使其保持未提交。新指令位于新准入用户之前。重试不重跑组装、步骤前选择或用户准入;压缩后,下一次尝试根据同一批已捕获段重建指令。恢复保留已提交指令并追加变化,因为进程重启本身不必使提供方缓存失效。
展示与计量
Chat 将后续非空系统节点显示为折叠的更新卡片,避免在同一步骤的请求头中重复。Trajectory 可将它表示为合成的请求头事实。请求头配置和工具独立于指令变化;只比较真实请求头会漏掉中间的系统更新。若已加载窗口没有更早的系统节点,该节点显示为初始提示词。面向人的 transcript(文本记录)省略系统消息。
请求提示词检查使用按存活对话位置排序的不可变前缀状态,而非仅依赖事件序号或来源引用。头节点重写可以在更靠后位置的更新之后才记入日志;替换可以移除系统节点。空节点保持可寻址,不会抹去其他保留文本。当替换端点早于已知窗口且其位置未知时,检查会隐藏后续提示词文本,直到向前补载重放解析缺失前缀。数字事件顺序无法安全猜测位置。历史卡片保留各自的前缀状态。
Token 构成将每条存活系统消息计入系统 token。紧凑计量状态保留系统节点和运行总量,而非逐消息计量条目。提供方的 cacheReadTokens 仍是可观察的缓存信号;增量准入和原生支持元数据都不保证缓存命中。
考虑过的替代方案
按路由限制的整份提示词替换。 原设计把最新系统消息视为完整提示词,并据此拒绝分段增量。规范的逐来源记录与显式替换或退役通知让新增内容无需该解释即可成立;适配器回退在其他路由保留这些内容。因此,原先的新序列和不支持路由合并规则并非当前行为。
部署开关取代原生能力声明。 开关可能错误描述选定模型能接受的内容。即使能力不再限制分段准入,它仍由模型负责声明。
始终追加,或在每次新序列或恢复时合并。 无限追加会保留陈旧头文本并占用上下文;压缩提供合并。局部替换可以保留大部分有效前缀,重启也可以保留热的提供方缓存,因此两者本身都不足以移动指令。模型切换同样无需在已记录路由变化之外增加特殊合并规则。
在步骤前处理与请求准备之前提交,或将更新放在新用户之后。 提前提交可能准入已取消内容,或漏掉移除了更新所比较指令的压缩。将更新放在用户之后会颠倒既有的指令先于输入顺序。捕获可以更早发生,但比较和提交必须晚于任何压缩并早于请求派生。
用此前请求上下文决定当前表示。 它描述前一次调用,对首次调用、恢复的 Session 或路由变化可能不适用。实际绑定的适配器负责当前序列化。
合并时只清除最新系统节点。 更早的指令仍会保留,并可能重新生效。重建先清除保留的新增内容,再替换初始头节点,保留中间对话。
只保留标量计量总量。 替换价格无法识别消失了哪些系统节点。仅保留这些节点并使用影子价格元数据,无需逐消息检查点即可分类。
影响
追加更新可能保留提供方前缀,但每项保留更新在合并或移除前都会消耗输入 token。频繁变化的事实仍更适合运行时上下文。将指令展平的适配器可能使开头提示词失效,而用户角色提醒不会获得系统权限。重写或重排消息的代理也可能破坏预期指令顺序或缓存复用。
验证
循环和请求重建测试覆盖路由变化、恢复、取消、固定重试组装、压缩后重建及不变前缀。客户端测试覆盖更新卡片、请求头去重、历史前缀检查及历史不完整时的隐藏。计量测试覆盖保留系统节点、替换和重放。TypeScript 与 Python SDK 的预期输出包含追加的类型化系统事件,无需密钥的已记录 Session 则固定对话顺序中的指令。真实 API 适配器覆盖使用 DEEPSEEK_IN_HISTORY_MODEL 检查模型遵从追加更新,并比较它与重写开头提示词的提供方缓存复用;未配置时跳过。这些真实提供方检查仍有必要,因为元数据不能证明模型行为。