From 39b8db00cdfa91c797330b72a3edd686c3440efc Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Fri, 13 Mar 2026 23:01:13 -0400 Subject: [PATCH] refactor(agents): implement token count adjustment for Claude model messages Added a method to adjust token counts for messages processed by the Claude model, applying a correction factor to align with API expectations. This enhancement improves the accuracy of token counting, ensuring reliable functionality when interacting with the Claude model. --- api/server/controllers/agents/client.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/api/server/controllers/agents/client.js b/api/server/controllers/agents/client.js index f44d01ca79..73824d988b 100644 --- a/api/server/controllers/agents/client.js +++ b/api/server/controllers/agents/client.js @@ -1230,6 +1230,21 @@ class AgentClient extends BaseClient { const encoding = this.getEncoding(); return Tokenizer.getTokenCount(text, encoding); } + + /** @type {number} Anthropic message framing correction factor. */ + static CLAUDE_TOKEN_CORRECTION = 1.1; + + /** + * @param {object} message + * @returns {number} + */ + getTokenCountForMessage(message) { + const count = super.getTokenCountForMessage(message); + if (this.getEncoding() === 'claude') { + return Math.ceil(count * AgentClient.CLAUDE_TOKEN_CORRECTION); + } + return count; + } } module.exports = AgentClient;