Skip to content

Conversation

GTxx
Copy link
Contributor

@GTxx GTxx commented Aug 9, 2025

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:
image

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:

totalToken = input + cache write + cache read + output

Since both cache write and cache read are empty, we can set input as:

input = totalToken - output

By doing this, we can get the correct context usage.

Test Procedure

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • ♻️ Refactor Changes
  • 💅 Cosmetic Changes
  • 📚 Documentation update
  • 🏃 Workflow Changes

Pre-flight Checklist

  • Changes are limited to a single feature, bugfix or chore (split larger changes into separate PRs)
  • Tests are passing (npm test) and code is formatted and linted (npm run format && npm run lint)
  • I have created a changeset using npm run changeset (required for user-facing changes)
  • I have reviewed contributor guidelines

Screenshots

Additional Notes


Important

Fixes token counting issue in SapAiCoreHandler for anthropic models by recalibrating input token counts.

  • Behavior:
    • Fixes token counting issue in SapAiCoreHandler for anthropic models by recalibrating input token counts.
    • Adjusts input token calculation to input = totalTokens - outputTokens when cache read/write are empty.
  • Files:
    • Modifies sapaicore.ts to implement the recalibration logic in the SapAiCoreHandler class.

This description was created by Ellipsis for 7148f5e. You can customize this summary. It will automatically update as commits are pushed.

Copy link

changeset-bot bot commented Aug 9, 2025

🦋 Changeset detected

Latest commit: 419953b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
claude-dev Patch

Not sure what this means? Click here to learn what changesets are.

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

@GTxx GTxx force-pushed the fix/sapaicore_anthropic_model_input_token_count branch from 7148f5e to 419953b Compare August 9, 2025 08:53
@saoudrizwan saoudrizwan merged commit cad28c4 into cline:main Aug 9, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants