33 lines
1.3 KiB
Markdown
33 lines
1.3 KiB
Markdown
---
|
||
title: "Prefix Matching(前缀匹配)"
|
||
created: 2026-05-11
|
||
updated: 2026-05-11
|
||
type: concept
|
||
tags: [cache, prompt-engineering, llm-api]
|
||
sources: [[prompt-caching-architecture]]
|
||
---
|
||
|
||
# Prefix Matching(前缀匹配)
|
||
|
||
## 定义
|
||
|
||
Prefix Matching 是 [[prompt-caching|Prompt Caching]] 的底层实现机制。在 LLM API 层面,任何一次调用请求本质上是一个 Token 序列 T = [t₁, t₂, ..., tₙ]。当请求中植入 `cache_control` 断点时,系统将前缀 T[0:k] 进行哈希持久化。后续请求若前缀的字节流完全一致,系统直接跳过对前缀的重新推理。
|
||
|
||
## 核心特性
|
||
|
||
- **确定性与严格性**:匹配是字节级别的,任何差异(包括空白字符、动态变量)都会导致匹配失败
|
||
- **雪崩效应**:一个动态变量插入前缀 → [[cache-invalidation|整个缓存树失效]]
|
||
- **隐含约束**:要求架构师将"静态"与"动态"严格分离到不同层
|
||
|
||
## 工程启示
|
||
|
||
- System Prompt 中**禁止**包含动态变量(日期、状态摘要等)
|
||
- 工具定义必须保持静态,通过 [[stub-pattern|Stub 模式]] 规避变更
|
||
- 前缀的"不可变性"是设计第一原则
|
||
|
||
## 相关概念
|
||
|
||
- [[prompt-caching|Prompt Caching]]
|
||
- [[cache-invalidation|缓存失效]]
|
||
- [[prompt-layering|提示分层]]
|