fix(ai): clamp streamSimple max tokens
Clamps streamSimple max-token defaults against estimated context, addressing #5595. closes #6061
This commit is contained in:
@@ -36,7 +36,7 @@ import { getProviderEnvValue } from "../utils/provider-env.ts";
|
||||
import { sanitizeSurrogates } from "../utils/sanitize-unicode.ts";
|
||||
|
||||
import { buildCopilotDynamicHeaders, hasCopilotVisionInput } from "./github-copilot-headers.ts";
|
||||
import { adjustMaxTokensForThinking, buildBaseOptions } from "./simple-options.ts";
|
||||
import { adjustMaxTokensForThinking, buildBaseOptions, clampMaxTokensToContext } from "./simple-options.ts";
|
||||
import { transformMessages } from "./transform-messages.ts";
|
||||
|
||||
/**
|
||||
@@ -771,7 +771,7 @@ export const streamSimple: StreamFunction<"anthropic-messages", SimpleStreamOpti
|
||||
): AssistantMessageEventStream => {
|
||||
assertRequestAuth(model.provider, options?.apiKey, options?.headers);
|
||||
|
||||
const base = buildBaseOptions(model, options, options?.apiKey);
|
||||
const base = buildBaseOptions(model, context, options, options?.apiKey);
|
||||
if (!options?.reasoning) {
|
||||
return stream(model, context, { ...base, thinkingEnabled: false } satisfies AnthropicOptions);
|
||||
}
|
||||
@@ -796,11 +796,13 @@ export const streamSimple: StreamFunction<"anthropic-messages", SimpleStreamOpti
|
||||
options.thinkingBudgets,
|
||||
);
|
||||
|
||||
const maxTokens = clampMaxTokensToContext(model, context, adjusted.maxTokens);
|
||||
|
||||
return stream(model, context, {
|
||||
...base,
|
||||
maxTokens: adjusted.maxTokens,
|
||||
maxTokens,
|
||||
thinkingEnabled: true,
|
||||
thinkingBudgetTokens: adjusted.thinkingBudget,
|
||||
thinkingBudgetTokens: Math.min(adjusted.thinkingBudget, Math.max(0, maxTokens - 1024)),
|
||||
} satisfies AnthropicOptions);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user