fix: calibrate input token when using anthropic models of sap ai core… #5469
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Related Issue
Issue: #XXXX
No issue is created yet, though there are discussions about token count information issue in PR #4683 and #5399.
Here is a screenshot to show that after prompt cache is enabled for anthropic model, the context usage is much fewer than the real situation:

As it shows, the input token is 4, and the context window usage is 4 + 1.0k ≈ 1.0K, which is not correct as the size of cline system prompt is more than 4 tokens.
Description
After #5399 is merged, there is a token counting issue
The root cause of it is SAP AI Core doesn't return cache read and cache write information.
The actual token count usage equation should be:
Since both cache write and cache read are empty, we can set input as:
By doing this, we can get the correct context usage.
Test Procedure
Type of Change
Pre-flight Checklist
npm test
) and code is formatted and linted (npm run format && npm run lint
)npm run changeset
(required for user-facing changes)Screenshots
Additional Notes
Important
Fixes token counting issue in
SapAiCoreHandler
for anthropic models by recalibrating input token counts.SapAiCoreHandler
for anthropic models by recalibrating input token counts.input = totalTokens - outputTokens
when cache read/write are empty.sapaicore.ts
to implement the recalibration logic in theSapAiCoreHandler
class.This description was created by
for 7148f5e. You can customize this summary. It will automatically update as commits are pushed.