Prompt Caching

工作原理

Cache 从messages array 的起始位置开始工作。请求到达时,系统会检查开头有多少条 message 与之前的请求完全匹配;匹配的部分称为“prefix”,并直接从 cache 提供:

  1. 首次请求 — 完整 prompt 会被处理并缓存

  2. 后续请求 — 如果 prompt prefix 匹配,就会复用已缓存的部分(即 cache hit

  3. 计费 — Cached token 按较低费率计费

示例

请求 1:

Text

[system] "You are a helpful assistant."
[user] "What is the capital of France?"
[assistant] "The capital of France is Paris."

请求 2:

Text

[system] "You are a helpful assistant."       ← cached
[user] "What is the capital of France?"       ← cached
[assistant] "The capital of France is Paris." ← cached
[user] "What about Germany?"                  ← new

前 3 条 message 与请求 1 完全匹配,因此直接从 cache 提供;只有新增的 message 需要计算。

后续内容