-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add openai style cost calculation #1451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add openai style cost calculation #1451
Conversation
|
|
Fixing tests... |
7d03860 to
c51f59e
Compare
|
Hi @mrubens, If you are not interested in promoting this change, I can simply copy the right, openai-style calculation into the requesty.ts file and only touch that one. Would that help us merge that quicker? |
mrubens
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix!
Context
The project had only one "method" of cost calculation, the Anthropic method, where the reported "input tokens" do NOT include the number of "cache read tokens".
This change introduces a new method that calculates the price correctly when the use is reported in an OpenAI compatible format, i.e. where the "input tokens" INCLUDE the "cache read token" count.
And uses the new method to correctly calculate the price for the Requesty provider.
Implementation
Renamed
calculateApiCosttocalculateApiCostAnthropicand created a newcalculateApiCostOpenAImethod.Both reuse the same underlying method.
Screenshots
Before:
Reported interaction/task cost for Requesty is incorrect
After:
Reported interaction/task cost for Requesty is correct
How to Test
There are unit-tests that cover both cost calculation methods.
The Anthropic method is covered by the existing tests.
And a new OpenAI method that is covering the new method.
Set up the Requesty provider.
Use any OpenAI model, e.g.
openai/gpt-4o-mini.Make sure the cost is correct.
I tested manually Requesty, Deepseek, OpenAI and Anthropic. All work as expected (Requesty fixed, no regressions for other providers).
Get in Touch
@ditti.o on discord
Important
Adds OpenAI-style cost calculation and updates relevant components and tests.
calculateApiCostOpenAIincost.tsfor OpenAI-style cost calculation, where input tokens include cached tokens.RequestyHandlerinrequesty.tsto usecalculateApiCostOpenAI.ClineinCline.tsto usecalculateApiCostAnthropic.calculateApiCosttocalculateApiCostAnthropic.calculateApiCostInternalto handle shared logic.calculateApiCostOpenAIincost.test.ts.calculateApiCostAnthropicincost.test.ts.This description was created by
for 7d038607066de19aff9912c4c9f7411f1fef1ece. It will automatically update as commits are pushed.