Prompt Caching是将重复使用的Prompt前缀部分的KV Cache缓存起来,后续请求直接复用的技术,能降低50-90%的首Token延迟和减少Token费用。
| 指标 | 无缓存 | 有缓存 |
|---|---|---|
| 首Token延迟 | 2.5s | 0.3s |
| Token费用 | $0.03/次 | $0.003/次 |
| 缓存命中率 | - | 85-95% |
# openclaw.config.yaml prompt_caching: enabled: true # 缓存策略 strategy: prefix # prefix | semantic # 缓存TTL ttl_seconds: 3600 # 1小时 # 最小缓存长度 min_tokens: 1024 # 至少1024 token才缓存 # 缓存存储 store: memory # memory | redis
// Anthropic风格的缓存标记
const messages = [
{
role: "system",
content: [
{
type: "text",
text: "你是一个专业的AI助手...", // 很长的系统提示
cache_control: { type: "ephemeral" } // 标记缓存点
}
]
},
{
role: "user",
content: "用户的具体问题"
}
];