Skip to content

Commit dedbbaf

Browse files
committed
[AI] fix(agents): align TOOL_RESULT_CHARS_PER_TOKEN with ESTIMATED_CHARS_PER_TOKEN
Change TOOL_RESULT_CHARS_PER_TOKEN from 2 to 4 so that tool-result text is estimated at the same chars-per-token ratio as ordinary text. The old value of 2 caused the mid-turn precheck to over-estimate tool-result token pressure by ~2x, triggering destructive truncate_tool_results_only recovery on turns well within the real context window. Updated affected test thresholds to match the corrected estimates and added a regression test for the reporter's scenario: ~250k chars of tool-result text across ~20 calls on a 200k context window with 50k reserve tokens should no longer false-positive overflow. Co-Authored-By: Claude <[email protected]> Related to #101929
1 parent 082bd45 commit dedbbaf

2 files changed

Lines changed: 40 additions & 11 deletions

File tree

‎src/agents/embedded-agent-runner/run/preemptive-compaction.test.ts‎

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,14 @@ describe("preemptive-compaction", () => {
266266
prompt: "continue",
267267
});
268268

269-
expect(estimatedPromptTokens).toBeGreaterThan(80_000);
269+
expect(estimatedPromptTokens).toBeGreaterThan(40_000);
270270

271271
const result = shouldPreemptivelyCompactBeforePrompt({
272272
messages,
273273
systemPrompt: "sys",
274274
prompt: "continue",
275-
contextTokenBudget: 96_000,
276-
reserveTokens: 20_000,
275+
contextTokenBudget: 52_000,
276+
reserveTokens: 5_000,
277277
});
278278

279279
expect(result.route).not.toBe("fits");
@@ -300,7 +300,7 @@ describe("preemptive-compaction", () => {
300300
expect(estimatedPromptTokens).toBeGreaterThan(30_000);
301301
});
302302

303-
it("prechecks a regression-sized synthetic tool-heavy transcript as over budget", () => {
303+
it("prechecks a regression-sized synthetic tool-heavy transcript as within the prompt budget", () => {
304304
const toolResultCharsPerMessage = Math.ceil(427_000 / 120);
305305
const generalCharsPerMessage = Math.ceil((503_000 - 427_000) / 121);
306306
const messages: AgentMessage[] = [];
@@ -325,10 +325,10 @@ describe("preemptive-compaction", () => {
325325
reserveTokens: 32_000,
326326
});
327327

328-
expect(result.estimatedPromptTokens).toBeGreaterThan(200_000);
328+
expect(result.estimatedPromptTokens).toBeLessThan(168_000);
329329
expect(result.promptBudgetBeforeReserve).toBe(168_000);
330-
expect(result.route).not.toBe("fits");
331-
expect(result.overflowTokens).toBeGreaterThan(0);
330+
expect(result.route).toBe("fits");
331+
expect(result.overflowTokens).toBe(0);
332332
});
333333

334334
it("caps reserve tokens so small context models keep usable prompt budget", () => {
@@ -517,7 +517,7 @@ describe("preemptive-compaction", () => {
517517
expect(aboveCutoff - belowCutoff).toBeLessThanOrEqual(2);
518518
});
519519

520-
it("keeps the conservative ratio for non-CJK tool results", () => {
520+
it("uses the normal text ratio for non-CJK tool results", () => {
521521
const latinText = "alpha beta gamma delta epsilon ".repeat(1000);
522522
const toolResultTokens = estimateLlmBoundaryTokenPressure({
523523
messages: [makeToolResultMessage(latinText)],
@@ -530,8 +530,7 @@ describe("preemptive-compaction", () => {
530530
prompt: "continue",
531531
});
532532

533-
expect(toolResultTokens).toBeGreaterThan(assistantTokens * 1.5);
534-
expect(toolResultTokens).toBeLessThan(assistantTokens * 2.5);
533+
expect(Math.abs(toolResultTokens - assistantTokens)).toBeLessThanOrEqual(5);
535534
});
536535

537536
it("applies the CJK-aware ratio to JSON tool-result payloads", () => {
@@ -584,4 +583,34 @@ describe("preemptive-compaction", () => {
584583

585584
expect(Number.isFinite(result.estimatedPromptTokens)).toBe(true);
586585
});
586+
587+
it("does not false-positive overflow for a tool-result-heavy turn within the context window (#101929)", () => {
588+
// The reporter's scenario: ~250k chars of tool result text across ~20
589+
// tool-result messages on a 200k context window with 50k reserve tokens.
590+
// With TOOL_RESULT_CHARS_PER_TOKEN=4 the estimate stays comfortably below
591+
// the 150k prompt budget (200k − 50k), where TOOL_RESULT_CHARS_PER_TOKEN=2
592+
// produced ~162k estimated tokens → false-positive overflow.
593+
const toolCharsPerMessage = Math.ceil(250_000 / 20);
594+
const messages: AgentMessage[] = [];
595+
for (let i = 0; i < 43; i += 1) {
596+
if (i % 2 === 0) {
597+
messages.push(makeToolResultMessage("x".repeat(toolCharsPerMessage)));
598+
} else {
599+
messages.push(makeAssistantHistory("y".repeat(500)));
600+
}
601+
}
602+
603+
const result = shouldPreemptivelyCompactBeforePrompt({
604+
messages,
605+
systemPrompt: "sys",
606+
prompt: "continue",
607+
contextTokenBudget: 200_000,
608+
reserveTokens: 50_000,
609+
});
610+
611+
expect(result.estimatedPromptTokens).toBeLessThan(150_000);
612+
expect(result.route).toBe("fits");
613+
expect(result.shouldCompact).toBe(false);
614+
expect(result.overflowTokens).toBe(0);
615+
});
587616
});

‎src/agents/embedded-agent-runner/run/preemptive-compaction.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const PREEMPTIVE_OVERFLOW_ERROR_TEXT =
2424
"Context overflow: prompt too large for the model (precheck).";
2525

2626
const ESTIMATED_CHARS_PER_TOKEN = 4;
27-
const TOOL_RESULT_CHARS_PER_TOKEN = 2;
27+
const TOOL_RESULT_CHARS_PER_TOKEN = 4;
2828
const JSON_PAYLOAD_CHARS_PER_TOKEN = 3;
2929
const MESSAGE_BOUNDARY_OVERHEAD_TOKENS = 12;
3030
const CONTENT_BLOCK_OVERHEAD_TOKENS = 6;

0 commit comments

Comments
 (0)