From 3370be68d985b8cec270c4b62d50132e0be85159 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Fri, 13 Mar 2026 21:33:38 -0400 Subject: [PATCH] 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. --- packages/api/src/agents/client.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/api/src/agents/client.ts b/packages/api/src/agents/client.ts index fd5d50f211..8d23df4c46 100644 --- a/packages/api/src/agents/client.ts +++ b/packages/api/src/agents/client.ts @@ -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[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; }; }