From 24322e6f214d7550053effe0c916aed536e485ea Mon Sep 17 00:00:00 2001 From: Marco Beretta <81851188+berry-13@users.noreply.github.com> Date: Sun, 8 Nov 2026 14:30:46 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=BE=20test:=20Bill=20Advertised=20Tool?= =?UTF-8?q?=20Definitions=20in=20the=20Mock=20Model's=20Input=20Tokens=20(?= =?UTF-8?q?#17897)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- e2e/setup/fake-model.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/e2e/setup/fake-model.js b/e2e/setup/fake-model.js index 68631139d6..73402f90a3 100644 --- a/e2e/setup/fake-model.js +++ b/e2e/setup/fake-model.js @@ -347,6 +347,19 @@ function collectToolDefinitions(agents) { return definitions; } +/** Text a provider bills for one advertised tool: its name, description and JSON schema. + * A zod schema (on a LangChain tool) is skipped, since serializing it reports the + * validator's internals rather than the schema the provider receives. */ +function toolDefinitionText(name, definition) { + const schema = definition?.parameters ?? definition?.input_schema ?? definition?.schema; + const plainSchema = + schema != null && typeof schema === 'object' && typeof schema.safeParse !== 'function' + ? JSON.stringify(schema) + : ''; + const description = typeof definition?.description === 'string' ? definition.description : ''; + return `${name}${description}${plainSchema}`; +} + function collectToolNames(agents) { return new Set(collectToolDefinitions(agents).keys()); } @@ -1012,15 +1025,22 @@ class UsageEmittingFakeChatModel extends FakeChatModel { outputChars += typeof chunk.text === 'string' ? chunk.text.length : 0; yield chunk; } - const { messages: promptMessages } = await getStreamAgentView({ + const { messages: promptMessages, toolDefinitions } = await getStreamAgentView({ graph: this.graph, messages: messages ?? [], options, runManager, }); + /** A provider bills the advertised tool definitions too. The context snapshot counts + * them inside its instruction share and reconciles against this total, so leaving + * them out would leave too little for the messages and the tool results they hold. */ + let toolChars = 0; + for (const [name, definition] of toolDefinitions) { + toolChars += toolDefinitionText(name, definition).length; + } const inputChars = promptMessages.reduce( (sum, message) => sum + getContentText(message?.content).length, - 0, + toolChars, ); const input_tokens = Math.max(1, Math.ceil(inputChars / 4)); const output_tokens = Math.max(1, Math.ceil(outputChars / 4));