Skip to content
Open
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/prompt-cache-key-official-openai-only.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@moonshot-ai/agent-core-v2": patch
"@moonshot-ai/kimi-code": patch
---

Stop sending the OpenAI prompt cache key to custom OpenAI-compatible endpoints, which reject the unknown field with a 400 error; the key is still sent to the official OpenAI API.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function isOfficialOpenAIBaseUrl(baseUrl: string | undefined): boolean {
if (baseUrl === undefined) {
return true;
}
try {
const hostname = new URL(baseUrl).hostname;
return hostname === 'api.openai.com' || hostname.endsWith('.api.openai.com');
} catch {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { encodeReasoningEffortFallback } from '#/llm/thinking';
import type { TokenUsage } from '#/llm/usage';

import { isContextOverflowErrorCode, isOpenAIInsufficientQuotaCode } from '../openai/format';
import { isOfficialOpenAIBaseUrl } from '../openai-base-url';
import { lowerMessage, type ResponsesInputItem } from './lower';

type RawObject = Record<string, unknown>;
Expand Down Expand Up @@ -365,7 +366,9 @@ function resolveRequestKwargs(input: FormatRequestInput): Record<string, unknown
} = input;
let kwargs: Record<string, unknown> = {};
if (cacheKey !== undefined) {
kwargs = trait?.cacheKey?.(cacheKey, ctx) ?? { prompt_cache_key: cacheKey };
kwargs =
trait?.cacheKey?.(cacheKey, ctx) ??
(isOfficialOpenAIBaseUrl(ctx.model.baseUrl) ? { prompt_cache_key: cacheKey } : {});
}
if (thinking !== undefined) {
kwargs = applyThinking(kwargs, thinking, trait, ctx, (t) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import type { TokenUsage } from '#/llm/usage';
import { lowerMessage, type OpenAIWireMessage } from './lower';
import { extractToolMedia } from './patterns';
import { DEFAULT_REASONING_KEY, extractReasoning } from './reasoning-key';
import { isOfficialOpenAIBaseUrl } from '../openai-base-url';

function responseFormatToOpenAI(format: ResponseFormat): Record<string, unknown> {
if (format.type === 'json_object') {
Expand Down Expand Up @@ -176,7 +177,9 @@ function resolveRequestKwargs(input: FormatRequestInput): ResolvedRequestKwargs
} = input;
let kwargs: Record<string, unknown> = {};
if (cacheKey !== undefined) {
kwargs = trait?.cacheKey?.(cacheKey, ctx) ?? { prompt_cache_key: cacheKey };
kwargs =
trait?.cacheKey?.(cacheKey, ctx) ??
(isOfficialOpenAIBaseUrl(ctx.model.baseUrl) ? { prompt_cache_key: cacheKey } : {});
}
let preserveThinking = false;
if (thinking !== undefined) {
Expand Down
16 changes: 15 additions & 1 deletion packages/agent-core-v2/src/human/test/llm/cache-key.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const model: LlmModel = {
provider: 'test',
model: 'test-model',
capability: UNKNOWN_CAPABILITY,
baseUrl: 'https://example.test/v1',
baseUrl: 'https://api.openai.com/v1',
};
const messages: readonly Message[] = [createUserMessage('hi')];

Expand Down Expand Up @@ -121,6 +121,20 @@ describe('openai requester cacheKey', () => {
expect(client.body()['extra_body']).toEqual({ trace_id: 't1' });
});

it('omits prompt_cache_key for third-party OpenAI endpoints', async () => {
const client = stubOpenAIClient(chatCompletionChunks);
const requester = createOpenAIRequester(undefined, { clientFactory: client.clientFactory });
await requester.generate(
{
model: { ...model, baseUrl: 'https://integrate.api.nvidia.com/v1' },
cacheKey: 'session-1',
},
{ messages },
{ signal: new AbortController().signal },
);
expect(client.body()['prompt_cache_key']).toBeUndefined();
});

it('lets a trait override the cache key params', async () => {
const client = stubOpenAIClient(chatCompletionChunks);
const requester = createOpenAIRequester(
Expand Down