跳转到内容

提示缓存

工作原理

缓存从消息数组的开头工作。当请求到达时,系统会检查开头的多少条消息与之前的请求完全匹配 — 匹配的部分就是"前缀",将从缓存中提供服务:

  1. 第一次请求 — 完整提示词被处理并缓存
  2. 后续请求 — 如果提示词前缀匹配,则缓存部分会被重用(缓存命中
  3. 计费 — 缓存令牌以较低费率计费

WARNING

提示缓存并非100%保证。缓存条目可能因内存压力而被清除,请求也可能被路由到不同的服务器。使用 x-grok-conv-id 来最大化缓存命中率。

示例

请求 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条消息与请求1完全匹配,因此从缓存中提供服务。只有新消息需要计算。

下一步

本文档为 docs.x.ai 全站中文翻译,由 AI 自动翻译生成。代码示例请以原文为准。