Raise Anthropic default max tokens
This commit is contained in:
@@ -28,6 +28,45 @@ import type { ChatMessage } from "../types.js";
|
||||
const INTERNAL_CORRECTION =
|
||||
"Internal correction: the previous assistant message claimed it would run a tool, but no tool call was made. If the task needs an available tool, call it now. Otherwise provide the final answer directly without saying you will run a tool.";
|
||||
|
||||
const DEFAULT_ANTHROPIC_MAX_TOKENS = 128_000;
|
||||
const MODEL_MAX_TOKENS_CACHE_MS = 24 * 60 * 60 * 1000;
|
||||
|
||||
const modelMaxTokensCache = new Map<string, { maxTokens: number; expiresAt: number }>();
|
||||
|
||||
function readMaxTokens(value: unknown) {
|
||||
return Number.isSafeInteger(value) && (value as number) > 0 ? (value as number) : undefined;
|
||||
}
|
||||
|
||||
function getModelInfoMaxTokens(modelInfo: any) {
|
||||
return readMaxTokens(modelInfo?.max_tokens) ?? readMaxTokens(modelInfo?.maxTokens);
|
||||
}
|
||||
|
||||
async function getMessagesMaxTokens(params: ToolAwareCompletionParams) {
|
||||
if (params.maxTokens) return params.maxTokens;
|
||||
|
||||
const cached = modelMaxTokensCache.get(params.model);
|
||||
if (cached && cached.expiresAt > Date.now()) return cached.maxTokens;
|
||||
|
||||
try {
|
||||
const retrieve = params.client?.models?.retrieve;
|
||||
if (typeof retrieve === "function") {
|
||||
const modelInfo = await retrieve.call(params.client.models, params.model);
|
||||
const maxTokens = getModelInfoMaxTokens(modelInfo);
|
||||
if (maxTokens) {
|
||||
modelMaxTokensCache.set(params.model, {
|
||||
maxTokens,
|
||||
expiresAt: Date.now() + MODEL_MAX_TOKENS_CACHE_MS,
|
||||
});
|
||||
return maxTokens;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Fall back to the documented max for Claude Opus 4.8 and related high-output models.
|
||||
}
|
||||
|
||||
return DEFAULT_ANTHROPIC_MAX_TOKENS;
|
||||
}
|
||||
|
||||
function toTools(tools: any[]) {
|
||||
return tools
|
||||
.map((tool) => {
|
||||
@@ -160,11 +199,12 @@ function mergeUsage(acc: Required<ToolAwareUsage>, usage: any) {
|
||||
|
||||
export async function completeWithMessagesApi(params: ToolAwareCompletionParams): Promise<ToolAwareCompletionResult> {
|
||||
const enabledTools = getEnabledChatTools(params);
|
||||
const maxTokens = await getMessagesMaxTokens(params);
|
||||
if (!enabledTools.length) {
|
||||
const response = await params.client.messages.create({
|
||||
model: params.model,
|
||||
system: buildTopLevelSystemPrompt(params.messages, params.userLocation),
|
||||
max_tokens: params.maxTokens ?? 1024,
|
||||
max_tokens: maxTokens,
|
||||
temperature: params.temperature,
|
||||
messages: buildBaseMessages(params),
|
||||
} as any);
|
||||
@@ -192,7 +232,7 @@ export async function completeWithMessagesApi(params: ToolAwareCompletionParams)
|
||||
const response = await params.client.messages.create({
|
||||
model: params.model,
|
||||
system: buildTopLevelSystemPrompt(params.messages, params.userLocation, buildChatToolSystemPrompt(params)),
|
||||
max_tokens: params.maxTokens ?? 1024,
|
||||
max_tokens: maxTokens,
|
||||
temperature: params.temperature,
|
||||
messages: conversation,
|
||||
tools: toTools(enabledTools),
|
||||
@@ -248,6 +288,7 @@ export async function completeWithMessagesApi(params: ToolAwareCompletionParams)
|
||||
|
||||
export async function* streamWithMessagesApi(params: ToolAwareCompletionParams): AsyncGenerator<ToolAwareStreamingEvent> {
|
||||
const enabledTools = getEnabledChatTools(params);
|
||||
const maxTokens = await getMessagesMaxTokens(params);
|
||||
if (!enabledTools.length) {
|
||||
const rawResponses: unknown[] = [];
|
||||
const usageAcc: Required<ToolAwareUsage> = { inputTokens: 0, outputTokens: 0, totalTokens: 0 };
|
||||
@@ -259,7 +300,7 @@ export async function* streamWithMessagesApi(params: ToolAwareCompletionParams):
|
||||
const stream = await params.client.messages.create({
|
||||
model: params.model,
|
||||
system: buildTopLevelSystemPrompt(params.messages, params.userLocation),
|
||||
max_tokens: params.maxTokens ?? 1024,
|
||||
max_tokens: maxTokens,
|
||||
temperature: params.temperature,
|
||||
messages: buildBaseMessages(params),
|
||||
stream: true,
|
||||
@@ -315,7 +356,7 @@ export async function* streamWithMessagesApi(params: ToolAwareCompletionParams):
|
||||
const stream = await params.client.messages.create({
|
||||
model: params.model,
|
||||
system: buildTopLevelSystemPrompt(params.messages, params.userLocation, buildChatToolSystemPrompt(params)),
|
||||
max_tokens: params.maxTokens ?? 1024,
|
||||
max_tokens: maxTokens,
|
||||
temperature: params.temperature,
|
||||
messages: conversation,
|
||||
tools: toTools(enabledTools),
|
||||
|
||||
Reference in New Issue
Block a user