Files
myWiki/concepts/prompt-caching.md

38 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "Prompt Caching"
created: 2026-05-11
updated: 2026-05-11
type: concept
tags: [agent-architecture, cache, prompt-engineering]
sources: [[prompt-caching-architecture]]
---
# Prompt Caching提示缓存
## 定义
Prompt Caching 是一种基于 [[prefix-matching|前缀匹配]] 的确定性计算优化技术。在 LLM API 调用中,通过对请求 Token 序列的前缀进行持久化哈希存储,使后续相同前缀的请求跳过重复推理,直接复用缓存结果。
## 核心机制
1. **前缀匹配**API 层将请求前 N 个 Token 哈希后持久化;后续请求前缀相同时命中缓存
2. **cache_control 断点**:显式标注缓存边界,控制哪些 Token 进入缓存
3. **雪崩式失效**:前缀的任何微小更改都会导致整个 [[cache-invalidation|缓存树失效]]
## 工程意义
- **延迟降低**:跳过前缀 Token 的重复推理,实现毫秒级响应
- **成本优化**:不重复计费缓存的 Token
- **推理一致性**:模型从"热启动"状态开始,保持对系统指令的稳定理解
## 架构约束
要求[[prompt-layering|提示分层]] 设计中静态前缀的不可变性,以及与 [[stub-pattern|Stub 模式]] 的配合使用。
## 相关概念
- [[prefix-matching|前缀匹配]]
- [[cache-invalidation|缓存失效]]
- [[cache-hit-ratio|缓存命中率]]
- [[prompt-layering|提示分层]]