Skip to content
开发指南

深度推理与 Prompt Caching ​

充分释放前沿多步推理模型(gpt-5、o3、Claude Extended Thinking、Gemini 2.5 Thinking、DeepSeek-R1)的复杂问题求解能力,同时借助 Prompt Caching 让长上下文 Agent 账单清晰可控。

调控推理深度 ​

bash
curl https://api.vanix.io/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $VANIX_API_KEY" \
  -d '{
    "model": "o3-mini",
    "reasoning_effort": "medium",
    "max_completion_tokens": 2048,
    "messages": [{"role": "user", "content": "Design a lock-free token bucket rate limiter in Go."}]
  }'
bash
curl https://api.vanix.io/v1/messages \
  -H "Content-Type: application/json" \
  -H "x-api-key: $VANIX_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "claude-sonnet-4-5",
    "max_tokens": 8000,
    "thinking": {"type": "enabled", "budget_tokens": 4096},
    "messages": [{"role": "user", "content": "Audit this SQL migration for zero-downtime lock hazards."}]
  }'

Prompt Caching ​

对于高频重复的静态前缀(如长篇系统指令、工具 Schema、代码库上下文或 RAG 规则文档),上游缓存可将输入 Token 成本降低 50%–90%,并大幅缩短首字延迟(TTFT)。

供应商缓存模式最小缓存前缀用量遥测字段
OpenAI相同前缀自动命中1,024 tokensusage.prompt_tokens_details.cached_tokens
Anthropic显式声明 cache_control: {"type": "ephemeral"}1,024–2,048 tokensusage.cache_creation_input_tokens 与 cache_read_input_tokens
Google Gemini隐式缓存 / Context Caching视模型而定usageMetadata.cachedContentTokenCount

Anthropic 显式缓存断点示例 ​

json
{
  "model": "claude-sonnet-4-5",
  "max_tokens": 1024,
  "system": [
    {
      "type": "text",
      "text": "<large static codebase rules and API specification exceeding 1,024 tokens>",
      "cache_control": { "type": "ephemeral" }
    }
  ],
  "messages": [
    { "role": "user", "content": "Review middleware/rate-limit.go for race conditions." }
  ]
}

提升缓存命中率的三条准则 ​

  1. 保持前缀逐字节一致:将时间戳、用户 ID 及动态检索的 RAG 片段放在提示词末尾,切勿插入静态 system 块内部。
  2. 固定工具定义顺序:对 tools[] 进行确定性排序,避免多轮对话间因 Schema 顺序变动导致缓存失效。
  3. 保持渠道亲和性:参考 渠道亲和与路由优化,确保同一 Agent 的连续多轮请求命中同一上游供应商的缓存节点。

Vanix AI 统一模型网关 · https://api.vanix.io