Skip to content

Commit 0776889

Browse files
kevin-dpclaude
andcommitted
fix(agents): estimate the tools breakdown from serialized schemas [review #4596]
`approxTokens(opts.tools)` hit approxTokens' array branch, which charges a flat ~64 per non-text block — so the "Tools" segment was ~64×toolCount regardless of how large each tool's name/description/parameter schema actually is, and the shortfall silently inflated the "Messages" remainder. Serialize the tool array first so the estimate reflects what really occupies the prompt. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent c055d7e commit 0776889

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

packages/agents-runtime/src/pi-adapter.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,12 @@ export function createPiAgentAdapter(
324324
// total minus these (see token-accountant computeContextBreakdown).
325325
const tokenBreakdown = {
326326
system: approxTokens(opts.systemPrompt),
327-
tools: approxTokens(opts.tools),
327+
// Serialize first: approxTokens' array branch charges a flat ~64 per
328+
// non-text block, so passing the AgentTool[] directly would estimate
329+
// ~64×toolCount regardless of the real schema size. JSON.stringify
330+
// captures each tool's name + description + parameter schema (functions
331+
// drop out), which is what actually occupies the prompt.
332+
tools: approxTokens(JSON.stringify(opts.tools)),
328333
}
329334

330335
const transformContext =

0 commit comments

Comments
 (0)