Skip to content

Commit 1a9eafe

Browse files
committed
fix(agents): reduce tool-result token over-count in mid-turn precheck
TOOL_RESULT_CHARS_PER_TOKEN was 2 in both tool-result-char-estimator.ts and preemptive-compaction.ts. Tool results (JSON, code output) average ~4-5 chars/token in practice — not 2. The 2 chars/token estimate produced a ~2-2.6× token over-count vs billed usage, triggering mid-turn truncation on turns far below the real context window. Change TOOL_RESULT_CHARS_PER_TOKEN(_ESTIMATE) from 2 to 3 in both files. The estimator ratio drops from 4/2=2.0x to 4/3≈1.33x, and the token pressure estimate changes from chars/2 to chars/3. Fixes #101929
1 parent 5c4b639 commit 1a9eafe

5 files changed

Lines changed: 18 additions & 12 deletions

File tree

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

Lines changed: 5 additions & 5 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(50_000);
270270

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

279279
expect(result.route).not.toBe("fits");
@@ -530,8 +530,8 @@ describe("preemptive-compaction", () => {
530530
prompt: "continue",
531531
});
532532

533-
expect(toolResultTokens).toBeGreaterThan(assistantTokens * 1.5);
534-
expect(toolResultTokens).toBeLessThan(assistantTokens * 2.5);
533+
expect(toolResultTokens).toBeGreaterThan(assistantTokens * 1.1);
534+
expect(toolResultTokens).toBeLessThan(assistantTokens * 1.8);
535535
});
536536

537537
it("applies the CJK-aware ratio to JSON tool-result payloads", () => {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ 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+
// Tool results (JSON, code output) average ~4-5 chars/token in practice.
28+
// The old 2 chars/token under-estimates density, causing ~2-2.6× over-count
29+
// vs billed usage that triggers mid-turn precheck too early.
30+
const TOOL_RESULT_CHARS_PER_TOKEN = 3;
2831
const JSON_PAYLOAD_CHARS_PER_TOKEN = 3;
2932
const MESSAGE_BOUNDARY_OVERHEAD_TOKENS = 12;
3033
const CONTENT_BLOCK_OVERHEAD_TOKENS = 6;

‎src/agents/embedded-agent-runner/tool-result-char-estimator.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe("tool-result-char-estimator", () => {
2828

2929
const cache = createMessageCharEstimateCache();
3030
const chars = estimateMessageCharsCached(malformed, cache);
31-
expect(chars).toBe(30);
31+
expect(chars).toBe(20);
3232
});
3333

3434
it("estimates text content when toolResult content includes null entries", () => {
@@ -41,7 +41,7 @@ describe("tool-result-char-estimator", () => {
4141

4242
const cache = createMessageCharEstimateCache();
4343
const chars = estimateMessageCharsCached(malformed, cache);
44-
expect(chars).toBe(12);
44+
expect(chars).toBe(8);
4545
});
4646

4747
it("getToolResultText skips malformed text blocks", () => {
@@ -65,7 +65,7 @@ describe("tool-result-char-estimator", () => {
6565

6666
const cache = createMessageCharEstimateCache();
6767
const chars = estimateMessageCharsCached(msg, cache);
68-
expect(chars).toBe(22);
68+
expect(chars).toBe(15);
6969
});
7070

7171
it("estimates a large bashExecution near its rendered size", () => {

‎src/agents/embedded-agent-runner/tool-result-char-estimator.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import {
1111
} from "../runtime/index.js";
1212

1313
export const CHARS_PER_TOKEN_ESTIMATE = 4;
14-
export const TOOL_RESULT_CHARS_PER_TOKEN_ESTIMATE = 2;
14+
// Tool results (JSON, code output) average ~4-5 chars/token in practice.
15+
// The old value of 2 chars/token under-estimates density by ~2×, causing
16+
// ~2-2.6× token over-count that triggers mid-turn truncation too early.
17+
export const TOOL_RESULT_CHARS_PER_TOKEN_ESTIMATE = 3;
1518
const IMAGE_CHAR_ESTIMATE = 8_000;
1619

1720
export type MessageCharEstimateCache = WeakMap<AgentMessage, number>;

‎src/agents/embedded-agent-runner/tool-result-context-guard.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ describe("installToolResultContextGuard", () => {
251251
it("drops oversized tool-result details when truncating once", async () => {
252252
const agent = makeGuardableAgent();
253253
const contextForNextCall = [
254-
makeToolResultWithDetails("call_big", "x".repeat(900), "d".repeat(8_000)),
254+
makeToolResultWithDetails("call_big", "x".repeat(2_000), "d".repeat(8_000)),
255255
];
256256

257257
const transformed = (await applyGuardToContext(agent, contextForNextCall)) as AgentMessage[];
@@ -316,7 +316,7 @@ describe("installToolResultContextGuard", () => {
316316

317317
it("supports model-window-specific truncation for large but otherwise valid tool results", async () => {
318318
const agent = makeGuardableAgent();
319-
const contextForNextCall = [makeToolResult("call_big", "q".repeat(95_000))];
319+
const contextForNextCall = [makeToolResult("call_big", "q".repeat(120_000))];
320320

321321
const transformed = (await applyGuardToContext(
322322
agent,

0 commit comments

Comments
 (0)