Skip to content

提示词缓存

NordRouter 在供应商支持时使用提示词缓存。重复的前缀可自动缓存,也可用 cache_control 明确标记;usage 会显示缓存读取和写入的 token。

  • 保持系统提示词和长上下文不变,只修改最后一条用户消息。
  • 自动缓存可在控制面板管理;需要精确控制时使用手动标记。
  • 缓存只降低重复输入的成本,不改变输出 token 的计费。

完整示例

原始指南中的可执行示例完整保留如下。只运行适用于你的工具和操作系统的代码块。

json
"usage": {
  "prompt_tokens": 21364,
  "prompt_tokens_details": {
    "cached_tokens": 21361     // ← read from cache at ×0.1
  },
  "completion_tokens": 40
}
bash
curl https://nordrouter.com/v1/messages \
  -H "x-api-key: sk-nr-YOUR-KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "anthropic/claude-fable-5",
    "max_tokens": 512,
    "system": [
      {
        "type": "text",
        "text": "You are an assistant for our documentation. Here is the reference: <...lots of text...>",
        "cache_control": { "type": "ephemeral" }
      }
    ],
    "messages": [{ "role": "user", "content": "First question about the reference?" }]
  }'
python
from anthropic import Anthropic

client = Anthropic(base_url="https://nordrouter.com", api_key="sk-nr-YOUR-KEY")

resp = client.messages.create(
    model="anthropic/claude-fable-5",
    max_tokens=512,
    system=[{
        "type": "text",
        "text": "You are an assistant for our documentation. Here is the reference: <...>",
        "cache_control": {"type": "ephemeral"},   # cache the reference
    }],
    messages=[{"role": "user", "content": "First question about the reference?"}],
)
print(resp.usage)  # cache_creation_input_tokens on the 1st request, cache_read on the next

官方资料与相关链接

OperationMultiplierWhen it's charged
Write (cache-write)×1.25the first time the context enters the cache
Read (cache-read)×0.1 (−90%)every repeat of the same context
No cacheWith cache
What's billed10 × 30k input1 write + 9 reads
Input cost~$0.46~$0.10

相关指南

先用最小文本请求验证连接,再添加流式输出、工具调用、图片等高级功能。不要把 API key 放入公开代码或浏览器前端。

故障排除