Commit 77b1058
committed
fix(claude_agent_sdk): stop assigning query usage to the final call
ResultMessage usage is cumulative across the query, so attaching it to the
final anthropic.messages.create span misattributes root and subagent tokens
and produces incorrect prompt-cache costs.
The caller's include_partial_messages option now determines where exact
per-request completion usage comes from. Braintrust leaves this option
unchanged because enabling it would expose additional public stream events.
include_partial_messages=True
message_start -> request ID and per-request prompt/cache usage
message_delta -> final per-request completion usage
result -> query metadata only
include_partial_messages=False or omitted
assistant -> request ID and per-request prompt/cache usage
transcript -> final completion usage when an exact match exists
no match -> omit completion_tokens and tokens
result -> query metadata only
Passive hooks keep root and subagent transcript paths separate:
UserPromptSubmit --------> root transcript ------> [root, message ID]
SubagentStop(tool A) ----> transcript A ---------> [tool A, message ID]
SubagentStop(tool B) ----> transcript B ---------> [tool B, message ID]
The agent context and message ID form one lookup key, preventing root and
parallel subagent calls from sharing usage. Transcript recovery searches
backward for the newest valid usage row and retries briefly for write races:
assistant(id=msg_1) -----> save [context, msg_1] and span
query stream ends -------> read context transcript
<----- newest valid usage row
update span [context, msg_1]
missing row -------------> wait 25 ms and retry (maximum: 3 reads)
Result usage remains query metadata only, and aggregate token metrics stay off
the parent task to avoid double-counting. Caller options and yielded messages
remain unchanged.
Anthropic cache metrics now emit either TTL-specific or legacy cache-creation
fields, never both, and prompt totals use the same representation. Sanitized
subprocess cassettes cover partial, transcript, subagent, and fallback paths.
Resolves #5641 parent a2f8347 commit 77b1058
9 files changed
Lines changed: 3674 additions & 1075 deletions
File tree
- py/src/braintrust/integrations
- anthropic
- claude_agent_sdk
- cassettes
- 0.1.10
- latest
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
56 | | - | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
57 | 62 | | |
58 | 63 | | |
59 | 64 | | |
60 | 65 | | |
| 66 | + | |
| 67 | + | |
61 | 68 | | |
62 | 69 | | |
63 | 70 | | |
| |||
66 | 73 | | |
67 | 74 | | |
68 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
69 | 80 | | |
70 | 81 | | |
71 | 82 | | |
| |||
77 | 88 | | |
78 | 89 | | |
79 | 90 | | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
80 | 94 | | |
81 | 95 | | |
82 | 96 | | |
83 | 97 | | |
84 | 98 | | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
89 | 115 | | |
90 | | - | |
91 | | - | |
92 | | - | |
| 116 | + | |
93 | 117 | | |
94 | 118 | | |
95 | | - | |
| 119 | + | |
| 120 | + | |
96 | 121 | | |
97 | 122 | | |
98 | 123 | | |
| |||
Lines changed: 32 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
376 | 376 | | |
377 | 377 | | |
378 | 378 | | |
379 | | - | |
380 | 379 | | |
381 | 380 | | |
382 | 381 | | |
| |||
410 | 409 | | |
411 | 410 | | |
412 | 411 | | |
413 | | - | |
| 412 | + | |
414 | 413 | | |
415 | 414 | | |
416 | 415 | | |
| |||
442 | 441 | | |
443 | 442 | | |
444 | 443 | | |
445 | | - | |
| 444 | + | |
446 | 445 | | |
447 | 446 | | |
448 | 447 | | |
| |||
851 | 850 | | |
852 | 851 | | |
853 | 852 | | |
854 | | - | |
| 853 | + | |
855 | 854 | | |
856 | 855 | | |
857 | 856 | | |
| |||
933 | 932 | | |
934 | 933 | | |
935 | 934 | | |
936 | | - | |
| 935 | + | |
937 | 936 | | |
938 | 937 | | |
939 | 938 | | |
| |||
973 | 972 | | |
974 | 973 | | |
975 | 974 | | |
976 | | - | |
| 975 | + | |
977 | 976 | | |
978 | 977 | | |
979 | 978 | | |
| |||
1014 | 1013 | | |
1015 | 1014 | | |
1016 | 1015 | | |
1017 | | - | |
| 1016 | + | |
1018 | 1017 | | |
1019 | 1018 | | |
1020 | 1019 | | |
| |||
1119 | 1118 | | |
1120 | 1119 | | |
1121 | 1120 | | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
1122 | 1145 | | |
1123 | 1146 | | |
1124 | 1147 | | |
| |||
1467 | 1490 | | |
1468 | 1491 | | |
1469 | 1492 | | |
1470 | | - | |
| 1493 | + | |
1471 | 1494 | | |
1472 | 1495 | | |
1473 | 1496 | | |
| |||
1498 | 1521 | | |
1499 | 1522 | | |
1500 | 1523 | | |
1501 | | - | |
| 1524 | + | |
1502 | 1525 | | |
1503 | 1526 | | |
1504 | 1527 | | |
| |||
0 commit comments