Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/openai-max-output-size.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@moonshot-ai/agent-core": patch
"@moonshot-ai/kimi-code": patch
---

Fix 400 "Invalid max_tokens value" error when using DeepSeek and other OpenAI-compatible providers with `max_output_size` configured
2 changes: 2 additions & 0 deletions packages/agent-core/src/session/provider-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ function toKosongProviderConfig(
baseUrl: providerValue(provider.baseUrl, provider.env, 'OPENAI_BASE_URL'),
apiKey: providerApiKey(provider),
reasoningKey,
...(maxOutputSize !== undefined ? { maxTokens: maxOutputSize } : {}),
...defaultHeadersField(provider.customHeaders),
};
case 'kimi':
Expand All @@ -259,6 +260,7 @@ function toKosongProviderConfig(
model,
baseUrl: providerValue(provider.baseUrl, provider.env, 'OPENAI_BASE_URL'),
apiKey: providerApiKey(provider),
...(maxOutputSize !== undefined ? { maxOutputTokens: maxOutputSize } : {}),
...defaultHeadersField(provider.customHeaders),
};
case 'vertexai': {
Expand Down
95 changes: 95 additions & 0 deletions packages/agent-core/test/harness/runtime-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,101 @@ describe('resolveRuntimeProvider maxOutputSize forwarding', () => {
});
});

it('forwards alias.maxOutputSize to the openai provider config as maxTokens', () => {
const resolved = resolveRuntimeProvider({
config: {
...BASE_CONFIG,
providers: {
...BASE_CONFIG.providers,
openai: {
type: 'openai',
apiKey: 'sk-openai',
baseUrl: 'https://openai.example/v1',
},
},
models: {
...BASE_CONFIG.models!,
'deepseek-alias': {
provider: 'openai',
model: 'deepseek-chat',
maxContextSize: 1_000_000,
maxOutputSize: 393216,
},
},
},
model: 'deepseek-alias',
});

expect(resolved.provider).toMatchObject({
type: 'openai',
model: 'deepseek-chat',
maxTokens: 393216,
});
});

it('omits maxTokens from the openai provider config when alias.maxOutputSize is unset', () => {
const resolved = resolveRuntimeProvider({
config: {
...BASE_CONFIG,
providers: {
...BASE_CONFIG.providers,
openai: {
type: 'openai',
apiKey: 'sk-openai',
baseUrl: 'https://openai.example/v1',
},
},
models: {
...BASE_CONFIG.models!,
'gpt-alias': {
provider: 'openai',
model: 'gpt-4o',
maxContextSize: 128000,
},
},
},
model: 'gpt-alias',
});

expect(resolved.provider).toMatchObject({
type: 'openai',
model: 'gpt-4o',
});
expect('maxTokens' in resolved.provider).toBe(false);
});

it('forwards alias.maxOutputSize to the openai_responses provider config as maxOutputTokens', () => {
const resolved = resolveRuntimeProvider({
config: {
...BASE_CONFIG,
providers: {
...BASE_CONFIG.providers,
openai_resp: {
type: 'openai_responses',
apiKey: 'sk-openai',
baseUrl: 'https://openai.example/v1',
},
},
models: {
...BASE_CONFIG.models!,
'o3-alias': {
provider: 'openai_resp',
model: 'o3',
maxContextSize: 200000,
maxOutputSize: 100000,
},
},
},
model: 'o3-alias',
});

expect(resolved.provider).toMatchObject({
type: 'openai_responses',
model: 'o3',
maxOutputTokens: 100000,
});
});

it('omits adaptiveThinking when alias.adaptiveThinking is unset', () => {
const resolved = resolveRuntimeProvider({
config: {
Expand Down