Commit eb15a0e
committed
fix(translator): stop double-counting OpenAI cache tokens in Anthropic streaming usage
OpenAI's prompt_tokens_details.cached_tokens and cache_creation_input_tokens
are a breakdown *within* prompt_tokens (analogous to text_tokens/audio_tokens
in the same struct), not additive extras. Anthropic's native usage schema is
the opposite: input_tokens excludes cache tokens, which are reported as
separate, additive counters (cache_read_input_tokens,
cache_creation_input_tokens).
The previous commit forwarded OpenAI's cache token breakdown directly into
Anthropic's additive cache usage fields without accounting for the semantic
mismatch. Since Anthropic-consuming clients compute total context usage as
input_tokens + cache_read_input_tokens + cache_creation_input_tokens, this
caused the cached portion of every prompt to be counted twice: once inside
input_tokens (via OpenAI's prompt_tokens) and again via the cache fields.
Depending on the backend's exact reporting of cache_creation_input_tokens
(a field with no documented meaning in OpenAI's real Chat Completions API,
so its semantics are backend-defined), this could inflate the reported
input token count far beyond the real prompt size, up to the point of
clients rejecting requests as exceeding the model's context window.
Revert to not forwarding OpenAI's cache token breakdown into Anthropic's
cache usage fields, for both the client-facing message_delta.usage payload
and the gateway's internal TokenUsage metrics. input_tokens/output_tokens
continue to be backfilled correctly, which was the original motivating fix.
The pre-existing ptr.To(int64(0)) cache args to
ExtractTokenUsageFromExplicitCaching are left untouched to keep this change
minimal.
Signed-off-by: ajac-zero <ajcardoza2000@gmail.com>1 parent 90394b5 commit eb15a0e
4 files changed
Lines changed: 45 additions & 45 deletions
File tree
- internal/translator
- tests/data-plane
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
385 | 385 | | |
386 | 386 | | |
387 | 387 | | |
388 | | - | |
| 388 | + | |
389 | 389 | | |
390 | 390 | | |
391 | 391 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
630 | 630 | | |
631 | 631 | | |
632 | 632 | | |
633 | | - | |
634 | | - | |
635 | | - | |
636 | | - | |
| 633 | + | |
| 634 | + | |
637 | 635 | | |
638 | 636 | | |
639 | 637 | | |
| |||
642 | 640 | | |
643 | 641 | | |
644 | 642 | | |
645 | | - | |
646 | | - | |
647 | | - | |
648 | | - | |
649 | | - | |
650 | | - | |
651 | | - | |
652 | | - | |
653 | | - | |
654 | | - | |
655 | | - | |
656 | | - | |
657 | | - | |
658 | | - | |
659 | | - | |
660 | | - | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
661 | 657 | | |
662 | 658 | | |
663 | 659 | | |
| |||
743 | 739 | | |
744 | 740 | | |
745 | 741 | | |
746 | | - | |
747 | | - | |
748 | | - | |
749 | | - | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
750 | 748 | | |
751 | 749 | | |
752 | 750 | | |
753 | | - | |
754 | | - | |
| 751 | + | |
| 752 | + | |
755 | 753 | | |
756 | 754 | | |
757 | 755 | | |
| |||
1059 | 1057 | | |
1060 | 1058 | | |
1061 | 1059 | | |
1062 | | - | |
1063 | | - | |
1064 | | - | |
| 1060 | + | |
| 1061 | + | |
1065 | 1062 | | |
1066 | 1063 | | |
1067 | 1064 | | |
1068 | 1065 | | |
1069 | | - | |
1070 | | - | |
1071 | | - | |
1072 | | - | |
| 1066 | + | |
| 1067 | + | |
1073 | 1068 | | |
1074 | 1069 | | |
1075 | 1070 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
612 | 612 | | |
613 | 613 | | |
614 | 614 | | |
615 | | - | |
| 615 | + | |
616 | 616 | | |
617 | 617 | | |
618 | 618 | | |
619 | 619 | | |
620 | 620 | | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
621 | 624 | | |
622 | | - | |
623 | | - | |
624 | 625 | | |
625 | 626 | | |
626 | 627 | | |
| |||
647 | 648 | | |
648 | 649 | | |
649 | 650 | | |
650 | | - | |
| 651 | + | |
651 | 652 | | |
652 | | - | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
653 | 658 | | |
654 | 659 | | |
655 | | - | |
| 660 | + | |
656 | 661 | | |
657 | 662 | | |
658 | | - | |
| 663 | + | |
659 | 664 | | |
660 | 665 | | |
661 | 666 | | |
| |||
696 | 701 | | |
697 | 702 | | |
698 | 703 | | |
699 | | - | |
| 704 | + | |
700 | 705 | | |
701 | 706 | | |
702 | 707 | | |
| |||
733 | 738 | | |
734 | 739 | | |
735 | 740 | | |
736 | | - | |
| 741 | + | |
737 | 742 | | |
738 | 743 | | |
739 | 744 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1342 | 1342 | | |
1343 | 1343 | | |
1344 | 1344 | | |
1345 | | - | |
| 1345 | + | |
1346 | 1346 | | |
1347 | 1347 | | |
1348 | 1348 | | |
| |||
1379 | 1379 | | |
1380 | 1380 | | |
1381 | 1381 | | |
1382 | | - | |
| 1382 | + | |
1383 | 1383 | | |
1384 | 1384 | | |
1385 | 1385 | | |
| |||
0 commit comments