refactor(agents): token counting for media content in messages

Introduced a new method to estimate token costs for image and document blocks in messages, improving the accuracy of token counting. This enhancement ensures that media content is properly accounted for, particularly for the Claude model, by integrating additional token estimation logic for various content types. Updated the token counting function to utilize this new method, enhancing overall reliability and functionality.
This commit is contained in:
Danny Avila 2026-03-15 14:26:44 -04:00
parent d2a4d9fbee
commit 9ab29a9b29
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956
2 changed files with 169 additions and 4 deletions

View file

@ -25,6 +25,7 @@ const {
loadAgent: loadAgentFn,
createMultiAgentMapper,
filterMalformedContentParts,
estimateMediaTokensForMessage,
hydrateMissingIndexTokenCounts,
} = require('@librechat/api');
const {
@ -1237,8 +1238,12 @@ class AgentClient extends BaseClient {
* @returns {number}
*/
getTokenCountForMessage(message) {
const count = super.getTokenCountForMessage(message);
if (this.getEncoding() === 'claude') {
const isClaude = this.getEncoding() === 'claude';
let count = super.getTokenCountForMessage(message);
count += estimateMediaTokensForMessage(message.content ?? message.text, isClaude, (text) =>
this.getTokenCount(text),
);
if (isClaude) {
return Math.ceil(count * AgentClient.CLAUDE_TOKEN_CORRECTION);
}
return count;