Repository navigation
Conversation
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
There was a problem hiding this comment.
I couldn't verify the caching fix itself, because that needs a real Anthropic-backed gateway. I did check the request body: the new test fails on the base, where the marker is inside the tool content array, and passes with this change. The existing packages/ai provider tests, the Databricks and Snowflake Cortex plugin tests in packages/core, and typecheck:tsc all pass.
Public API: unchanged. The request body's tool message can now carry cache_control, but that is internal.
The main concern is the scope of the change. It isn't limited to the gateway in #57199. The built-in Databricks and Snowflake Cortex plugins set cacheFormat: "anthropic" on their Claude models, and both use the openai-compatible route, so their tool-result markers move too. There's no evidence yet that either honours a marker on the tool message. The only data in #57199 shows OpenRouter and LiteLLM accept both positions, so the evidence that the old position fails comes from one private gateway. If Databricks or Cortex ignore the new position, those users lose caching after tool results. Could you confirm it against them, or make the message-level position an explicit opt-in instead of tying it to cacheFormat? There's also a smaller point about where the check lives (inline).
| // A gateway that speaks the chat shape in front of Anthropic reads a tool result's marker | ||
| // off the tool message, and one nested in the content array is not a position it translates. | ||
| // OpenRouter is left alone: it documents the content-part position for its own contract. | ||
| cacheControlAtMessageLevel: |
There was a problem hiding this comment.
This switches the marker position for every openai-compatible-chat model with cacheFormat: "anthropic". That includes the Claude models of the built-in Databricks and Snowflake Cortex plugins (packages/core/src/plugin/provider/databricks.ts, snowflake-cortex.ts), not only the gateway from #57199. Has the message-level marker been checked on those? If they don't read it, this would turn off caching after tool results for them.
Also, the generic chat protocol now checks for a specific route id. Elsewhere, provider-specific differences are passed in through LoweringOptions by the provider or route (as DigitalOcean and OpenRouter do with fromRequest(request, { ... })). Following that pattern, or adding an explicit compatibility setting for this position, would keep openai-chat.ts free of route ids and let only the gateways that need it opt in.
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
73c3e08 to
ffc6e09
Compare
|
@opencodejr The scope point is right, and I took the opt-in route. I checked the two providers instead of guessing:
Neither documents a marker on the tool message, so tying the move to I also moved the check out of the lowering literal into The tests now cover both directions: an anthropic-format model without the flag keeps the marker in the content array, and the opt-in case moves it to the tool message. I did not verify either provider against a live account; that would need credentials I do not have. The docs are all the evidence available on my side, which is why the default behavior is unchanged. |
…ways A gateway that speaks the OpenAI chat shape in front of Anthropic reads a tool result's cache marker off the tool message, while an endpoint that documents content blocks keeps the nested position. The position belongs to the endpoint, so it is a compatibility field rather than a consequence of cacheFormat.
ffc6e09 to
22ab9e7
Compare
There was a problem hiding this comment.
The earlier scope concern is fixed: the tool-message position is now an explicit opt-in, so Databricks, Snowflake Cortex and other models with only cacheFormat: "anthropic" keep the content-part marker. I checked the request bodies with the PR's 4 tests. The full packages/ai suite and the packages/ai and packages/schema type checks pass. I couldn't check actual cache reads, because that needs a real Anthropic-backed gateway.
Public API: adds an optional compatibility.cacheControlAtMessageLevel setting at model and provider level (backwards compatible, defaults to off, so request bodies don't change unless it's set).
One remaining issue: the generated client types weren't regenerated (inline).
| /** Prompt-cache marker syntax the endpoint accepts. Overrides the route default; both send `cache_control`, and `alibaba` skips tool definitions. */ | ||
| cacheFormat: CacheFormat.pipe(optional), | ||
| /** OpenAI Chat: put an anthropic tool-result marker on the tool message instead of its content part, for a gateway that translates the chat shape into Anthropic's messages. */ | ||
| cacheControlAtMessageLevel: Schema.Boolean.pipe(optional), |
There was a problem hiding this comment.
This field becomes part of the generated client types. Running bun run generate from packages/client (as AGENTS.md asks after a public protocol change) adds cacheControlAtMessageLevel?: boolean to ModelCompatibility and ProviderCompatibility in packages/client/src/promise/generated/types.ts. Please commit the regenerated files, or check:generated will likely fail.
There was a problem hiding this comment.
This push only rebases onto newer dev; the PR's own commit is unchanged, so nothing new to review. The 4 tests in openai-compatible-cache.test.ts and typecheck:tsc in packages/ai still pass. The generated client types are still not regenerated (the open thread on packages/schema/src/model-compatibility.ts).
Public API: adds an optional compatibility.cacheControlAtMessageLevel setting for models and providers (backwards compatible, defaults to off).
Issue for this PR
Closes #57199
Type of change
What does this PR do?
A gateway that speaks the OpenAI chat shape in front of Anthropic expects the cache marker for a tool result on the tool message. OpenCode nested it in the tool message's content array, and a gateway does not translate a marker from that position, so a tool-heavy session was billed as uncached input from the first tool result on, exactly as the issue's table shows: cache read pinned at the system prompt and tools, cache write at zero, while the prompt grew to 272K tokens.
The marker can now move to the tool message, but only for a model that opts in:
{ "providers": { "gateway": { "models": { "claude-sonnet-5-5": { "compatibility": { "cacheFormat": "anthropic", "cacheControlAtMessageLevel": true } } } } } }It is opt-in because the position is a property of the endpoint, not of the marker syntax. An OpenAI-compatible endpoint that reads
cache_controlfrom content blocks wants the old position, and two built-in providers declarecacheFormat: "anthropic"for exactly that:messages.contentarray" as cacheable.cache_controlto content blocks".Making the message position a consequence of
cacheFormatwould have moved the marker away from the position those endpoints document, which is the scope concern raised in review. The default is unchanged; the flag is what separates a gateway that translates the chat shape into Anthropic's messages from an endpoint that reads content blocks directly.The user-message position is unchanged, and OpenRouter keeps its documented content-part position even when a model opts in.
The flag is added to the config compatibility schema (
packages/schema/src/model-compatibility.ts), the model compatibility schema (packages/ai/src/schema/options.ts), and documented inservices/site/src/docs/content/models.mdx.How did you verify your code works?
cacheFormat: "anthropic"without the flag keeps it on the content part; no declared format places no marker at all; OpenRouter with the flag keeps the content part.packages/aisuite: 2192 pass, 0 fail, 29 skipped.packages/coreconfig provider and model resolver tests: 92 pass, 0 fail.bun run typecheck:tscpasses inpackages/aiandpackages/schema.models.mdx; I left that untouched and changed only the two paragraphs and the table row this PR adds.Screenshots / recordings
None. The change is in the request body and in a config field, not in anything the user sees.
Checklist
Added by the opencode bot (AI-generated).
Fixes #57199
Related: #58802
Public API: adds the optional compatibility.cacheControlAtMessageLevel config setting for models and providers (backwards compatible, defaults to off)