Skip to content

fix(ai): allow disabling cache control on tools - #58802

Closed
TheBrain0110 wants to merge 2 commits into
anomalyco:devfrom
TheBrain0110:anthropic-cache-compat
Closed

TheBrain0110 wants to merge 2 commits into
anomalyco:devfrom
TheBrain0110:anthropic-cache-compat

Conversation

@TheBrain0110

@TheBrain0110 TheBrain0110 commented Oct 24, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #58799

Type of change

  • Bug fix

What does this PR do?

Some Anthropic-compatible endpoints accept prompt-cache markers on system and message content but return HTTP 400 when a tool definition carries cache_control.

The only escape today is cache: "none", which is too broad — it disables all automatic cache placement, including the sites those endpoints do support.

This adds a model compatibility option, supportsToolCacheControl. When it is false:

  • automatic cache placement skips tool definitions
  • manually supplied tool cache hints are dropped during lowering, on the Anthropic Messages route and on the OpenAI Chat route
  • tool hints are excluded from the four-breakpoint budget, so system and message markers keep the available capacity

When the option is omitted it defaults to the existing behavior (true), so Anthropic and other providers are unaffected. The change is generic compatibility metadata, not a check against any particular provider.

How did you verify your code works?

  • bun test test/cache-policy.test.ts from packages/ai: 32 pass, 0 fail — including four new tests covering automatic and manual tool hints on both the Anthropic Messages and OpenAI Chat routes, and asserting that system and message markers stay intact
  • bun run --cwd packages/schema typecheck: passed
  • bun run --cwd packages/ai typecheck: passed
  • bun run --cwd packages/core typecheck: passed
  • bun run --cwd packages/protocol typecheck: passed
  • bun run --cwd packages/client typecheck: passed
  • bun run --cwd packages/protocol check:generated: passed
  • bun run --cwd packages/client check:generated: passed
  • git diff --check: clean

(Built and tested with bun 1.4.4, the version the repo pins.)

Screenshots / recordings

Not a UI change.

Checklist

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

Added by the opencode bot (AI-generated).

Related: #57199

Public API: adds optional compatibility.supportsToolCacheControl to model and provider config (backwards compatible)

@opencode-agent opencode-agent 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 setting works as described. I compiled Anthropic Messages requests with it on the base and on this branch. With the PR, tool definitions lose cache_control, both automatic and hand-added, while the system and message markers stay. cache-policy.test.ts passes, and tsc is clean for packages/ai and packages/schema.

Overlap with an existing option. On the current base, compatibility.cacheFormat: "alibaba" already sends a byte-identical body for automatic caching: no marker on tools, markers on the system prompt and the last message. The docs describe it as for endpoints that "accept the same markers but ignore them on tool definitions". The only extra here is dropping hand-added tool hints, and OpenCode's core never adds those. So the endpoint in #58799 should already work with "cacheFormat": "alibaba". Could you try that? If it works, documenting it (or adding a neutrally named format value) may be better than a second setting that overlaps with cacheFormat. Maintainers should decide which they prefer.

Generated files. If the new setting stays, packages/protocol/openapi.json and packages/client/src/promise/generated/types.ts need regenerating. Run bun run generate in packages/protocol, then in packages/client. check:generated currently fails on this branch and passes on the base.

Public API. Adds the optional config key compatibility.supportsToolCacheControl (boolean, defaults to true) on models, and on providers through ProviderCompatibility. It is also exposed through the OpenAPI/client Model.Compatibility types once regenerated. Backwards compatible: existing configs are unaffected.

Related: #57199

const hint = makeHint(policy.ttlSeconds)
const budget = { remaining: Math.max(0, BREAKPOINT_CAP - countHints(request)) }
const tools = policy.tools ? markLastTool(request.tools, hint, budget) : request.tools
const supportsToolCacheControl = request.model.compatibility?.supportsToolCacheControl !== false

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.

For automatic caching, this gives the same result as selectPolicy already does for cacheFormat: "alibaba" (which returns QWEN = system + message tail, no tools). I checked that the compiled Anthropic body is identical for both settings. If a separate setting is still wanted, it overlaps with cacheFormat, and the two can now disagree. Could the endpoint in #58799 use "cacheFormat": "alibaba" instead?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch, and I checked this rather than assuming. They are not equivalent — I compiled the Anthropic body for both settings and compared.

With a manually added tool cache hint (tool.cache), on the Anthropic Messages route:

  • cacheFormat: "alibaba" → the tool definition still carries "cache_control":{"type":"ephemeral"}
  • supportsToolCacheControl: false → the tool definition has no cache_control

They are identical only for the automatic-placement case (no manual hint), where cacheFormat: "alibaba" maps to the QWEN policy (system + message tail, no tools) and my option skips tools as well.

So cacheFormat: "alibaba" does not fully solve the problem in #58799: it suppresses automatic tool markers, but a hand-added tool hint still puts cache_control on the tool definition, which is exactly the shape the endpoint rejects with a 400.

Two more reasons I did not want to route this through cacheFormat:

  1. cacheFormat selects the marker dialect, and on the Anthropic Messages route it is not consulted for lowering at all (anthropic-messages.ts does not read it). Its only effect there is the automatic policy. Using it as the workaround would be semantically misleading — it says "this endpoint speaks the Alibaba dialect" rather than "this endpoint rejects cache_control on tool definitions".
  2. It is a route-wide switch. It also changes marker writing on OpenAI-compatible and Bedrock routes, where it would be wrong for an endpoint that speaks the Anthropic dialect but rejects tool markers.

That said, I agree the two settings now overlap for the automatic case, and I would rather not leave two knobs that can disagree.

One option is to fold this into cacheFormat as a third dialect value, but that conflates "which dialect" with "which parts of the request may carry markers" — and the second question is already answered per-part by the existing cache: { tools: false, system: true, messages: ... } granularity.

My preference is to keep the separate option, and to make the interaction explicit: when supportsToolCacheControl: false is set, tool hints are dropped in lowering on every route that can emit them, and tool hints are excluded from the four-breakpoint budget so system and message markers keep the available capacity.

If you would rather consolidate this into cacheFormat, I am happy to rework it that way — the change is small. Which shape do you prefer?

(As noted in the other thread, hand-added tool hints on OpenAI-compatible routes now respect this option too.)

description: tool.description,
input_schema: tool.inputSchema,
cache_control: cacheControl(breakpoints, tool.cache),
cache_control: supportsCacheControl ? cacheControl(breakpoints, tool.cache) : undefined,

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.

Hand-added tool hints are only dropped in Anthropic Messages lowering. openai-chat.ts (used with cacheFormat: "anthropic" on OpenAI-compatible proxies) still sends cache_control on tools from tool.cache (around line 346), so a hand-added hint ignores the setting there. Automatic markers are skipped on every route, so this only matters for hand-added hints.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch — that was a real gap and it is fixed in fc209b7b.

openai-chat.ts now reads the same compatibility flag and drops hand-added tool cache hints when it is false, so the setting behaves the same way on the OpenAI Chat route as it does on Anthropic Messages:

cache_control: supportsToolCacheControl ? options.cacheControl?.(tool.cache) : undefined,

Two tests cover it:

  • tool cache compatibility suppresses manual tool hints on OpenAI-compatible routes — asserts the tool definition carries no cache_control
  • tool cache compatibility keeps tool hints by default on OpenAI-compatible routes — asserts the default behaviour is unchanged

These use cacheFormat: "anthropic" on the OpenAI-compatible route, since that is the configuration where a marker writer is active on tools at all.

Full suite for the file is now 32 pass / 0 fail, and packages/ai typechecks clean.

/** Supports per-message effort updates. Overrides model-ID detection. */
supportsEffortUpdates: Schema.Boolean.pipe(optional),
/** Supports cache_control on Anthropic tool definitions. Defaults to true. */
supportsToolCacheControl: 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 changes the public Model.Compatibility schema, so the generated OpenAPI document and client types need regenerating: bun run generate in packages/protocol, then in packages/client. bun run check:generated in packages/protocol fails on this branch and passes on the base.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in fc209b7b — thanks for catching this.

Ran the generators and committed the result:

  • bun run generate in packages/protocol → packages/protocol/openapi.json
  • bun run generate in packages/client → packages/client/src/promise/generated/types.ts

Both now pass their checks:

  • bun run check:generated in packages/protocol — passes
  • bun run check:generated in packages/client — passes

The option appears in the generated OpenAPI document and in the generated client types.

@opencode-agent opencode-agent 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 new commit addresses two of my earlier points:

  • Hand-added tool hints are now also dropped on the OpenAI Chat route, and there are tests for both settings.
  • openapi.json and the client's generated types are regenerated; check:generated now passes in packages/protocol and packages/client.

cache-policy.test.ts passes (32 tests) and tsc is clean for packages/ai.

Still open: cacheFormat: "alibaba" already gives the same automatic caching, so maintainers should decide whether to keep a separate setting (see the open thread).

Public API. Adds the optional compatibility.supportsToolCacheControl (boolean, defaults to true) to model and provider config, and to Model.Compatibility and Provider.Compatibility in the OpenAPI spec and generated client types. Backwards compatible.

@TheBrain0110
TheBrain0110 force-pushed the anthropic-cache-compat branch from fc209b7 to f046314 Compare October 25, 2026 14:36

@opencode-agent opencode-agent 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 the PR onto the latest dev. The change itself is unchanged apart from the test import path. On the new head, cache-policy.test.ts passes (32 tests), tsc is clean for packages/ai, and check:generated passes for packages/protocol and packages/client.

The previous commit already fixed the OpenAI Chat handling of hand-added hints and the stale generated files. Still open: cacheFormat: "alibaba" already gives the same automatic caching (see the open thread).

Public API. Unchanged since my last review: this adds the optional compatibility.supportsToolCacheControl (defaults to true) to model and provider config and to the OpenAPI spec and generated client types. Backwards compatible.

@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE]: allow disabling cache_control on tool definitions for Anthropic-compatible endpoints

1 participant