Python

Prompt Cache 片段:命中率不够就别指望折扣

只打印缓存相关字段。折扣只发生在 cached_tokens(cache read)。前缀太短、有改动、第一次写入、或命中率太低,都不要按缓存单价估算。

本页不发起请求;不收 Key;中转 curl 不在本站。

← 计费示例 · 计费路径 · 官方 usage 文档

输入input_tokens_details.cache_write_tokens
输出
缓存input_tokens_details.cached_tokens

hit_rate · 门槛:新模型约 1024 tokens 前缀,旧模型约 2048。低于门槛、前缀有任何改动、或首次请求,折扣都不成立。

Python
import os
from openai import OpenAI

# Prefix must be byte-identical across calls. Short prefixes never hit.
# Newer models ~1024 tokens minimum; older ~2048. First call is a write, not a discount.
STABLE_PREFIX = ("You are a billing ledger. Keep answers short. ") * 160

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

response = client.responses.create(
    model="gpt-4o-mini",
    input=[
        {"role": "system", "content": STABLE_PREFIX},
        {"role": "user", "content": "Reply with one word: ok"},
    ],
)

usage = response.usage
input_tokens = usage.input_tokens if usage else 0            # 输入
output_tokens = usage.output_tokens if usage else 0          # 输出
details = usage.input_tokens_details if usage else None
cached = (details.cached_tokens or 0) if details else 0      # 缓存(read)
write = (getattr(details, "cache_write_tokens", 0) or 0) if details else 0
hit_rate = (cached / input_tokens) if input_tokens else 0

print("输入", input_tokens)
print("输出", output_tokens)
print("缓存", cached)
print("cache_write", write)
print("hit_rate", round(hit_rate, 4))

# Discount does NOT apply when:
# - prefix shorter than the cacheable minimum
# - prefix bytes change (whitespace counts)
# - this is the first request (write, not read)
# - hit_rate is too low to beat uncached input $/M
这段打印对上哪一列 · Python · 输入 / 输出 / 缓存
字段对上列说明
input_tokens_details.cached_tokens缓存唯一按缓存读 $/M 的字段。命中率 = cached / 输入。
input_tokens_details.cache_write_tokens输入写入不是折扣。接近输入价,有的模型还略贵。不要记进缓存列。
hit_rate门槛:新模型约 1024 tokens 前缀,旧模型约 2048。低于门槛、前缀有任何改动、或首次请求,折扣都不成立。

其它片段

语言片段对上列打开
PythonChat Completions 的 usage 怎么对上输入 / 输出 / 缓存输入 / 输出 / 缓存打开
JavaScriptResponses API 的 usage 怎么对上输入 / 输出列输入 / 输出 / 缓存打开

本页拒绝 zip 安装包与 API Key 表单。片段里只有 process.env.OPENAI_API_KEY / os.environ["OPENAI_API_KEY"];中转 curl、测速和请求构造器不在本站。

Prompt Cache 片段:命中率不够就别指望折扣 · OpenAICN