openwork icon indicating copy to clipboard operation
openwork copied to clipboard

[Bug]: Custom provider compatibility.cacheFormat is dropped, so Claude through an OpenAI-compatible gateway is never prompt-cached

Open pcharasz opened this issue 3 weeks ago • 3 comments

Summary

Claude models served by a custom OpenAI-compatible provider are never prompt-cached. OpenWork drops compatibility.cacheFormat when it builds the v2 engine config from the Den provider definition, then rewrites that config on later syncs. Every turn is billed as fresh input.

To Reproduce

  1. In Den, add a custom OpenAI-compatible provider that serves Claude and at least one non-Claude model.
  2. On each Claude model only, set "compatibility": { "cacheFormat": "anthropic" }. Do not set it on the provider or on the other models; those models can reject Anthropic cache markers.
  3. Select one of those Claude models and send two short turns a few seconds apart.
  4. Compare the provider's token usage, and compare the generated v2 engine config with the Den definition.

Expected behavior

The generated model entry keeps compatibility.cacheFormat. The first request writes the prompt cache. The next request reads it, and fresh input falls to the new tokens only.

Actual behavior

The generated model entry has no compatibility field. Both turns report cache read 0 and cache write 0, with the full prompt counted as fresh input.

Adding compatibility.cacheFormat directly to the generated engine config fixes it immediately: the next request writes the cache, and following requests read it. The next provider sync rewrites that file from Den and caching stops again. Putting the same field in the user opencode.jsonc does not help, because the v2 engine is started with OPENCODE_CONFIG_DIR pointed at the generated config directory.

Screenshots (optional)

None.

OW version & Desktop info (optional)

  • OpenWork version: 0.18.66
  • OS: Windows 11
  • Engine: the desktop's bundled OpenCode v2 engine

Additional context (optional)

compatibility is not copied at either step that builds the engine provider:

  • modelConfigPassthroughKeys in packages/types/src/managed-provider-config.ts allows family, release_date, attachment, reasoning, temperature, tool_call, interleaved, cost, limit, modalities, status, options, headers, provider, and variants. It does not allow compatibility. The same list also drops other Den fields, such as structured_output and open_weights.
  • renderOpencodeV2Config in apps/server/src/managed-opencode-v2.ts builds each model from name, id, capabilities, limit, family, options, variants, and headers. It never copies compatibility.

OpenCode only adds Anthropic cache_control markers for Claude behind an OpenAI-compatible gateway when compatibility.cacheFormat is "anthropic". See the OpenCode v2 models guide, "Prompt caching". Without that field, a gateway that requires explicit markers cannot cache.

Related, but a different cause: #7164. There the model id is a gwm_… alias, so automatic detection does not recognize Claude. Here the model id is a normal Claude id and the required setting is present in Den, but OpenWork removes it before the engine sees it.

Suggested fix: pass compatibility through both builders, per model rather than for the whole provider. A useful extra default is cacheFormat: "anthropic" for Claude model ids on @opencode/ai/providers/openai-compatible when the model does not set one. That default would not fix #7164, because those alias ids do not contain claude.

pcharasz avatar Nov 11 '26 08:11 pcharasz

Thanks, your analysis was spot on. The fix is in #7300.

  • compatibility now passes through both builders (modelConfigPassthroughKeys and renderOpencodeV2Config), per model. Your Claude models keep cacheFormat: "anthropic", and the other models on that provider stay untouched.
  • As you suggested, a Claude model on an OpenAI-compatible provider now defaults to cacheFormat: "anthropic" when it doesn't set one.
  • Claude models reached through the OpenWork Gateway get their cache breakpoints added server-side (the #7164 case).

Once #7300 is merged and released, could you test it against OpenWork Cloud? Use your Den custom provider with the Claude model and send two short turns a few seconds apart. The second turn should report a cache read, and it should keep working after the next provider sync. Let us know how it goes.

OmarMcAdam avatar Nov 11 '26 22:11 OmarMcAdam

#7300 is merged and its server-side part is live on OpenWork Cloud. Your case (a Den custom provider the desktop calls directly) is fixed in the desktop app, and that ships in the next desktop release after v0.18.66. Once you're on it, could you send two short turns to one of your Claude models a few seconds apart? The second should report a cache read, and it should keep working after the next provider sync. If your Claude model goes through the OpenWork Gateway instead, you can test it on Cloud right now.

OmarMcAdam avatar Nov 11 '26 22:11 OmarMcAdam

@OmarMcAdam Thank you for the fix. I tested it after updating to v.0.18.67, and caching works correctly now.

pcharasz avatar Nov 14 '26 09:11 pcharasz