[GH-ISSUE #4742] [Issue]: Google models do not cache prompt when using Openrouter as provider #2604

Closed
opened 2026-10-21 05:51:10 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @firelightning13 on GitHub (Aug 8, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/4742

Originally assigned to: @SpicyMarinara on GitHub.

Summary

When enableCaching is toggled on for an OpenRouter connection, Marinara only sends the cache_control flag for Claude models. Models that support OpenRouter prompt caching — such as Google Gemini — never receive the flag, resulting in full-price token costs instead of cached pricing.

Expected behavior

When "Enable Prompt Caching" is toggled on for an OpenRouter connection, Marinara should send cache_control: { type: "ephemeral" } for all models, so that any model supporting OpenRouter prompt caching (Claude, Gemini, etc.) receives the benefit. OpenRouter gracefully ignores the flag for models that don't support caching.

Actual behavior

The shouldUseOpenRouterPromptCaching method in packages/server/src/services/llm/providers/openai.provider.ts (line ~930) gates the cache_control flag behind a model-name check:

private shouldUseOpenRouterPromptCaching(options: ChatOptions): boolean {
  return (
    !this.isGenericCustomProvider() &&
    this.baseUrl.includes("openrouter.ai") &&
    !!options.enableCaching &&
    options.model.toLowerCase().includes("claude")  // ← only Claude gets the flag
  );
}

Because of the options.model.toLowerCase().includes("claude") condition, non-Claude models that support caching on OpenRouter (e.g., Google Gemini) never receive the cache_control flag, even when the user has explicitly enabled prompt caching in the connection settings.

Some models (DeepSeek, GLM, MiMo) cache automatically on OpenRouter's side without needing the flag, so they appear to work fine. Google Gemini requires the explicit cache_control flag per OpenRouter's docs, so it silently gets no caching.

Steps to reproduce

  1. Set up an OpenRouter connection in Marinara Engine with a Google Gemini model (e.g., google/gemini-3.6-flash or google/gemini-3.1-pro-preview).
  2. Enable "Prompt Caching" in the connection settings.
  3. Send multiple messages in a chat to build up a cacheable prompt prefix.
  4. Check the token usage / cost. No cached tokens are reported, and full-price tokens are charged.

Environment

  • Marinara Engine version: 2.4.1
  • Install type: termux
  • OS + version: Android (Termux)
  • Browser or app shell: Brave mobile browser

Logs, screenshots, or video

After removing the options.model.toLowerCase().includes("claude") gate from shouldUseOpenRouterPromptCaching, Google Gemini Flash via OpenRouter started receiving cached tokens, resulting in approximately a 50% cost reduction on subsequent messages in the same chat.

Image

Additional context

  • OpenRouter docs confirm Gemini prompt caching requires the cache_control flag: https://openrouter.ai/docs/guides/best-practices/prompt-caching#how-to-enable-gemini-prompt-caching
  • The connection editor UI shows the Prompt Caching toggle for all OpenRouter models, but the help text says "For OpenRouter Claude models, sends the cache_control flag... Most non-Claude OpenRouter models cache automatically and do not need this toggle", this is inaccurate for Gemini, which needs the flag.
  • Suggested fix: Remove the options.model.toLowerCase().includes("claude") condition from shouldUseOpenRouterPromptCaching, so the flag is sent for all OpenRouter models when enableCaching is true. OpenRouter ignores the flag for models that don't support caching, so this is safe.
private shouldUseOpenRouterPromptCaching(options: ChatOptions): boolean {
  return (
    !this.isGenericCustomProvider() &&
    this.baseUrl.includes("openrouter.ai") &&
    !!options.enableCaching
  );
}

Or add the Gemini model under Claude's option, though this assumes only two provider supports:

options.model.toLowerCase().includes("claude") ||
options.model.toLowerCase().includes("gemini")

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @firelightning13 on GitHub (Aug 8, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/4742 Originally assigned to: @SpicyMarinara on GitHub. ## Summary When `enableCaching` is toggled on for an OpenRouter connection, Marinara only sends the `cache_control` flag for Claude models. Models that support OpenRouter prompt caching — such as Google Gemini — never receive the flag, resulting in full-price token costs instead of cached pricing. ## Expected behavior When "Enable Prompt Caching" is toggled on for an OpenRouter connection, Marinara should send `cache_control: { type: "ephemeral" }` for **all** models, so that any model supporting OpenRouter prompt caching (Claude, Gemini, etc.) receives the benefit. OpenRouter gracefully ignores the flag for models that don't support caching. ## Actual behavior The `shouldUseOpenRouterPromptCaching` method in `packages/server/src/services/llm/providers/openai.provider.ts` (line ~930) gates the `cache_control` flag behind a model-name check: ```typescript private shouldUseOpenRouterPromptCaching(options: ChatOptions): boolean { return ( !this.isGenericCustomProvider() && this.baseUrl.includes("openrouter.ai") && !!options.enableCaching && options.model.toLowerCase().includes("claude") // ← only Claude gets the flag ); } ``` Because of the `options.model.toLowerCase().includes("claude")` condition, non-Claude models that support caching on OpenRouter (e.g., Google Gemini) never receive the `cache_control` flag, even when the user has explicitly enabled prompt caching in the connection settings. Some models (DeepSeek, GLM, MiMo) cache automatically on OpenRouter's side without needing the flag, so they appear to work fine. Google Gemini requires the explicit `cache_control` flag per [OpenRouter's docs](https://openrouter.ai/docs/guides/best-practices/prompt-caching#how-to-enable-gemini-prompt-caching), so it silently gets no caching. ## Steps to reproduce 1. Set up an OpenRouter connection in Marinara Engine with a Google Gemini model (e.g., `google/gemini-3.6-flash` or `google/gemini-3.1-pro-preview`). 2. Enable "Prompt Caching" in the connection settings. 3. Send multiple messages in a chat to build up a cacheable prompt prefix. 4. Check the token usage / cost. No cached tokens are reported, and full-price tokens are charged. ## Environment - Marinara Engine version: 2.4.1 - Install type: termux - OS + version: Android (Termux) - Browser or app shell: Brave mobile browser ## Logs, screenshots, or video After removing the `options.model.toLowerCase().includes("claude")` gate from `shouldUseOpenRouterPromptCaching`, Google Gemini Flash via OpenRouter started receiving cached tokens, resulting in approximately a **50% cost reduction** on subsequent messages in the same chat. <img width="1920" height="864" alt="Image" src="https://github.com/user-attachments/assets/694f8138-3ff5-40a2-b135-dd01e733bff8" /> ## Additional context - OpenRouter docs confirm Gemini prompt caching requires the `cache_control` flag: https://openrouter.ai/docs/guides/best-practices/prompt-caching#how-to-enable-gemini-prompt-caching - The connection editor UI shows the Prompt Caching toggle for all OpenRouter models, but the help text says "For OpenRouter Claude models, sends the cache_control flag... Most non-Claude OpenRouter models cache automatically and do not need this toggle", this is inaccurate for Gemini, which needs the flag. - **Suggested fix:** Remove the `options.model.toLowerCase().includes("claude")` condition from `shouldUseOpenRouterPromptCaching`, so the flag is sent for all OpenRouter models when `enableCaching` is true. OpenRouter ignores the flag for models that don't support caching, so this is safe. ```typescript private shouldUseOpenRouterPromptCaching(options: ChatOptions): boolean { return ( !this.isGenericCustomProvider() && this.baseUrl.includes("openrouter.ai") && !!options.enableCaching ); } ``` Or add the Gemini model under Claude's option, though this assumes only two provider supports: ``` options.model.toLowerCase().includes("claude") || options.model.toLowerCase().includes("gemini") ``` ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:51:10 +07:00
Author
Owner

@SpicyMarinara commented on GitHub (Aug 8, 2026):

Claimed by @SpicyMarinara for the current assigned-issue sweep. Implementation and validation are tracked in draft PR #4747.

<!-- gh-comment-id:5225218136 --> @SpicyMarinara commented on GitHub (Aug 8, 2026): Claimed by @SpicyMarinara for the current assigned-issue sweep. Implementation and validation are tracked in draft PR #4747.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#2604
No description provided.