Skip to content
Merged
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
4 changes: 4 additions & 0 deletions src/web-ui/src/flow_chat/utils/tokenUsageDisplay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ describe('tokenUsageDisplay', () => {
it('mirrors the runtime compression trigger budget', () => {
expect(getCompressionTriggerTokens(128_000)).toBe(86_000);
expect(getCompressionTriggerTokens(128_000, 16_000)).toBe(102_000);
expect(getCompressionTriggerTokens(256_000)).toBe(214_000);
expect(getCompressionTriggerTokens(256_000, 64_000)).toBe(182_000);
expect(getCompressionTriggerTokens(399_999)).toBe(357_999);
expect(getCompressionTriggerTokens(400_000)).toBe(326_000);
expect(getCompressionTriggerTokens(1_000_000)).toBe(926_000);
});

Expand Down
11 changes: 2 additions & 9 deletions src/web-ui/src/flow_chat/utils/tokenUsageDisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { formatDuration } from '@/shared/utils/format';
import type { DialogTurn, Session, TokenUsage } from '../types/flow-chat';
import { LONG_CONTEXT_WARNING_THRESHOLD_TOKENS } from '@/shared/constants/modelContext';
import { i18nService } from '@/infrastructure/i18n';
import { automaticMaxOutputTokens } from '@/shared/utils/modelOutputTokens';
import { formatTokenCount } from '@/shared/utils/tokenUsageFormatting';

export const DEFAULT_MAX_CONTEXT_TOKENS = 128128;
Expand Down Expand Up @@ -35,7 +36,6 @@ export interface ModelSelectorTooltipDetails {
warning?: string;
}

const AUTOMATIC_MAX_OUTPUT_TOKEN_TIERS = [8_000, 16_000, 24_000, 32_000, 64_000] as const;
const AUTO_COMPRESSION_SAFETY_RESERVE_TOKENS = 10_000;

export function formatCompactTokenCount(value: number): string {
Expand All @@ -45,14 +45,7 @@ export function formatCompactTokenCount(value: number): string {
);
}

function automaticMaxOutputTokens(contextWindow: number): number {
const quarterContext = Math.floor(contextWindow / 4);
return [...AUTOMATIC_MAX_OUTPUT_TOKEN_TIERS]
.reverse()
.find(tier => tier <= quarterContext) ?? quarterContext;
}

/** Mirrors the runtime compression budget in execution_engine.rs. */
/** Mirrors the runtime compression budget in context_budget.rs. */
export function getCompressionTriggerTokens(
contextWindow: number,
configuredMaxOutputTokens?: number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ describe('backup channels', () => {
expect(isEligibleBackup(model('legacy', { context_window: undefined }), model('small', { context_window: 32_000 }))).toBe(false);
});

it('uses the host’s 400K context gate for the automatic 64K output tier', () => {
const primary = model('primary', { context_window: 256_000, max_tokens: 32_000 });
expect(isEligibleBackup(primary, model('backup', { context_window: 256_000 }))).toBe(true);
});

it('stores empty routes as absent and drops stale manual choices', () => {
expect(normalizeChannelRouting({ version: 1, fallback_model_ids: [] })).toBeUndefined();
expect(normalizeChannelRouting({ version: 1, fallback_model_ids: ['b'], manual_channel: 'b' }))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { automaticMaxOutputTokens } from '@/shared/utils/modelOutputTokens';
import type {
AIModelConfig,
AuthConfig,
Expand Down Expand Up @@ -56,10 +57,9 @@ export function newAccountSlotId(provider: SubscriptionProvider): string {
function channelInputCapacity(model: Pick<AIModelConfig, 'context_window' | 'max_tokens'>): number {
const context = model.context_window ?? 128_128;
const configured = model.max_tokens;
const quarter = Math.floor(context / 4);
const output = configured && configured > 0 && configured * 100 <= context * 40
? configured
: [64_000, 32_000, 24_000, 16_000, 8_000].find(tier => tier <= quarter) ?? quarter;
: automaticMaxOutputTokens(context);
return Math.max(0, context - output);
}

Expand Down
10 changes: 10 additions & 0 deletions src/web-ui/src/shared/utils/modelOutputTokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const AUTOMATIC_MAX_OUTPUT_TOKEN_TIERS = [64_000, 32_000, 24_000, 16_000, 8_000] as const;
const MAX_OUTPUT_64K_MIN_CONTEXT_WINDOW = 400_000;

/** Mirrors automatic_max_output_tokens in config-contracts. */
export function automaticMaxOutputTokens(contextWindow: number): number {
const quarterContext = Math.floor(contextWindow / 4);
return AUTOMATIC_MAX_OUTPUT_TOKEN_TIERS.find(tier =>
tier <= quarterContext && (tier !== 64_000 || contextWindow >= MAX_OUTPUT_64K_MIN_CONTEXT_WINDOW),
) ?? quarterContext;
}
Loading