Skip to content

LM Studio + Qwen: prompt prefix is not byte-stable across turns, breaks prefix cache #26750

Closed as not planned

Description

@ipogosov

Description

LM Studio prefix caching requires the tokenized prompt prefix to remain identical across turns. With Qwen / QwQ-family chat templates served via LM Studio's OpenAI-compatible endpoint, two things make the prefix unstable across turns when OpenCode replays the conversation history as-is:

  1. Assistant reasoning content is replayed. Qwen chat templates render historical assistant reasoning (think) blocks differently on later turns once a new user message is appended, so the same historical assistant turn tokenizes differently the next time it appears in the prompt.
  2. Raw role: "tool" messages are tokenized non-deterministically. Qwen-style chat templates render past tool results inconsistently when they sit as standalone role: "tool" messages, but render them stably when they appear as <tool_response>...</tool_response> blocks inside the preceding user/assistant text.

Either behavior breaks LM Studio prefix caching even though the underlying conversation history has not meaningfully changed, so a long context can fully re-tokenize on every turn.

Expected: when OpenCode replays the same conversation history through LM Studio's OpenAI-compatible endpoint with a Qwen-family model, the prompt prefix is byte-stable, so the prefix cache hits on every turn after the first.

Actual: the prefix changes turn-over-turn, causing a large or full prompt-cache miss against LM Studio.

This is provider/model compatibility behavior specific to LM Studio + Qwen-style templates and should not affect Anthropic, OpenAI, or non-Qwen OpenAI-compatible backends.

Plugins

None

OpenCode version

1.14.46

Steps to reproduce

  1. Run a Qwen / QwQ-family model under LM Studio (any model whose chat template uses the Qwen <tool_response> convention).
  2. Configure OpenCode to use LM Studio's OpenAI-compatible endpoint as the provider.
  3. Have at least one tool-using turn in the session, then send another user message so the previous tool result becomes history.
  4. Observe LM Studio's prefix-cache stats on the second model request (and any later turn): cache hit on the prefix is much smaller than the actual unchanged history would imply, often near zero, because the rendered prompt prefix is not byte-stable.
  5. (Optional) Compare against a non-Qwen OpenAI-compatible LM Studio model — prefix cache behaves normally.

Screenshot and/or share link

No response

Operating System

macOS

Terminal

iTerm2

Activity

  1. github-actions commented on May 10, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

    If your issue adds meaningfully new information (e.g. the role: "tool" serialization instability as a second independent source of cache misses), please comment on #19081 with that detail or clarify how your report differs.

  2. github-actions commented on Jul 10, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions