""" 调试脚本:检查缓存失效的原因 分析trace中的消息,看看为什么缓存点没有生效 """ import json from pathlib import Path import hashlib trace_dir = Path("/Users/elksmmx/Desktop/agent 2.10/Agent/examples/find knowledge/.trace/7ff963bc-3106-47fc-9725-b712f3e0d7d5/messages") print("=== 分析缓存失效原因 ===\n") # 读取前几次assistant消息,看看它们发送的消息列表是否一致 assistant_calls = [] for i in range(1, 101): fname = trace_dir / f"7ff963bc-3106-47fc-9725-b712f3e0d7d5-{i:04d}.json" if not fname.exists(): continue with open(fname) as f: data = json.load(f) if data.get('role') == 'assistant': assistant_calls.append({ 'seq': i, 'cache_read': data.get('cache_read_tokens', 0), 'cache_create': data.get('cache_creation_tokens', 0), }) print("=== Assistant消息的缓存情况 ===") for call in assistant_calls[:20]: # 只看前20次 seq = call['seq'] cache_read = call['cache_read'] cache_create = call['cache_create'] if cache_create > 0: print(f"seq={seq:03d} 🆕 创建缓存 {cache_create}") elif cache_read > 0: print(f"seq={seq:03d} ✅ 命中缓存 {cache_read}") else: print(f"seq={seq:03d} ❌ 缓存失效") print("\n=== 关键发现 ===") print(f"总共{len(assistant_calls)}次assistant调用") print(f"创建缓存: {sum(1 for c in assistant_calls if c['cache_create'] > 0)}次") print(f"命中缓存: {sum(1 for c in assistant_calls if c['cache_read'] > 0)}次") print(f"缓存失效: {sum(1 for c in assistant_calls if c['cache_read'] == 0 and c['cache_create'] == 0)}次") # 分析缓存创建的token数 cache_creates = [c['cache_create'] for c in assistant_calls if c['cache_create'] > 0] if cache_creates: print(f"\n缓存创建的token数: {cache_creates}") print(f"所有缓存大小都是 {cache_creates[0]} tokens,说明只缓存了system prompt") # 分析缓存命中的token数 cache_reads = [c['cache_read'] for c in assistant_calls if c['cache_read'] > 0] if cache_reads: unique_reads = set(cache_reads) print(f"\n缓存命中的token数: {unique_reads}") if len(unique_reads) == 1: print(f"所有缓存命中都是 {list(unique_reads)[0]} tokens,说明只命中了system prompt") print("\n=== 结论 ===") print("虽然我们在第20、40、60条消息上设置了缓存点,") print("但Anthropic API只创建了system prompt的缓存。") print("这说明后续消息的缓存点没有生效。") print("\n可能的原因:") print("1. 消息内容在每次调用时发生了变化") print("2. 消息格式转换(str->list)导致Anthropic认为是不同的消息") print("3. Anthropic的缓存机制有其他限制(如最小缓存长度)")