@@ -137,3 +137,85 @@ func TestCloneWithOptions_DirectProvider(t *testing.T) {
137137 assert .Nil (t , clonedConfig .ModelConfig .ThinkingBudget ,
138138 "ThinkingBudget should be nil after cloning with WithThinking(false)" )
139139}
140+
141+ func TestCloneWithOptions_PreservesMaxTokens (t * testing.T ) {
142+ t .Parallel ()
143+
144+ // This test verifies that max_tokens is preserved when cloning a provider
145+ // with options that don't explicitly set max_tokens. Previously, options
146+ // that didn't set max_tokens would accidentally clear it to 0.
147+
148+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
149+ w .Header ().Set ("Content-Type" , "text/event-stream" )
150+ _ , _ = w .Write ([]byte ("data: {\" choices\" :[{\" delta\" :{\" content\" :\" hi\" }}]}\n \n " ))
151+ _ , _ = w .Write ([]byte ("data: [DONE]\n \n " ))
152+ }))
153+ defer server .Close ()
154+
155+ maxTokens := int64 (8192 )
156+ cfg := & latest.ModelConfig {
157+ Provider : "openai" ,
158+ Model : "gpt-4o" ,
159+ BaseURL : server .URL ,
160+ MaxTokens : & maxTokens ,
161+ }
162+
163+ env := newCloneTestEnv (map [string ]string {
164+ "OPENAI_API_KEY" : "test-key" ,
165+ })
166+
167+ provider , err := New (t .Context (), cfg , env , options .WithMaxTokens (maxTokens ))
168+ require .NoError (t , err )
169+
170+ // Clone with an option that doesn't affect max_tokens (e.g., WithThinking)
171+ cloned := CloneWithOptions (t .Context (), provider , options .WithThinking (false ))
172+
173+ clonedConfig := cloned .BaseConfig ()
174+
175+ // MaxTokens should be preserved, not cleared to 0 or nil
176+ require .NotNil (t , clonedConfig .ModelConfig .MaxTokens ,
177+ "MaxTokens should be preserved after cloning with unrelated options" )
178+ assert .Equal (t , maxTokens , * clonedConfig .ModelConfig .MaxTokens ,
179+ "MaxTokens value should be unchanged after cloning" )
180+ }
181+
182+ func TestCloneWithOptions_OverridesMaxTokens (t * testing.T ) {
183+ t .Parallel ()
184+
185+ // This test verifies that max_tokens can be explicitly overridden when cloning.
186+
187+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
188+ w .Header ().Set ("Content-Type" , "text/event-stream" )
189+ _ , _ = w .Write ([]byte ("data: {\" choices\" :[{\" delta\" :{\" content\" :\" hi\" }}]}\n \n " ))
190+ _ , _ = w .Write ([]byte ("data: [DONE]\n \n " ))
191+ }))
192+ defer server .Close ()
193+
194+ originalMaxTokens := int64 (8192 )
195+ newMaxTokens := int64 (4096 )
196+
197+ cfg := & latest.ModelConfig {
198+ Provider : "openai" ,
199+ Model : "gpt-4o" ,
200+ BaseURL : server .URL ,
201+ MaxTokens : & originalMaxTokens ,
202+ }
203+
204+ env := newCloneTestEnv (map [string ]string {
205+ "OPENAI_API_KEY" : "test-key" ,
206+ })
207+
208+ provider , err := New (t .Context (), cfg , env , options .WithMaxTokens (originalMaxTokens ))
209+ require .NoError (t , err )
210+
211+ // Clone with an explicit max_tokens override
212+ cloned := CloneWithOptions (t .Context (), provider , options .WithMaxTokens (newMaxTokens ))
213+
214+ clonedConfig := cloned .BaseConfig ()
215+
216+ // MaxTokens should be updated to the new value
217+ require .NotNil (t , clonedConfig .ModelConfig .MaxTokens ,
218+ "MaxTokens should not be nil after cloning with explicit override" )
219+ assert .Equal (t , newMaxTokens , * clonedConfig .ModelConfig .MaxTokens ,
220+ "MaxTokens should be updated to the new value" )
221+ }
0 commit comments