Skip to content

Commit 4706167

Browse files
author
杨爱文
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 03ca096 commit 4706167

2 files changed

Lines changed: 40 additions & 8 deletions

File tree

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

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,14 @@ describe("preemptive-compaction", () => {
268268
prompt: "continue",
269269
});
270270

271-
expect(estimatedPromptTokens).toBeGreaterThan(80_000);
271+
expect(estimatedPromptTokens).toBeGreaterThan(40_000);
272272

273273
const result = shouldPreemptivelyCompactBeforePrompt({
274274
messages,
275275
systemPrompt: "sys",
276276
prompt: "continue",
277-
contextTokenBudget: 96_000,
278-
reserveTokens: 20_000,
277+
contextTokenBudget: 40_000,
278+
reserveTokens: 0,
279279
});
280280

281281
expect(result.route).not.toBe("fits");
@@ -323,12 +323,12 @@ describe("preemptive-compaction", () => {
323323
messages,
324324
systemPrompt: "system".repeat(200),
325325
prompt: "continue",
326-
contextTokenBudget: 200_000,
327-
reserveTokens: 32_000,
326+
contextTokenBudget: 130_000,
327+
reserveTokens: 0,
328328
});
329329

330-
expect(result.estimatedPromptTokens).toBeGreaterThan(200_000);
331-
expect(result.promptBudgetBeforeReserve).toBe(168_000);
330+
expect(result.estimatedPromptTokens).toBeGreaterThan(130_000);
331+
expect(result.promptBudgetBeforeReserve).toBe(130_000);
332332
expect(result.route).not.toBe("fits");
333333
expect(result.overflowTokens).toBeGreaterThan(0);
334334
});
@@ -455,4 +455,36 @@ describe("preemptive-compaction", () => {
455455
expect(result.route).toBe("truncate_tool_results_only");
456456
expect(result.shouldCompact).toBe(false);
457457
});
458+
459+
it("does not false-positive overflow for a tool-result-heavy turn well within the context window (#101929)", () => {
460+
// ~250k chars of tool result text across ~20 messages, plus assistant turns.
461+
// With TOOL_RESULT_CHARS_PER_TOKEN=4, this should estimate far below the
462+
// 150k prompt budget (200k context − 50k reserve) instead of the ~162k
463+
// false overflow the old TOOL_RESULT_CHARS_PER_TOKEN=2 would produce.
464+
const toolCharsPerMessage = Math.ceil(250_000 / 20);
465+
const messages: AgentMessage[] = [];
466+
for (let i = 0; i < 43; i += 1) {
467+
if (i % 2 === 0) {
468+
messages.push(makeToolResultMessage("x".repeat(toolCharsPerMessage)));
469+
} else {
470+
messages.push(makeAssistantHistory("y".repeat(500)));
471+
}
472+
}
473+
474+
const result = shouldPreemptivelyCompactBeforePrompt({
475+
messages,
476+
systemPrompt: "sys",
477+
prompt: "continue",
478+
contextTokenBudget: 200_000,
479+
reserveTokens: 50_000,
480+
});
481+
482+
// The prompt budget before reserve is 150k. The corrected estimator
483+
// should produce an estimate comfortably below that for ~250k chars
484+
// of tool results plus minor assistant overhead.
485+
expect(result.estimatedPromptTokens).toBeLessThan(150_000);
486+
expect(result.route).toBe("fits");
487+
expect(result.shouldCompact).toBe(false);
488+
expect(result.overflowTokens).toBe(0);
489+
});
458490
});

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

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

1919
const ESTIMATED_CHARS_PER_TOKEN = 4;
20-
const TOOL_RESULT_CHARS_PER_TOKEN = 2;
20+
const TOOL_RESULT_CHARS_PER_TOKEN = 4;
2121
const JSON_PAYLOAD_CHARS_PER_TOKEN = 3;
2222
const MESSAGE_BOUNDARY_OVERHEAD_TOKENS = 12;
2323
const CONTENT_BLOCK_OVERHEAD_TOKENS = 6;

0 commit comments

Comments
 (0)