Skip to content

Conversation

@dtrugman
Copy link
Contributor

@dtrugman dtrugman commented Mar 7, 2025

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 calculateApiCost to calculateApiCostAnthropic and created a new calculateApiCostOpenAI method.
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.

  • Behavior:
    • Introduces calculateApiCostOpenAI in cost.ts for OpenAI-style cost calculation, where input tokens include cached tokens.
    • Updates RequestyHandler in requesty.ts to use calculateApiCostOpenAI.
    • Updates Cline in Cline.ts to use calculateApiCostAnthropic.
  • Refactoring:
    • Renames calculateApiCost to calculateApiCostAnthropic.
    • Adds calculateApiCostInternal to handle shared logic.
  • Testing:
    • Adds tests for calculateApiCostOpenAI in cost.test.ts.
    • Updates tests for calculateApiCostAnthropic in cost.test.ts.

This description was created by Ellipsis for 7d038607066de19aff9912c4c9f7411f1fef1ece. It will automatically update as commits are pushed.

@changeset-bot
Copy link

changeset-bot bot commented Mar 7, 2025

⚠️ No Changeset found

Latest commit: 03045d8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request labels Mar 7, 2025
@dtrugman
Copy link
Contributor Author

dtrugman commented Mar 7, 2025

Fixing tests...

@dtrugman dtrugman force-pushed the feat/add-openai-style-cost-calculation branch from 7d03860 to c51f59e Compare March 7, 2025 16:41
@hannesrudolph hannesrudolph moved this from New to PR [Unverified] in Roo Code Roadmap Mar 7, 2025
@mrubens mrubens moved this from PR [Unverified] to PR [Greenlit] in Roo Code Roadmap Mar 10, 2025
@dtrugman
Copy link
Contributor Author

Hi @mrubens,
This PR adds support for OpenAI-style price calculation, which will be useful for OpenAI & Deepseek providers as well (The current calculation is wrong).
I had to touch more files than I would ideally want, but tried to make them as clear and contained as possible.

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?

Copy link
Collaborator

@mrubens mrubens left a 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!

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Mar 10, 2025
@mrubens mrubens merged commit 6644202 into RooCodeInc:main Mar 10, 2025
11 checks passed
@github-project-automation github-project-automation bot moved this from PR [Greenlit] to Done in Roo Code Roadmap Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants