Skip to content

fix(agent-core): only send prompt_cache_key to official OpenAI endpoints#2203

Open
B143KC47 wants to merge 3 commits into
MoonshotAI:mainfrom
B143KC47:fix/openai-prompt-cache-key-custom-endpoints
Open

fix(agent-core): only send prompt_cache_key to official OpenAI endpoints#2203
B143KC47 wants to merge 3 commits into
MoonshotAI:mainfrom
B143KC47:fix/openai-prompt-cache-key-custom-endpoints

Conversation

@B143KC47

Copy link
Copy Markdown

Related Issue

Resolve #2166

Problem

Since v0.29.0 (#1970), every OpenAI-compatible provider receives the session prompt_cache_key in the request body. Strictly-validating endpoints (e.g. NVIDIA NIM) reject the unknown parameter with 400 Unsupported parameter(s): prompt_cache_key, which makes custom openai / openai_responses providers unusable.

What changed

The field is now only sent when the effective base URL targets the official OpenAI API (api.openai.com or a regional variant like eu.api.openai.com, or no base URL, which the client defaults there):

  • v1 engine: toKosongProviderConfig gates prompt_cache_key in both the openai and openai_responses branches on a new isOfficialOpenAIBaseUrl helper. The kimi branch is intentionally untouched — Kimi endpoints support the field.
  • v2 engine (kimi web / experimental print mode): the same gate on the bare fallback in the OpenAI chat-completions and Responses bases. Vendors that support the field keep encoding it through their cacheKey trait hook (e.g. Kimi), which is unaffected.

This restores pre-0.29.0 behavior for custom endpoints while preserving #1970's cache-affinity intent for the official API. Trade-off, stated explicitly: OpenAI-compatible gateways on custom base URLs that do accept prompt_cache_key lose cache affinity again — exactly the pre-0.29.0 behavior; a per-provider opt-in flag could cover them as a follow-up if wanted.

Tests: runtime-provider.test.ts gains custom-endpoint (omitted) and official-endpoint (kept) cases for both provider types; composition.test.ts gains the same probes on the v2 wire. All four fail without the fix.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

B143KC47 added 2 commits July 26, 2026 17:01
…dpoint

Since 0.29.0 every OpenAI-compatible provider received the session
prompt_cache_key in the request body. Strictly-validating endpoints
reject the unknown parameter with a 400, breaking custom providers.

Gate the field on the effective base URL targeting api.openai.com (or
being unset, which the client defaults there), in both the v1 provider
config resolution and the v2 OpenAI chat-completions/responses bases.
Vendors that support the field (e.g. Kimi) keep encoding it through
their own branch or cacheKey trait hook.

Fixes MoonshotAI#2166
…pt cache affinity

Data-residency endpoints (eu.api.openai.com, us.api.openai.com) are
official OpenAI hosts and accept prompt_cache_key; match any
*.api.openai.com hostname instead of the apex only.
Copilot AI review requested due to automatic review settings July 26, 2026 09:39
@changeset-bot

changeset-bot Bot commented Jul 26, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0f89f11

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a6909f91e6

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +266 to +270
/**
* `prompt_cache_key` is an official-OpenAI request field: strictly-validating
* OpenAI-compatible endpoints reject unknown parameters with a 400, so the
* bases only fall back to it when the effective base URL targets
* `api.openai.com` or a regional variant like `eu.api.openai.com` (or is

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Move the helper documentation to the module header

This function-level JSDoc violates the package convention that comments may appear only in the top-of-file block. Move the helper’s externally relevant responsibility into the existing module header and remove this local narration.

AGENTS.md reference: packages/agent-core-v2/AGENTS.md:L11-L13

Useful? React with 👍 / 👎.

Comment on lines +711 to +713
// The bare `prompt_cache_key` fallback is official-OpenAI only:
// strictly-validating compatible endpoints 400 on unknown fields
// (#2166); vendors that support it encode it via their cacheKey hook.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Remove the statement-level cache narration

These inline comments narrate the branch beside a statement, contrary to this package’s header-only comment convention. Keep any externally relevant responsibility in the file header and let the conditional express the implementation.

AGENTS.md reference: packages/agent-core-v2/AGENTS.md:L11-L13

Useful? React with 👍 / 👎.

Comment on lines +1063 to +1065
// Per-turn intent overlays in the fixed contract order. The
// `prompt_cache_key` overlay is official-OpenAI only: strictly-validating
// compatible endpoints reject unknown fields with a 400 (#2166).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Move the request-overlay explanation to the header

This statement-adjacent explanation violates the scoped rule that comments live solely in the top-of-file block. Incorporate the externally relevant endpoint responsibility into that header and remove the inline narration.

AGENTS.md reference: packages/agent-core-v2/AGENTS.md:L11-L13

Useful? React with 👍 / 👎.

Comment on lines +704 to +706
// Strictly-validating OpenAI-compatible servers reject the unknown
// `prompt_cache_key` field with a 400, so the bare fallback stays
// official-endpoint only (#2166).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Remove the in-test implementation commentary

The new explanatory comment is inside the test body, but the package convention permits comments only in the top-of-file scenario header. Move any necessary scenario responsibility to that existing header and keep the test’s setup and assertions self-explanatory.

AGENTS.md reference: packages/agent-core-v2/AGENTS.md:L11-L13

Useful? React with 👍 / 👎.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a regression where prompt_cache_key was being sent to all OpenAI-compatible endpoints, causing strict third-party providers (e.g. NVIDIA NIM) to reject requests with 400 Unsupported parameter(s): prompt_cache_key. The change scopes prompt_cache_key emission to official OpenAI base URLs (or unset base URL), restoring compatibility for custom endpoints while keeping session cache affinity for the official API.

Changes:

  • Gate prompt_cache_key behind an isOfficialOpenAIBaseUrl check for v1 openai and openai_responses provider config generation.
  • Apply the same official-endpoint-only fallback behavior in v2 OpenAI Chat Completions and Responses bases.
  • Add tests covering both “custom endpoint omits” and “official endpoint keeps” behavior for v1 and v2.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
packages/agent-core/test/harness/runtime-provider.test.ts Adds v1 regression tests ensuring prompt_cache_key is omitted for custom base URLs and kept for official OpenAI URLs.
packages/agent-core/src/session/provider-manager.ts Gates prompt_cache_key in OpenAI provider configs using a new isOfficialOpenAIBaseUrl helper.
packages/agent-core-v2/test/kosong/provider/composition.test.ts Adds v2 behavior probes verifying omission on custom endpoints and retention on official OpenAI endpoints.
packages/agent-core-v2/src/kosong/provider/bases/openai/openai-responses.ts Gates the per-turn prompt_cache_key overlay to official OpenAI base URLs.
packages/agent-core-v2/src/kosong/provider/bases/openai/openai-legacy.ts Makes the base fallback prompt_cache_key overlay official-endpoint-only while preserving vendor cacheKey hook behavior.
packages/agent-core-v2/src/kosong/provider/bases/openai/openai-common.ts Introduces shared isOfficialOpenAIBaseUrl helper for v2 OpenAI bases.
.changeset/prompt-cache-key-official-openai-only.md Adds a patch changeset for the published CLI package describing the user-visible fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

The package convention allows comments only in the top-of-file block;
move the prompt_cache_key gating notes there.
@B143KC47

Copy link
Copy Markdown
Author

@codex review — the comment-convention findings are addressed in 0f89f11 (commentary moved into the module headers).

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 0f89f116de

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error with models not supporting prompt cache

2 participants