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 金鑰寫入公開程式碼或瀏覽器前端。

疑難排解