[GH-ISSUE #1097] [Issue]: OpenRouter Gemini reasoning replay inflates prompt tokens #541

Closed
opened 2026-10-20 17:29:59 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @cha1latte on GitHub (May 22, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/1097

Originally assigned to: @cha1latte on GitHub.

Summary

OpenRouter Gemini requests can replay prior assistant reasoning metadata into later OpenAI-compatible messages, causing excessive prompt token usage for Gemini-family models.

Expected behavior

Gemini/OpenRouter requests should not replay extra.chatCompletionsReasoning / reasoning_details metadata when that provider/model family does not need reasoning continuity from prior assistant messages.

Actual behavior

The reporter observed that Gemini 3.5 Flash through Vertex AI via OpenRouter BYOK consumed unusually high prompt tokens. Locally, they found Marinara reattaching extra.chatCompletionsReasoning from assistant messages into providerMetadata and replaying it in OpenAI-compatible messages. After a local patch stopped the replay, Gemini 3.5 Flash prompt token counts returned to normal.

Steps to reproduce

  1. Use OpenRouter BYOK with the google-vertex/global provider.
  2. Compare long-term chat requests using Gemini-family models with similar output volume.
  3. Observe prompt token accounting for google/gemini-3.1-pro-preview versus google/gemini-3.5-flash.
  4. Inspect whether prior assistant extra.chatCompletionsReasoning / reasoning_details metadata is being saved and replayed into later OpenAI-compatible messages.

Reporter's example:

google/gemini-3.1-pro-preview: prompt approx. 27k-30k
google/gemini-3.5-flash: prompt approx. 59k-63k
Same OpenRouter + google-vertex/global provider
Output volume is nearly identical
3.5 Flash log: prompt 62,977 / cached 56,549 / completion 2,491 / reasoning 1,932

Marinara Engine version

v1.6.0, build 171edef

Install type

Not specified.

OS + version

Windows 11

Browser or app shell

Not specified.

Screenshots or video

Image

Logs

3.5 Flash log: prompt 62,977 / cached 56,549 / completion 2,491 / reasoning 1,932

Additional context

Source: Discord forum post by 로도 in #marinara-engine-bugs-features, posted Friday, May 22, 2026 at 11:23 AM and edited at 11:31 AM.

Reporter recommendation: for OpenRouter Gemini models, do not replay chatCompletionsReasoning as assistant message metadata and do not save Gemini reasoning metadata for future replay. This should stay separate from thinking models that require replay, such as DeepSeek or GLM.

Template check

  • I provided the required summary, behavior, reproduction, and environment details.
  • If this is a visual/UI bug, blank screen, layout issue, mobile/responsive issue, incorrect image or sprite, broken animation, or unexpected error state, I attached screenshots or video.
Originally created by @cha1latte on GitHub (May 22, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/1097 Originally assigned to: @cha1latte on GitHub. ## Summary OpenRouter Gemini requests can replay prior assistant reasoning metadata into later OpenAI-compatible messages, causing excessive prompt token usage for Gemini-family models. ## Expected behavior Gemini/OpenRouter requests should not replay `extra.chatCompletionsReasoning` / `reasoning_details` metadata when that provider/model family does not need reasoning continuity from prior assistant messages. ## Actual behavior The reporter observed that Gemini 3.5 Flash through Vertex AI via OpenRouter BYOK consumed unusually high prompt tokens. Locally, they found Marinara reattaching `extra.chatCompletionsReasoning` from assistant messages into `providerMetadata` and replaying it in OpenAI-compatible messages. After a local patch stopped the replay, Gemini 3.5 Flash prompt token counts returned to normal. ## Steps to reproduce 1. Use OpenRouter BYOK with the `google-vertex/global` provider. 2. Compare long-term chat requests using Gemini-family models with similar output volume. 3. Observe prompt token accounting for `google/gemini-3.1-pro-preview` versus `google/gemini-3.5-flash`. 4. Inspect whether prior assistant `extra.chatCompletionsReasoning` / `reasoning_details` metadata is being saved and replayed into later OpenAI-compatible messages. Reporter's example: ```text google/gemini-3.1-pro-preview: prompt approx. 27k-30k google/gemini-3.5-flash: prompt approx. 59k-63k Same OpenRouter + google-vertex/global provider Output volume is nearly identical 3.5 Flash log: prompt 62,977 / cached 56,549 / completion 2,491 / reasoning 1,932 ``` ## Marinara Engine version v1.6.0, build `171edef` ## Install type Not specified. ## OS + version Windows 11 ## Browser or app shell Not specified. ## Screenshots or video <img width="1088" height="579" alt="Image" src="https://github.com/user-attachments/assets/a28c8b8c-8645-4f12-aa90-a7c44a5d7f47" /> ## Logs ```text 3.5 Flash log: prompt 62,977 / cached 56,549 / completion 2,491 / reasoning 1,932 ``` ## Additional context Source: Discord forum post by 로도 in `#marinara-engine-bugs-features`, posted Friday, May 22, 2026 at 11:23 AM and edited at 11:31 AM. Reporter recommendation: for OpenRouter Gemini models, do not replay `chatCompletionsReasoning` as assistant message metadata and do not save Gemini reasoning metadata for future replay. This should stay separate from thinking models that require replay, such as DeepSeek or GLM. ## Template check - [ ] I provided the required summary, behavior, reproduction, and environment details. - [ ] If this is a visual/UI bug, blank screen, layout issue, mobile/responsive issue, incorrect image or sprite, broken animation, or unexpected error state, I attached screenshots or video.
Author
Owner

@franko-c commented on GitHub (May 23, 2026):

the replay site is packages/server/src/routes/generate.routes.ts:1162-1166, gated only on m.role === "assistant" with no provider check — extra.chatCompletionsReasoning from prior turns gets Object.assign'd into providerMetadata regardless of the active model:

const chatCompletionsReasoning =
  m.role === "assistant" ? readChatCompletionsReasoningMetadata(extra.chatCompletionsReasoning) : undefined;
if (chatCompletionsReasoning) {
  Object.assign(providerMetadata, chatCompletionsReasoning);
}

isGoogleProvider at line 1151 (conn.provider === "google" || conn.provider === "google_vertex") already exists but only covers direct Google connections. OpenRouter BYOK routing to google-vertex/global lands as conn.provider === "openrouter" with conn.model like google/gemini-3.5-flash, so it falls outside that check and gets the OpenAI-style reasoning replay anyway. there's a similar provider+model-prefix pattern at line 2975 (providerLower === "openrouter" && modelLower.startsWith("x-ai/grok-")) that's the right shape to reuse.

both the replay site and the save site (line 6280-6282 rememberChatCompletionsReasoning, persisted at 6911-6912) need to gate on the same condition — otherwise re-enabling replay for Gemini would silently work for cached chats but not new ones, or vice-versa. the existing comment at line 6910 ("Store Chat Completions reasoning fields for providers that require replay (DeepSeek/OpenRouter)") suggests the original author already had a model-family allowlist in mind — turning that comment into a real check covers the reporter's case.

haven't traced whether GLM (mentioned in the reporter's note alongside DeepSeek) routes through OpenRouter the same way, so the allowlist might need to include both an OpenRouter+model-prefix shape and direct-provider matches.

<!-- gh-comment-id:4524652979 --> @franko-c commented on GitHub (May 23, 2026): the replay site is `packages/server/src/routes/generate.routes.ts:1162-1166`, gated only on `m.role === "assistant"` with no provider check — `extra.chatCompletionsReasoning` from prior turns gets `Object.assign`'d into `providerMetadata` regardless of the active model: ```ts const chatCompletionsReasoning = m.role === "assistant" ? readChatCompletionsReasoningMetadata(extra.chatCompletionsReasoning) : undefined; if (chatCompletionsReasoning) { Object.assign(providerMetadata, chatCompletionsReasoning); } ``` `isGoogleProvider` at line 1151 (`conn.provider === "google" || conn.provider === "google_vertex"`) already exists but only covers direct Google connections. OpenRouter BYOK routing to `google-vertex/global` lands as `conn.provider === "openrouter"` with `conn.model` like `google/gemini-3.5-flash`, so it falls outside that check and gets the OpenAI-style reasoning replay anyway. there's a similar provider+model-prefix pattern at line 2975 (`providerLower === "openrouter" && modelLower.startsWith("x-ai/grok-")`) that's the right shape to reuse. both the replay site and the save site (line 6280-6282 `rememberChatCompletionsReasoning`, persisted at 6911-6912) need to gate on the same condition — otherwise re-enabling replay for Gemini would silently work for cached chats but not new ones, or vice-versa. the existing comment at line 6910 ("Store Chat Completions reasoning fields for providers that require replay (DeepSeek/OpenRouter)") suggests the original author already had a model-family allowlist in mind — turning that comment into a real check covers the reporter's case. haven't traced whether GLM (mentioned in the reporter's note alongside DeepSeek) routes through OpenRouter the same way, so the allowlist might need to include both an OpenRouter+model-prefix shape and direct-provider matches.
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#541
No description provided.