Skip to content

Commit dbe53f4

Browse files
committed
fix(max-tokens): anthropic models streaming vs non-streaming
1 parent be2a9ef commit dbe53f4

File tree

6 files changed

+79
-4
lines changed

6 files changed

+79
-4
lines changed

apps/sim/blocks/blocks/agent.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,12 @@ Return ONLY the JSON array.`,
513513
})(),
514514
}),
515515
},
516+
{
517+
id: 'maxTokens',
518+
title: 'Max Output Tokens',
519+
type: 'short-input',
520+
placeholder: 'Enter max tokens (e.g., 4096)...',
521+
},
516522
{
517523
id: 'responseFormat',
518524
title: 'Response Format',
@@ -754,6 +760,7 @@ Example 3 (Array Input):
754760
},
755761
},
756762
temperature: { type: 'number', description: 'Response randomness level' },
763+
maxTokens: { type: 'number', description: 'Maximum number of tokens in the response' },
757764
reasoningEffort: { type: 'string', description: 'Reasoning effort level for GPT-5 models' },
758765
verbosity: { type: 'string', description: 'Verbosity level for GPT-5 models' },
759766
thinkingLevel: { type: 'string', description: 'Thinking level for Gemini 3 models' },

apps/sim/providers/anthropic/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
generateToolUseId,
1010
} from '@/providers/anthropic/utils'
1111
import {
12+
getMaxOutputTokensForModel,
1213
getProviderDefaultModel,
1314
getProviderModels,
1415
supportsNativeStructuredOutputs,
@@ -178,7 +179,9 @@ export const anthropicProvider: ProviderConfig = {
178179
model: request.model,
179180
messages,
180181
system: systemPrompt,
181-
max_tokens: Number.parseInt(String(request.maxTokens)) || 1024,
182+
max_tokens:
183+
Number.parseInt(String(request.maxTokens)) ||
184+
getMaxOutputTokensForModel(request.model, request.stream ?? false),
182185
temperature: Number.parseFloat(String(request.temperature ?? 0.7)),
183186
}
184187

apps/sim/providers/bedrock/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ import {
2020
generateToolUseId,
2121
getBedrockInferenceProfileId,
2222
} from '@/providers/bedrock/utils'
23-
import { getProviderDefaultModel, getProviderModels } from '@/providers/models'
23+
import {
24+
getMaxOutputTokensForModel,
25+
getProviderDefaultModel,
26+
getProviderModels,
27+
} from '@/providers/models'
2428
import type {
2529
ProviderConfig,
2630
ProviderRequest,
@@ -259,7 +263,9 @@ export const bedrockProvider: ProviderConfig = {
259263

260264
const inferenceConfig = {
261265
temperature: Number.parseFloat(String(request.temperature ?? 0.7)),
262-
maxTokens: Number.parseInt(String(request.maxTokens)) || 4096,
266+
maxTokens:
267+
Number.parseInt(String(request.maxTokens)) ||
268+
getMaxOutputTokensForModel(request.model, request.stream ?? false),
263269
}
264270

265271
const shouldStreamToolCalls = request.streamToolCalls ?? false

apps/sim/providers/models.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ export interface ModelCapabilities {
3434
toolUsageControl?: boolean
3535
computerUse?: boolean
3636
nativeStructuredOutputs?: boolean
37+
maxOutputTokens?: {
38+
/** Maximum tokens for streaming requests */
39+
max: number
40+
/** Safe default for non-streaming requests (to avoid timeout issues) */
41+
default: number
42+
}
3743
reasoningEffort?: {
3844
values: string[]
3945
}
@@ -613,6 +619,7 @@ export const PROVIDER_DEFINITIONS: Record<string, ProviderDefinition> = {
613619
capabilities: {
614620
temperature: { min: 0, max: 1 },
615621
nativeStructuredOutputs: true,
622+
maxOutputTokens: { max: 64000, default: 4096 },
616623
},
617624
contextWindow: 200000,
618625
},
@@ -627,6 +634,7 @@ export const PROVIDER_DEFINITIONS: Record<string, ProviderDefinition> = {
627634
capabilities: {
628635
temperature: { min: 0, max: 1 },
629636
nativeStructuredOutputs: true,
637+
maxOutputTokens: { max: 64000, default: 4096 },
630638
},
631639
contextWindow: 200000,
632640
},
@@ -640,6 +648,7 @@ export const PROVIDER_DEFINITIONS: Record<string, ProviderDefinition> = {
640648
},
641649
capabilities: {
642650
temperature: { min: 0, max: 1 },
651+
maxOutputTokens: { max: 64000, default: 4096 },
643652
},
644653
contextWindow: 200000,
645654
},
@@ -654,6 +663,7 @@ export const PROVIDER_DEFINITIONS: Record<string, ProviderDefinition> = {
654663
capabilities: {
655664
temperature: { min: 0, max: 1 },
656665
nativeStructuredOutputs: true,
666+
maxOutputTokens: { max: 64000, default: 4096 },
657667
},
658668
contextWindow: 200000,
659669
},
@@ -668,6 +678,7 @@ export const PROVIDER_DEFINITIONS: Record<string, ProviderDefinition> = {
668678
capabilities: {
669679
temperature: { min: 0, max: 1 },
670680
nativeStructuredOutputs: true,
681+
maxOutputTokens: { max: 64000, default: 4096 },
671682
},
672683
contextWindow: 200000,
673684
},
@@ -681,6 +692,7 @@ export const PROVIDER_DEFINITIONS: Record<string, ProviderDefinition> = {
681692
},
682693
capabilities: {
683694
temperature: { min: 0, max: 1 },
695+
maxOutputTokens: { max: 64000, default: 4096 },
684696
},
685697
contextWindow: 200000,
686698
},
@@ -695,6 +707,7 @@ export const PROVIDER_DEFINITIONS: Record<string, ProviderDefinition> = {
695707
capabilities: {
696708
temperature: { min: 0, max: 1 },
697709
computerUse: true,
710+
maxOutputTokens: { max: 8192, default: 8192 },
698711
},
699712
contextWindow: 200000,
700713
},
@@ -709,6 +722,7 @@ export const PROVIDER_DEFINITIONS: Record<string, ProviderDefinition> = {
709722
capabilities: {
710723
temperature: { min: 0, max: 1 },
711724
computerUse: true,
725+
maxOutputTokens: { max: 8192, default: 8192 },
712726
},
713727
contextWindow: 200000,
714728
},
@@ -1655,6 +1669,7 @@ export const PROVIDER_DEFINITIONS: Record<string, ProviderDefinition> = {
16551669
capabilities: {
16561670
temperature: { min: 0, max: 1 },
16571671
nativeStructuredOutputs: true,
1672+
maxOutputTokens: { max: 64000, default: 4096 },
16581673
},
16591674
contextWindow: 200000,
16601675
},
@@ -1668,6 +1683,7 @@ export const PROVIDER_DEFINITIONS: Record<string, ProviderDefinition> = {
16681683
capabilities: {
16691684
temperature: { min: 0, max: 1 },
16701685
nativeStructuredOutputs: true,
1686+
maxOutputTokens: { max: 64000, default: 4096 },
16711687
},
16721688
contextWindow: 200000,
16731689
},
@@ -1681,6 +1697,7 @@ export const PROVIDER_DEFINITIONS: Record<string, ProviderDefinition> = {
16811697
capabilities: {
16821698
temperature: { min: 0, max: 1 },
16831699
nativeStructuredOutputs: true,
1700+
maxOutputTokens: { max: 64000, default: 4096 },
16841701
},
16851702
contextWindow: 200000,
16861703
},
@@ -1694,6 +1711,7 @@ export const PROVIDER_DEFINITIONS: Record<string, ProviderDefinition> = {
16941711
capabilities: {
16951712
temperature: { min: 0, max: 1 },
16961713
nativeStructuredOutputs: true,
1714+
maxOutputTokens: { max: 64000, default: 4096 },
16971715
},
16981716
contextWindow: 200000,
16991717
},
@@ -2333,3 +2351,31 @@ export function getThinkingLevelsForModel(modelId: string): string[] | null {
23332351
const capability = getThinkingCapability(modelId)
23342352
return capability?.levels ?? null
23352353
}
2354+
2355+
/**
2356+
* Get the max output tokens for a specific model
2357+
* Returns the model's max capacity for streaming requests,
2358+
* or the model's safe default for non-streaming requests to avoid timeout issues.
2359+
*
2360+
* @param modelId - The model ID
2361+
* @param streaming - Whether the request is streaming (default: false)
2362+
*/
2363+
export function getMaxOutputTokensForModel(modelId: string, streaming = false): number {
2364+
const normalizedModelId = modelId.toLowerCase()
2365+
const STANDARD_MAX_OUTPUT_TOKENS = 4096
2366+
2367+
for (const provider of Object.values(PROVIDER_DEFINITIONS)) {
2368+
for (const model of provider.models) {
2369+
const baseModelId = model.id.toLowerCase()
2370+
if (normalizedModelId === baseModelId || normalizedModelId.startsWith(`${baseModelId}-`)) {
2371+
const outputTokens = model.capabilities.maxOutputTokens
2372+
if (outputTokens) {
2373+
return streaming ? outputTokens.max : outputTokens.default
2374+
}
2375+
return STANDARD_MAX_OUTPUT_TOKENS
2376+
}
2377+
}
2378+
}
2379+
2380+
return STANDARD_MAX_OUTPUT_TOKENS
2381+
}

apps/sim/providers/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
getComputerUseModels,
99
getEmbeddingModelPricing,
1010
getHostedModels as getHostedModelsFromDefinitions,
11+
getMaxOutputTokensForModel as getMaxOutputTokensForModelFromDefinitions,
1112
getMaxTemperature as getMaxTempFromDefinitions,
1213
getModelPricing as getModelPricingFromDefinitions,
1314
getModelsWithReasoningEffort,
@@ -992,6 +993,18 @@ export function getThinkingLevelsForModel(model: string): string[] | null {
992993
return getThinkingLevelsForModelFromDefinitions(model)
993994
}
994995

996+
/**
997+
* Get max output tokens for a specific model
998+
* Returns the model's maxOutputTokens capability for streaming requests,
999+
* or a conservative default (8192) for non-streaming requests to avoid timeout issues.
1000+
*
1001+
* @param model - The model ID
1002+
* @param streaming - Whether the request is streaming (default: false)
1003+
*/
1004+
export function getMaxOutputTokensForModel(model: string, streaming = false): number {
1005+
return getMaxOutputTokensForModelFromDefinitions(model, streaming)
1006+
}
1007+
9951008
/**
9961009
* Prepare tool execution parameters, separating tool parameters from system parameters
9971010
*/

apps/sim/tools/browser_use/run_task.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { ToolConfig, ToolResponse } from '@/tools/types'
55
const logger = createLogger('BrowserUseTool')
66

77
const POLL_INTERVAL_MS = 5000
8-
const MAX_POLL_TIME_MS = 180000
8+
const MAX_POLL_TIME_MS = 600000 // 10 minutes
99
const MAX_CONSECUTIVE_ERRORS = 3
1010

1111
async function createSessionWithProfile(

0 commit comments

Comments
 (0)