Skip to content

Preserve Anthropic system-prompt cache_control on OpenAI→Anthropic path#337

Merged
paultancre-bt merged 6 commits into
mainfrom
paultancre/inf-170-anthropic-system-cache-control
Jul 9, 2026
Merged

Preserve Anthropic system-prompt cache_control on OpenAI→Anthropic path#337
paultancre-bt merged 6 commits into
mainfrom
paultancre/inf-170-anthropic-system-cache-control

Conversation

@paultancre-bt

@paultancre-bt paultancre-bt commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Problem

On the OpenAI-compat /chat/completions path to a Claude model, cache_control on the system message was silently dropped, so Anthropic prompt caching on the system prompt never engaged (cached_tokens: 0). User-message and native /anthropic/v1/messages paths were unaffected.

Approach

request_from_universal (anthropic adapter) flattened leading system messages into a plain String, keeping only t.text. The top-level system param supports an array of text blocks with cache_control, so:

  • If any system text block carries cache_control, emit system as an array of {type,text,cache_control} blocks (reusing anthropic_cache_control_from_universal, now pub(crate)).
  • Otherwise keep the existing string flattening — no behavior change when caching isn't requested.

Tests

  • test_anthropic_system_cache_control_preserved — asserts outbound system is an array whose block carries cache_control.type = "ephemeral". Verified red without the fix, green with it.
  • test_anthropic_system_without_cache_control_stays_string — backward-compat guard.
  • cargo fmt --check + cargo clippy -p lingua clean.

Repro (prod, x-bt-use-cache: never)

Warm cache via native (cache_creation=5012), then read identical prefix via openai-compat system → MISS before fix. Scripts: scripts/repro_pylon_18806_*.py (monorepo).

Linear: INF-170 (https://linear.app/braintrustdata/issue/INF-170)
Pylon: #18806

@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: 9cb39e3063

ℹ️ 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 (@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 (@codex) address that feedback".

Comment on lines +430 to +433
if let UserContentPart::Text(t) = part {
let mut block = Map::new();
block.insert("type".into(), Value::String("text".into()));
block.insert("text".into(), Value::String(t.text));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve system separators when caching is enabled

When a leading system/developer prompt has multiple text parts or multiple leading messages and any text part has cache_control, this path switches away from the old flattening that inserted "\n" between parts and "\n\n" between messages, and instead emits one Anthropic system text block per part with only the raw text. That changes the system prompt only for cached OpenAI→Anthropic requests (for example, two leading instructions that previously became A\n\nB are sent as adjacent blocks with no separator). Add separator text to the generated blocks, or fold it into adjacent blocks while preserving the intended cache boundary.

Useful? React with 👍 / 👎.

The anthropic adapter flattened leading system messages into a plain
string, discarding cache_control breakpoints. Anthropic prompt caching
on the system prompt therefore never engaged via the OpenAI-compat path
(cached_tokens stayed 0), while the native and user-message paths worked.

Emit `system` as an array of text blocks preserving cache_control when a
breakpoint is present; keep the string form otherwise (backward compatible).

Fixes INF-170. Repro: Pylon #18806.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@paultancre-bt paultancre-bt force-pushed the paultancre/inf-170-anthropic-system-cache-control branch from 9cb39e3 to d8d659e Compare July 9, 2026 12:56
@knjiang

Copy link
Copy Markdown
Contributor

do you mind adding a test case in params.ts with a system message including cache_control and then running make capture

paultancre-bt and others added 2 commits July 9, 2026 14:58
Adds chatCompletionsSystemCacheControlParam (system message with a
cache_control text part) plus a ChatCompletionSystemMessageWithCacheControl
type. The chat-completions → anthropic transform snapshot confirms the
system prompt is forwarded as a cache_control-bearing text block array.

Captured with `make capture CASES=chatCompletionsSystemCacheControlParam`
(anthropic only; other providers left uncaptured).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Deserialize into CreateMessageParams and match on System instead of
poking at serde_json::Value, satisfying the typed-boundary CI check.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@paultancre-bt

Copy link
Copy Markdown
Contributor Author

Ken Jiang (@knjiang) only supplied an Anthropic key for the test seems this make test-typescript failing do I need to supply all the keys? (- OPENAI_API_KEY, ANTHROPIC_API_KEY, GOOGLE_API_KEY,AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY) ?

@knjiang

Copy link
Copy Markdown
Contributor

yep all those keys would be nice. this is to make sure we understand our change in the context of all the different model apis

paultancre-bt and others added 3 commits July 9, 2026 16:58
Adds the OpenAI/Bedrock/Google/Responses transform captures and the
chat-completions provider snapshot for chatCompletionsSystemCacheControlParam,
plus the vitest transform snapshots, so the payloads sync coverage check passes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Main's reasoning/content merge for OpenAI chat completions (#331) changed
the chat-completions → responses transform; refresh the case snapshot to match.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@paultancre-bt paultancre-bt merged commit 7f4aef6 into main Jul 9, 2026
9 checks passed
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.

3 participants