Files
myWiki/concepts/prefix-matching.md

1.3 KiB
Raw Blame History

title, created, updated, type, tags, sources
title created updated type tags sources
Prefix Matching前缀匹配 2026-05-11 2026-05-11 concept
cache
prompt-engineering
llm-api
prompt-caching-architecture

Prefix Matching前缀匹配

定义

Prefix Matching 是 prompt-caching 的底层实现机制。在 LLM API 层面,任何一次调用请求本质上是一个 Token 序列 T = [t₁, t₂, ..., tₙ]。当请求中植入 cache_control 断点时,系统将前缀 T[0:k] 进行哈希持久化。后续请求若前缀的字节流完全一致,系统直接跳过对前缀的重新推理。

核心特性

  • 确定性与严格性:匹配是字节级别的,任何差异(包括空白字符、动态变量)都会导致匹配失败
  • 雪崩效应:一个动态变量插入前缀 → cache-invalidation
  • 隐含约束:要求架构师将"静态"与"动态"严格分离到不同层

工程启示

  • System Prompt 中禁止包含动态变量(日期、状态摘要等)
  • 工具定义必须保持静态,通过 stub-pattern 规避变更
  • 前缀的"不可变性"是设计第一原则

相关概念