测试 Anthropic Prompt Caching 功能的命中率和效果。
这个测试用例会:
export OPENROUTER_API_KEY="your-api-key"
cd examples/cache-test
python3 run.py
测试完成后会显示:
# 查看 trace 数据
cd .trace/<trace-id>
# 分析缓存命中情况
python3 << 'EOF'
import json
print("seq | role | cache_creation | cache_read | hit_rate")
print("-" * 70)
with open("events.jsonl") as f:
for line in f:
event = json.loads(line)
if event.get("event") == "message_added":
msg = event["message"]
if msg.get("role") == "assistant":
seq = msg["sequence"]
role = msg["role"]
creation = msg.get("cache_creation_tokens", 0)
read = msg.get("cache_read_tokens", 0)
prompt = msg.get("prompt_tokens", 1)
rate = f"{read/prompt*100:.1f}%" if prompt > 0 else "0%"
print(f"{seq:3d} | {role:9s} | {creation:14,d} | {read:10,d} | {rate:>8s}")
EOF
当前策略: