@@ -105,18 +105,29 @@ export namespace ProviderTransform {
105105 return result
106106 }
107107
108- export function maxOutputTokens ( providerID : string , outputLimit : number , options : Record < string , any > ) : number {
108+ export function maxOutputTokens (
109+ providerID : string ,
110+ options : Record < string , any > ,
111+ modelLimit : number ,
112+ globalLimit : number ,
113+ ) : number {
114+ const modelCap = modelLimit || globalLimit
115+ const standardLimit = Math . min ( modelCap , globalLimit )
116+
109117 if ( providerID === "anthropic" ) {
110- const thinking = options [ "thinking" ]
111- if ( typeof thinking === "object" && thinking !== null ) {
112- const type = thinking [ "type" ]
113- const budgetTokens = thinking [ "budgetTokens" ]
114- if ( type === "enabled" && typeof budgetTokens === "number" && budgetTokens > 0 ) {
115- return outputLimit - budgetTokens
118+ const thinking = options ?. [ "thinking" ]
119+ const budgetTokens = typeof thinking ?. [ "budgetTokens" ] === "number" ? thinking [ "budgetTokens" ] : 0
120+ const enabled = thinking ?. [ "type" ] === "enabled"
121+ if ( enabled && budgetTokens > 0 ) {
122+ // Return text tokens so that text + thinking <= model cap, preferring 32k text when possible.
123+ if ( budgetTokens + standardLimit <= modelCap ) {
124+ return standardLimit
116125 }
126+ return modelCap - budgetTokens
117127 }
118128 }
119- return outputLimit
129+
130+ return standardLimit
120131 }
121132
122133 export function schema ( _providerID : string , _modelID : string , schema : JSONSchema . BaseSchema ) {
0 commit comments