20260706:新增一些文章

This commit is contained in:
2026-07-06 10:14:02 +08:00
parent 6021dea160
commit 24b006225b
194 changed files with 8512 additions and 91 deletions

View File

@@ -0,0 +1,47 @@
---
title: "Prefix Matching Invariant前缀匹配不变性"
created: 2026-06-29
updated: 2026-06-29
type: concept
tags: [prompt-caching, optimization, constraint]
sources: [[prompt-to-loop-engineering-2026]]
confidence: high
---
# Prefix Matching Invariant
> [[prompt-caching|Prompt Caching]] 的铁律:缓存按字节从头进行哈希校验,前缀中任何微小更改都会导致该位置往后的所有缓存集体失效。
## 机制
1. 模型缓存已计算完毕的上下文前缀KV Cache
2. 下次请求前缀完全一致 → 命中缓存,跳过 Prefill 阶段
3. 成本降低约 90%,延迟降低最高 85%Anthropic, 2024
## "从静到动"分层排列
为最大化缓存命中率,上下文必须严格按以下顺序排列:
```
静态 ──────────────────────────────────────→ 动态
[工具定义] → [系统提示] → [历史对话] → [动态消息]
```
## 反直觉设计要求
**动态全局变量不能插在开头的系统提示中**
-`System: "今天是 2026-06-29。你是..."` → 日期变化击穿整段缓存
- ✅ 将日期作为普通消息挂在对话流**最末尾**,保证前缀稳定性
## 缓存经济学
参考阿里云百炼计费规则:首次创建缓存按输入 100% 计费,后续命中仅需 20%。
**N > 3 即可产生净收益**(同一前缀在被清理前复用第 2 次开始省钱)。高频迭代 Agent 的极低边际成本即源于此。
## 相关概念
- [[prompt-caching|Prompt Caching]]
- [[context-engineering|Context Engineering]]
- [[prompt-layering|提示分层]]