Repository navigation
Description
Area: [Engine] (the server)
Terminal or source: Source (START-HERE.bat checkout, terminal/serve)
Reproduction status: Reproduced locally (details and one caveat below)
What happened
With "switch_on_request": true, a request that switches to another model and is otherwise identical to the request just before it gets the previous model's rendered and tokenized prompt.
Service._exact_prompt_cache (added in f5bde72, "incremental prompt cache") is keyed by (messages, tools, kwargs, effort_end, preserve_thinking) in encode_prompt() (terminal/serve/server.py, around line 6706). The key does not include the model. The model switch, which sets self.model, self.template and self.tok (around line 5310), does not clear the cache either. The same Service object serves both models, so the cached token ids from model A are sent to model B's engine. They come from A's chat template and A's tokenizer.
Smallest steps that reproduce it
- Use a run config with
"switch_on_request": trueand two installed models. Mine: Gemma 4 26B-A4B UD-Q3_K_M (strata-poly) is loaded, and Qwen3.8-Flash-Next Coder IQ1_M (strata) is the other one. - Send
{"model": "gemma-4-26b-a4b-ud-q3_k_m", "messages": [{"role": "user", "content": "Say OK."}], "max_tokens": 20, "strata_mcp": true}to/v1/chat/completions. - Send the same body with
"model": "qwen3.8-flash-next-coder-iq1_m".
Expected: the Qwen request is rendered with Qwen's template and tokenizer, with its own tool list. That is 34,040 prompt tokens in my setup.
Actual: the server switches to Qwen, but Qwen's engine receives the Gemma prompt.
gemma+tools prompt 1475
qwen+tools prompt 1475 <- Gemma's ids
Qwen's engine log confirms what it received:
strata serve: prompt 1475 tokens = 0 reused + 1475 read in 20277 ms (72.7 tok/s), 5 generated in 481 ms (10.4 tok/s)
After clearing the cache on a switch (workaround below), the same two requests give 1,475 for Gemma and 34,040 for Qwen.
Caveat, so nobody chases the wrong thing: my install wraps Service.render_prompt to give each model a different subset of MCP tools, which is why the token counts differ so much. The bug does not depend on that wrapper: the cache is consulted in encode_prompt() before render_prompt() runs, so any identical request across a switch gets the previous model's ids. I only measured it with the wrapper in place.
Workaround I use (outside the repository)
encode_prompt = server.Service.encode_prompt
def encode_for_this_model(service, *args, **kwargs):
cache = getattr(service, "_exact_prompt_cache", None)
if cache is not None and getattr(service, "_cache_model", None) != service.model:
with service._prompt_cache_lock:
cache.clear()
service._cache_model = service.model
return encode_prompt(service, *args, **kwargs)
server.Service.encode_prompt = encode_for_this_modelA fix in the server could clear _exact_prompt_cache where the switch replaces template/tok, or add the model (or the config path) to exact_key. counter_for() returns another Service and is not affected.
Version, hardware, model
- PolyStrata
mainat 4106d1a (the poly9 merge), strata-poly prebuiltstrata-poly-windows-x64-6a6e023b1970c1e2.zip - Windows 11, NVIDIA GeForce RTX 5060 Ti 16 GB
- Gemma 4 26B-A4B UD-Q3_K_M (32K context) and Qwen3.8-Flash-Next Coder IQ1_M (64K context, int8 KV) on one server with
switch_on_request
Not tested: Linux, Mac, AMD; requests through /v1/messages; a switch between two models of the same family.
Activity
github-actions commentedon Dec 2, 2026
Thanks for the issue. To keep the tracker readable, every item needs an area prefix in the title and the parts of the issue form filled in. This one is missing:
Edit the issue (title and description) and this note goes away on its own. If it is still missing parts in 5 days it will be closed, which is not a rejection: fix it and comment
/reopen.Details: CONTRIBUTING.md