Commit 63f99a5
fix(langchain): stop double-counting anthropic cache tokens in prompt totals
langchain-anthropic has folded cache read/creation tokens into
usage_metadata input_tokens since 0.2.3 (versions before that don't emit
input_token_details at all), and langchain-aws does the same — per the
langchain-core UsageMetadata contract, input_token_details is a breakdown
of input_tokens, not an addition to it.
The cache normalization from #411/#445 detected "separate cache token
accounting" by the presence of cache_creation/ephemeral_* detail keys,
which langchain-anthropic always emits, so every cached Anthropic call
had cache tokens added to prompt_tokens a second time. With a warm cache
this roughly doubles reported prompt tokens (e.g. a real trace reported
75,387 prompt tokens for a 37,694-token request with 37,324 cache reads
and 369 cache writes).
Detect separate accounting arithmetically instead: only fold cache
tokens into prompt/total when they exceed the reported prompt total,
which is impossible under the UsageMetadata contract but is exactly the
inconsistency the original normalization (BT-5150) was added to repair.
Strengthen the VCR prompt-caching test to assert span prompt/total
tokens equal the usage_metadata the model reported, and add unit
coverage for the folded (Anthropic), subset (OpenAI), and separate
(legacy) conventions.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>1 parent 82d86b1 commit 63f99a5
2 files changed
Lines changed: 108 additions & 25 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
617 | 617 | | |
618 | 618 | | |
619 | 619 | | |
620 | | - | |
621 | | - | |
622 | | - | |
623 | | - | |
624 | | - | |
625 | | - | |
626 | | - | |
627 | | - | |
628 | | - | |
629 | | - | |
630 | | - | |
631 | | - | |
632 | | - | |
633 | | - | |
634 | | - | |
635 | | - | |
636 | | - | |
637 | | - | |
638 | 620 | | |
639 | 621 | | |
640 | 622 | | |
| |||
685 | 667 | | |
686 | 668 | | |
687 | 669 | | |
688 | | - | |
689 | | - | |
690 | | - | |
691 | | - | |
692 | | - | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
693 | 677 | | |
694 | 678 | | |
695 | | - | |
696 | | - | |
| 679 | + | |
697 | 680 | | |
698 | 681 | | |
699 | 682 | | |
| |||
Lines changed: 100 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| 16 | + | |
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
| |||
906 | 908 | | |
907 | 909 | | |
908 | 910 | | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
909 | 999 | | |
910 | 1000 | | |
911 | 1001 | | |
| |||
1098 | 1188 | | |
1099 | 1189 | | |
1100 | 1190 | | |
| 1191 | + | |
| 1192 | + | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + | |
| 1196 | + | |
1101 | 1197 | | |
1102 | 1198 | | |
1103 | 1199 | | |
| |||
1134 | 1230 | | |
1135 | 1231 | | |
1136 | 1232 | | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
1137 | 1237 | | |
1138 | 1238 | | |
1139 | 1239 | | |
| |||
0 commit comments