Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

serve: DeepSeek-V4.1 marks the system prompt and tools as the shared start (pin=N) so a sub-agent goes back to it

Niko1221b8f9c63ae743
diff --git a/serve/server.py b/serve/server.py
index 19e03a2be..8141108d5 100644
--- a/serve/server.py
+++ b/serve/server.py
@@ -4894,10 +4894,30 @@ class Service:
# (an encoder restart, the cache's eviction) with other bits still matches its checkpoints
self.embeddings.keys = [Path(p).stem[:16] for p, _ in encoded]
write_temporary(combined, [p for p, _ in encoded])
+ if self.family == "deepseek41" and req is not None and req.get("strata_prefix") is None and not images:
+ lead = self.deepseek_shared_start(messages)
+ if lead:
+ req["strata_prefix"] = {"messages": lead}
if req is not None and req.get("strata_prefix") is not None:
req["strata_prefix"] = self.resolve_prefix(req["strata_prefix"], messages, tools, kwargs, ids, bool(images))
return ids, kwargs.get("enable_thinking", True) is not False, max_new
+ @staticmethod
+ def deepseek_shared_start(messages) -> int:
+ """DeepSeek-V4.1 only: how many leading messages are the system prompt (its tools are rendered with them). An
+ agent and its sub-agents that work behind the same system prompt and tools share exactly this start, so the
+ engine keeps a checkpoint of its state there (pin=N): the next agent behind the same start goes back to it
+ instead of reading it again. 0: none (STRATA_DS4_AUTO_PIN=0, no system message, or nothing after it)."""
+ if os.environ.get("STRATA_DS4_AUTO_PIN", "1") == "0":
+ return 0
+ n = 0
+ for m in messages:
+ if isinstance(m, dict) and m.get("role") in ("system", "developer"):
+ n += 1
+ else:
+ break
+ return n if 0 < n < len(messages) else 0
+
PREFIX_END = "\u0001strata-prefix-end\u0001" # marks where a message's shared part ends in the rendered prompt
def resolve_prefix(self, spec, messages, tools, kwargs, ids, has_images=False) -> dict:
diff --git a/serve/test_dsv41.py b/serve/test_dsv41.py
index bc798034c..a4c13bcd9 100644
--- a/serve/test_dsv41.py
+++ b/serve/test_dsv41.py
@@ -527,6 +527,54 @@ class Api(unittest.TestCase):
self.assertEqual(last["decode_tok_s"], 100.0)
+ # -- the shared start of an agent's and its sub-agents' prompts (the engine keeps its state there) ------------
+ def test_the_system_prompt_and_tools_are_the_shared_start(self):
+ self.start("ok")
+ sysmsg = {"role": "system", "content": "You are an agent. " * 20}
+ body = {"messages": [sysmsg, {"role": "user", "content": "task one"}], "tools": [WEATHER], "max_tokens": 40}
+ self.assertEqual(self.post("/v1/chat/completions", body)[0], 200)
+ n = self.engine.last_sampling["strata_prefix"]["tokens"]
+ ids = self.engine.last_prompt
+ self.assertTrue(0 < n < len(ids) - 1)
+ self.assertIn("get_weather", self.tok.decode(ids[:n])) # the tools are in the shared start
+ self.assertNotIn("task one", self.tok.decode(ids[:n])) # the first user message is not
+ # another agent behind the same system prompt and tools, another task: the same boundary, the same start
+ body["messages"] = [sysmsg, {"role": "user", "content": "a different task for the sub-agent"}]
+ self.assertEqual(self.post("/v1/chat/completions", body)[0], 200)
+ self.assertEqual(self.engine.last_sampling["strata_prefix"]["tokens"], n)
+ self.assertEqual(self.engine.last_prompt[:n], ids[:n])
+
+ def test_no_shared_start_without_a_system_prompt_or_when_switched_off(self):
+ self.start("ok")
+ self.chat()
+ self.assertNotIn("strata_prefix", self.engine.last_sampling)
+ body = {"messages": [{"role": "system", "content": "s"}, {"role": "user", "content": "u"}], "max_tokens": 40}
+ old = os.environ.get("STRATA_DS4_AUTO_PIN")
+ os.environ["STRATA_DS4_AUTO_PIN"] = "0"
+ try:
+ self.post("/v1/chat/completions", body)
+ self.assertNotIn("strata_prefix", self.engine.last_sampling)
+ finally:
+ if old is None:
+ os.environ.pop("STRATA_DS4_AUTO_PIN", None)
+ else:
+ os.environ["STRATA_DS4_AUTO_PIN"] = old
+
+ def test_a_clients_own_prefix_wins(self):
+ self.start("ok")
+ body = {"messages": [{"role": "system", "content": "s " * 30}, {"role": "user", "content": "u"}],
+ "max_tokens": 40, "strata_prefix": {"tokens": 7}}
+ self.post("/v1/chat/completions", body)
+ self.assertEqual(self.engine.last_sampling["strata_prefix"], {"tokens": 7})
+
+ def test_the_qwen_path_asks_for_no_prefix(self):
+ # the same request on a server that is not DeepSeek's: nothing is added (Qwen keeps its own cache logic)
+ svc = Service(MockEngine(self.tok, "ok"), self.tok, ChatTemplate(ROOT / "serve" / "chat_template.jinja"))
+ req = {}
+ svc.prepare([{"role": "system", "content": "s"}, {"role": "user", "content": "u"}], None, {}, 16, req=req)
+ self.assertEqual(req, {})
+
+
class PickTemplateTests(unittest.TestCase):
"""A pack made from another GGUF can ship the V4-Flash template (`tool_calls`, no space): the V4.1 model then
writes malformed calls (P2003 install test). The server swaps in the V4.1 template for such a pack."""