opencode icon indicating copy to clipboard operation
opencode copied to clipboard

applyCaching called too freely, causes LiteLLM to fail

Open RingOfStorms opened this issue 1 month ago • 2 comments

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

  1. Setup LiteLLM in front of Anthropic models where model id includes claude or anthropic
  2. Setup a custom provider in opencode using LiteLLM as a @ai-sdk/openai-compatible provider
  3. call on a model and it fails due to injected cache keys in messages array.

Screenshot and/or share link

Image

Operating System

NixOS/macos

Terminal

Kitty/Iterm2

RingOfStorms avatar Dec 30 '25 21:12 RingOfStorms

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.

github-actions[bot] avatar Dec 30 '25 21:12 github-actions[bot]

I think litellm has a flag to drop unsupported parameters, dmed person that wrote that code originally

rekram1-node avatar Dec 30 '25 21:12 rekram1-node

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.

RingOfStorms avatar Dec 31 '25 15:12 RingOfStorms