fix(ai): clamp MiniMax shared budget max tokens

closes #6061
This commit is contained in:
Mario Zechner
2026-06-25 12:00:37 +02:00
parent 8c9dbffa36
commit b940c52e7e
9 changed files with 356 additions and 0 deletions
+16
View File
@@ -29,6 +29,7 @@ import type {
ToolCall,
ToolResultMessage,
} from "../types.ts";
import { estimateContextTokens } from "../utils/estimate.ts";
import { AssistantMessageEventStream } from "../utils/event-stream.ts";
import { headersToRecord } from "../utils/headers.ts";
import { parseJsonWithRepair, parseStreamingJson } from "../utils/json-parse.ts";
@@ -177,9 +178,12 @@ function getAnthropicCompat(
supportsCacheControlOnTools: model.compat?.supportsCacheControlOnTools ?? true,
supportsTemperature: model.compat?.supportsTemperature ?? true,
allowEmptySignature: model.compat?.allowEmptySignature ?? false,
maxTokensSharesContextWindow: model.compat?.maxTokensSharesContextWindow ?? false,
};
}
const SHARED_BUDGET_MIN_OUTPUT_TOKENS = 1024;
export interface AnthropicOptions extends StreamOptions {
/**
* Enable extended thinking.
@@ -998,6 +1002,18 @@ function buildParams(
}
}
if (compat.maxTokensSharesContextWindow && model.contextWindow > 0) {
const estimatedInput = estimateContextTokens(context).tokens;
const available = Math.max(SHARED_BUDGET_MIN_OUTPUT_TOKENS, model.contextWindow - estimatedInput);
const clamped = Math.min(params.max_tokens, available);
if (clamped < params.max_tokens) {
params.max_tokens = clamped;
if (params.thinking?.type === "enabled" && params.thinking.budget_tokens >= clamped) {
params.thinking.budget_tokens = Math.max(0, clamped - SHARED_BUDGET_MIN_OUTPUT_TOKENS);
}
}
}
return params;
}