mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-07-11 08:43:48 +00:00
refactor: adjust token counting for Claude model to account for API discrepancies
Implemented a correction factor for token counting when using the Claude model, addressing discrepancies between Anthropic's API and local tokenizer results. This change ensures accurate token counts by applying a scaling factor, improving the reliability of token-related functionalities.
This commit is contained in:
parent
b87f9c7154
commit
3370be68d9
1 changed files with 10 additions and 1 deletions
|
|
@ -27,10 +27,19 @@ export function payloadParser({ req, endpoint }: { req: ServerRequest; endpoint:
|
|||
return req.body?.endpointOption?.model_parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Anthropic's API consistently reports ~10% more tokens than the local
|
||||
* claude tokenizer due to internal message framing and content encoding.
|
||||
* Verified empirically across content types via the count_tokens endpoint.
|
||||
*/
|
||||
const CLAUDE_TOKEN_CORRECTION = 1.1;
|
||||
|
||||
export function createTokenCounter(encoding: Parameters<typeof Tokenizer.getTokenCount>[1]) {
|
||||
const isClaude = encoding === 'claude';
|
||||
return function (message: BaseMessage) {
|
||||
const countTokens = (text: string) => Tokenizer.getTokenCount(text, encoding);
|
||||
return getTokenCountForMessage(message, countTokens);
|
||||
const count = getTokenCountForMessage(message, countTokens);
|
||||
return isClaude ? Math.ceil(count * CLAUDE_TOKEN_CORRECTION) : count;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue