applyCaching called too freely, causes LiteLLM to fail
Description
Calls to models with the string claude or anthropic in the id fail when they are not actual anthropic upstream providers.
I have this custom provider in my config:
"provider": {
"custom": {
"models": {
"claude-opus-4.1": {
"name": "claude-opus-4.1"
},
},
"npm": "@ai-sdk/openai-compatible",
"options": {
"apiKey": "na",
"baseURL": "https://custom.com"
}
}
},
Chatting with this model fails with
litellm.BadRequestError: Vertex_aiException BadRequestError - b'{"type":"error","error":{"type":"invalid_request_error","message":"invalid beta flag"},"request_id":"req_vrtx_011CWdZLV7UDvRZBkvyfXcKJ"}'. Received Model Group=claude-opus-4.1 Available Model Group Fallbacks=None
Because of this logic https://github.com/sst/opencode/blob/3fe5d91372fdf859e09ed5a2aefe359e0648ed10/packages/opencode/src/provider/transform.ts#L198-L208
Even though my model has claude in the name I am explicitly saying it is an openai provider with "npm": "@ai-sdk/openai-compatible". In this case I am using LiteLLM proxy to front all LLM access and it does not support the added "cache_control" key this injects to every message.
Some possible solutions that could be done:
- config option to disable the applyCaching functionality, either per model or provider
-
(... existing ors) && model.api.npm !== "@ai-sdk/openai-compatible"be added to the if statement logic there. Though I am not sure if that is the best approach or if it should apply to other providers as well.
OpenCode version
1.0.212
Steps to reproduce
- Setup LiteLLM in front of Anthropic models where model id includes claude or anthropic
- Setup a custom provider in opencode using LiteLLM as a @ai-sdk/openai-compatible provider
- call on a model and it fails due to injected cache keys in messages array.
Screenshot and/or share link
Operating System
NixOS/macos
Terminal
Kitty/Iterm2
This issue might be a duplicate of existing issues. Please check:
- #4386: Extra inputs are not permitted, field: 'promptCacheKey' - Similar issue where cache-related fields are being added to OpenAI-compatible providers that don't support them
- #5416: [FEATURE]: Anthropic (and others) caching improvement - Related discussion about caching behavior across different providers
Feel free to ignore if none of these address your specific case.
I think litellm has a flag to drop unsupported parameters, dmed person that wrote that code originally
For anyone else hitting this, this plugin will work (for now at least) since the model.api.id is used primarily before this event hook is called so if we change it during this stage the applyLogic comes after.
This could break something in the future but it works as a patch for me now.
import type { Plugin } from "@opencode-ai/plugin";
export const PatchCachingBug: Plugin = async () => {
return {
async "chat.params"(input) {
const model = input.model;
// Only touch the specific bad combo: OpenAI-compatible / gateway model
// whose api.id contains "claude" but is NOT actually Anthropic.
if (
model.providerID !== "anthropic" &&
model.api.npm !== "@ai-sdk/anthropic" &&
typeof model.api.id === "string" &&
(model.api.id.includes("claude") || model.api.id.includes("anthropic"))
) {
// Strip the "claude" substring so ProviderTransform.message
// does not enter the Anthropic caching branch.
model.api.id = model.api.id.replace(/claude/gi, "cl4ude");
}
},
};
};
I think litellm has a flag to drop unsupported parameters, dmed person that wrote that code originally
I don't have full control over this particular LiteLLM instance myself so I can't easily get this option added yet.