Commit 9cb1a8e
committed
fix: cache token double subtraction for non-Anthropic providers
PR #346 introduced cache token subtraction for all providers, causing
double subtraction for OpenAI/OpenRouter and resulting in negative costs.
The plugin-server only subtracts cache tokens for Anthropic providers
(exact match on provider name or substring match on model name). This
fix aligns the Python SDK with that behavior.
Changes:
- Only subtract cache tokens when provider="anthropic" OR model contains "anthropic"
- Passes provider and model metadata to usage parsing functions
- Updates tests to reflect correct behavior (no subtraction for OpenAI)
- Adds test for Anthropic provider subtraction
Fixes negative cost calculations for users on OpenAI/OpenRouter with cached tokens.1 parent 3a1b8e4 commit 9cb1a8e
File tree
4 files changed
+86
-17
lines changed- posthog
- ai/langchain
- test/ai/langchain
4 files changed
+86
-17
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
1 | 5 | | |
2 | 6 | | |
3 | 7 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
575 | 575 | | |
576 | 576 | | |
577 | 577 | | |
578 | | - | |
| 578 | + | |
579 | 579 | | |
580 | 580 | | |
581 | 581 | | |
| |||
696 | 696 | | |
697 | 697 | | |
698 | 698 | | |
| 699 | + | |
| 700 | + | |
699 | 701 | | |
700 | 702 | | |
701 | 703 | | |
| |||
764 | 766 | | |
765 | 767 | | |
766 | 768 | | |
767 | | - | |
768 | | - | |
769 | | - | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
770 | 784 | | |
771 | 785 | | |
772 | 786 | | |
773 | 787 | | |
774 | 788 | | |
775 | 789 | | |
776 | | - | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
777 | 793 | | |
778 | 794 | | |
779 | 795 | | |
| |||
787 | 803 | | |
788 | 804 | | |
789 | 805 | | |
790 | | - | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
791 | 809 | | |
792 | 810 | | |
793 | 811 | | |
794 | 812 | | |
795 | 813 | | |
796 | | - | |
| 814 | + | |
797 | 815 | | |
798 | 816 | | |
799 | 817 | | |
800 | 818 | | |
801 | 819 | | |
802 | 820 | | |
803 | 821 | | |
804 | | - | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
805 | 825 | | |
806 | 826 | | |
807 | 827 | | |
| |||
828 | 848 | | |
829 | 849 | | |
830 | 850 | | |
831 | | - | |
| 851 | + | |
832 | 852 | | |
833 | 853 | | |
834 | 854 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1584 | 1584 | | |
1585 | 1585 | | |
1586 | 1586 | | |
1587 | | - | |
| 1587 | + | |
| 1588 | + | |
| 1589 | + | |
1588 | 1590 | | |
1589 | 1591 | | |
1590 | 1592 | | |
1591 | 1593 | | |
1592 | 1594 | | |
1593 | 1595 | | |
| 1596 | + | |
| 1597 | + | |
| 1598 | + | |
| 1599 | + | |
| 1600 | + | |
| 1601 | + | |
| 1602 | + | |
| 1603 | + | |
| 1604 | + | |
| 1605 | + | |
| 1606 | + | |
| 1607 | + | |
| 1608 | + | |
| 1609 | + | |
| 1610 | + | |
| 1611 | + | |
| 1612 | + | |
| 1613 | + | |
| 1614 | + | |
| 1615 | + | |
| 1616 | + | |
| 1617 | + | |
| 1618 | + | |
| 1619 | + | |
| 1620 | + | |
| 1621 | + | |
| 1622 | + | |
| 1623 | + | |
| 1624 | + | |
| 1625 | + | |
| 1626 | + | |
| 1627 | + | |
| 1628 | + | |
| 1629 | + | |
| 1630 | + | |
| 1631 | + | |
| 1632 | + | |
| 1633 | + | |
| 1634 | + | |
| 1635 | + | |
| 1636 | + | |
| 1637 | + | |
| 1638 | + | |
1594 | 1639 | | |
1595 | 1640 | | |
1596 | 1641 | | |
| |||
1626 | 1671 | | |
1627 | 1672 | | |
1628 | 1673 | | |
1629 | | - | |
| 1674 | + | |
1630 | 1675 | | |
1631 | 1676 | | |
1632 | 1677 | | |
| |||
1708 | 1753 | | |
1709 | 1754 | | |
1710 | 1755 | | |
1711 | | - | |
| 1756 | + | |
1712 | 1757 | | |
1713 | 1758 | | |
1714 | 1759 | | |
| |||
1917 | 1962 | | |
1918 | 1963 | | |
1919 | 1964 | | |
1920 | | - | |
1921 | | - | |
| 1965 | + | |
| 1966 | + | |
1922 | 1967 | | |
1923 | 1968 | | |
1924 | 1969 | | |
| |||
1959 | 2004 | | |
1960 | 2005 | | |
1961 | 2006 | | |
1962 | | - | |
1963 | | - | |
| 2007 | + | |
| 2008 | + | |
1964 | 2009 | | |
1965 | 2010 | | |
1966 | 2011 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
0 commit comments