Skip to content

Commit 2dab142

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 d16ba0b commit 2dab142

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
@@ -320,7 +320,12 @@ export function createPiAgentAdapter(
320320
// total minus these (see token-accountant computeContextBreakdown).
321321
const tokenBreakdown = {
322322
system: approxTokens(opts.systemPrompt),
323-
tools: approxTokens(opts.tools),
323+
// Serialize first: approxTokens' array branch charges a flat ~64 per
324+
// non-text block, so passing the AgentTool[] directly would estimate
325+
// ~64×toolCount regardless of the real schema size. JSON.stringify
326+
// captures each tool's name + description + parameter schema (functions
327+
// drop out), which is what actually occupies the prompt.
328+
tools: approxTokens(JSON.stringify(opts.tools)),
324329
}
325330

326331
const transformContext =

0 commit comments

Comments
 (0)