Skip to content

fix(ai): make the tool-message cache marker opt-in for anthropic gateways - #63828

Open
dat-rd wants to merge 1 commit into
anomalyco:devfrom
dat-rd:fix-tool-cache-marker
Open

dat-rd wants to merge 1 commit into
anomalyco:devfrom
dat-rd:fix-tool-cache-marker

Conversation

@dat-rd

@dat-rd dat-rd commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Issue for this PR

Closes #57199

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

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_control from content blocks wants the old position, and two built-in providers declare cacheFormat: "anthropic" for exactly that:

Making the message position a consequence of cacheFormat would 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 in services/site/src/docs/content/models.mdx.

How did you verify your code works?

  • Four compile cases: a gateway that opts in gets the marker on the tool message; 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.
  • The two placement cases fail in opposite directions, so neither can pass by ignoring the flag: removing the flag from the model leaves the marker in the content array, and ignoring it in the lowering keeps it there as well.
  • Full packages/ai suite: 2192 pass, 0 fail, 29 skipped.
  • packages/core config provider and model resolver tests: 92 pass, 0 fail.
  • bun run typecheck:tsc passes in packages/ai and packages/schema.
  • oxlint and prettier clean on the changed source files. Prettier reports pre-existing table formatting in 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

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

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)

@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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).

Fixes #57199
Related: #58802

// 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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@github-actions github-actions Bot removed needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels Nov 13, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@dat-rd
dat-rd force-pushed the fix-tool-cache-marker branch from 73c3e08 to ffc6e09 Compare November 13, 2026 18:28
@dat-rd dat-rd changed the title fix(ai): put a tool result's cache marker on the tool message for anthropic gateways fix(ai): make the tool-message cache marker opt-in for anthropic gateways Nov 13, 2026
@dat-rd

dat-rd commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

@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 cacheFormat would have moved the marker out of the documented position for both. The message-level position is now compatibility.cacheControlAtMessageLevel: true (default false, honored on the openai-compatible-chat route). Databricks, Cortex, and OpenRouter keep the content-part position; the gateway in #57199 gets the tool-message position by opting in.

I also moved the check out of the lowering literal into toolCacheControlAtMessageLevel, which is the second point you raised.

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.
@dat-rd
dat-rd force-pushed the fix-tool-cache-marker branch from ffc6e09 to 22ab9e7 Compare November 13, 2026 18:34

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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).

This branch has not been deployed

No deployments
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.

provider: cacheFormat "anthropic" puts cache_control inside tool message content, gateway ignores it, no caching after tool results

1 participant