@@ -4764,6 +4764,12 @@ describe("output token budget", () => {
47644764 expect ( clampOutputTokens ( { model, requested : 16_384 , inputTokens : 60_000 } ) ) . toBe ( 16_384 )
47654765 } )
47664766
4767+ test ( "never demands more headroom than the model's own output reservation" , ( ) => {
4768+ const model = createWindowModel ( { context : 8_192 , output : 512 } )
4769+ expect ( clampOutputTokens ( { model, requested : 512 , inputTokens : 7_169 } ) ) . toBe ( 512 )
4770+ expect ( ( ) => clampOutputTokens ( { model, requested : 512 , inputTokens : 8_000 } ) ) . toThrow ( OutputTokenBudgetError )
4771+ } )
4772+
47674773 test ( "does not clamp a window too small to hold even a floor-sized completion" , ( ) => {
47684774 // A declared window this small is a placeholder or a test fixture, not a real limit. Failing
47694775 // the request client-side on numbers we do not believe would be worse than letting the
@@ -5066,6 +5072,18 @@ describe("output token budget", () => {
50665072 headerSources : [ { aiGatewayHeaders : { "anthropic-beta" : "context-1m-2025-08-07" } } ] ,
50675073 } ) ,
50685074 ) . toBe ( 1_000_000 )
5075+ expect (
5076+ effectiveContextWindow ( {
5077+ model,
5078+ headerSources : [ { "Anthropic-Beta" : "context-1m-2025-08-07" } ] ,
5079+ } ) ,
5080+ ) . toBe ( 1_000_000 )
5081+ expect (
5082+ effectiveContextWindow ( {
5083+ model,
5084+ headerSources : [ { aiGatewayHeaders : { "anthropic-beta" : "interleaved-thinking-2025-05-14" } } ] ,
5085+ } ) ,
5086+ ) . toBe ( 200_000 )
50695087 } )
50705088
50715089 test ( "clamps fixed reasoning budgets with the output reservation without mutating inputs" , ( ) => {
@@ -5081,6 +5099,28 @@ describe("output token budget", () => {
50815099 expect ( options . thinking . budgetTokens ) . toBe ( 16_000 )
50825100 } )
50835101
5102+ test ( "preserves non-JSON provider options while lowering reasoning budgets" , ( ) => {
5103+ const callback = ( ) => undefined
5104+ const options = {
5105+ thinking : { budgetTokens : 31_999 } ,
5106+ keepUndefined : undefined ,
5107+ keepFunction : callback ,
5108+ keepBigInt : 10n ,
5109+ nested : [ { thinkingBudget : 31_999 } , { untouched : "value" } ] ,
5110+ }
5111+ const result = clampReasoningBudget ( options , 8_192 )
5112+ expect ( result . thinking . budgetTokens ) . toBe ( 7_168 )
5113+ expect ( result . nested [ 0 ] ! . thinkingBudget ) . toBe ( 7_168 )
5114+ expect ( result . nested [ 1 ] ! . untouched ) . toBe ( "value" )
5115+ expect ( "keepUndefined" in result ) . toBeTrue ( )
5116+ expect ( result . keepFunction ) . toBe ( callback )
5117+ expect ( result . keepBigInt ) . toBe ( 10n )
5118+ expect ( options . thinking . budgetTokens ) . toBe ( 31_999 )
5119+
5120+ const unchanged = { thinking : { budgetTokens : 512 } }
5121+ expect ( clampReasoningBudget ( unchanged , 8_192 ) ) . toBe ( unchanged )
5122+ } )
5123+
50845124 test ( "fails clearly when reasoning and visible output cannot both fit" , ( ) => {
50855125 expect ( ( ) => clampReasoningBudget ( { thinking : { budgetTokens : 16_000 } } , 1_500 ) ) . toThrow (
50865126 / c a n n o t p r e s e r v e t h e c o n f i g u r e d r e a s o n i n g b u d g e t / ,
0 commit comments