Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/server/request-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1004,10 +1004,14 @@ function finalizedUsage(
const usageFallback = !finalUsage && estimate !== undefined
? { inputTokens: estimate, outputTokens: 0, estimated: true }
: undefined;
const loggedUsage = finalUsage && estimate !== undefined
const combinedInputTokens = finalUsage && estimate !== undefined
? Math.max(finalUsage.inputTokens, estimate)
: undefined;
const loggedUsage = finalUsage && combinedInputTokens !== undefined
? {
...finalUsage,
inputTokens: Math.max(finalUsage.inputTokens, estimate),
inputTokens: combinedInputTokens,
totalTokens: combinedInputTokens + finalUsage.outputTokens,
estimated: true,
}
: finalUsage
Expand All @@ -1017,7 +1021,11 @@ function finalizedUsage(
// ESTIMATE via capEstimateAtContextWindow, and Math.max preserves a real
// provider-reported count, so it needs no further reduction.
? (finalUsage.estimated && contextWindow !== undefined && finalUsage.inputTokens > contextWindow
? { ...finalUsage, inputTokens: contextWindow }
? {
...finalUsage,
inputTokens: contextWindow,
totalTokens: contextWindow + finalUsage.outputTokens,
}
: finalUsage)
: usageFallback;
const totalTokens = usageTotalTokens(loggedUsage);
Expand Down
51 changes: 51 additions & 0 deletions tests/request-log-estimate-cap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,57 @@ describe("request log token-estimate context-window cap (codex-router PR #140)",
expect(attempt.usage?.inputTokens).toBe(100_000);
});

test("capping adapter-estimated input recomputes an explicit total", () => {
const attempt = beginRequestAttempt(1, "kiro", "deepseek-3.2", "kiro");
finishRequestAttempt(attempt, 200, 10, {
inputTokens: 200_000,
outputTokens: 4,
totalTokens: 200_004,
estimated: true,
});
expect(attempt.usage).toEqual({
inputTokens: DEEPSEEK_WINDOW,
outputTokens: 4,
totalTokens: DEEPSEEK_WINDOW + 4,
estimated: true,
});
expect(attempt.totalTokens).toBe(DEEPSEEK_WINDOW + 4);
});

test("combining a local estimate recomputes the nested and outer totals", () => {
const attempt = beginRequestAttempt(1, "kiro", "deepseek-3.2", "kiro");
noteAttemptSend(attempt, 200_000);
finishRequestAttempt(attempt, 200, 10, {
inputTokens: 50,
outputTokens: 4,
totalTokens: 54,
});
expect(attempt.usage).toEqual({
inputTokens: DEEPSEEK_WINDOW,
outputTokens: 4,
totalTokens: DEEPSEEK_WINDOW + 4,
estimated: true,
});
expect(attempt.totalTokens).toBe(DEEPSEEK_WINDOW + 4);
});

test("caps an estimated Cursor checkpoint without double-adding output", () => {
const attempt = beginRequestAttempt(1, "cursor", "claude-4.6-opus-high", "cursor");
finishRequestAttempt(attempt, 200, 10, {
inputTokens: 499_994,
outputTokens: 6,
totalTokens: 500_000,
estimated: true,
});
expect(attempt.usage).toEqual({
inputTokens: CURSOR_CLAUDE_WINDOW,
outputTokens: 6,
totalTokens: CURSOR_CLAUDE_WINDOW + 6,
estimated: true,
});
expect(attempt.totalTokens).toBe(CURSOR_CLAUDE_WINDOW + 6);
});

test("a positive provider-reported input count is never reduced by the cap", () => {
const attempt = beginRequestAttempt(1, "kiro", "deepseek-3.2", "kiro");
noteAttemptSend(attempt, 200_000); // estimate would exceed the window
Expand Down
Loading