Repository navigation
fix(ai): allow disabling cache control on tools - #58802
TheBrain0110 wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 nocache_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:
cacheFormatselects the marker dialect, and on the Anthropic Messages route it is not consulted for lowering at all (anthropic-messages.tsdoes 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 rejectscache_controlon tool definitions".- 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, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 nocache_controltool 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), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Fixed in fc209b7b — thanks for catching this.
Ran the generators and committed the result:
bun run generateinpackages/protocol→packages/protocol/openapi.jsonbun run generateinpackages/client→packages/client/src/promise/generated/types.ts
Both now pass their checks:
bun run check:generatedinpackages/protocol— passesbun run check:generatedinpackages/client— passes
The option appears in the generated OpenAPI document and in the generated client types.
There was a problem hiding this comment.
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.jsonand the client's generated types are regenerated;check:generatednow passes inpackages/protocolandpackages/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.
fc209b7 to
f046314
Compare
There was a problem hiding this comment.
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.
|
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:
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. |
Issue for this PR
Closes #58799
Type of change
What does this PR do?
Some Anthropic-compatible endpoints accept prompt-cache markers on
systemand message content but return HTTP 400 when a tool definition carriescache_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 isfalse: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.tsfrompackages/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 intactbun run --cwd packages/schema typecheck: passedbun run --cwd packages/ai typecheck: passedbun run --cwd packages/core typecheck: passedbun run --cwd packages/protocol typecheck: passedbun run --cwd packages/client typecheck: passedbun run --cwd packages/protocol check:generated: passedbun run --cwd packages/client check:generated: passedgit diff --check: clean(Built and tested with bun 1.4.4, the version the repo pins.)
Screenshots / recordings
Not a UI change.
Checklist
Added by the opencode bot (AI-generated).
Related: #57199
Public API: adds optional
compatibility.supportsToolCacheControlto model and provider config (backwards compatible)