Skip to content

Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

glm53: replay a bare tool-call turn as the model wrote it (#1576)

JustVugg3821fcf1e66c
diff --git a/c/openai_server.py b/c/openai_server.py
index 7476fc08..97ce64fc 100644
--- a/c/openai_server.py
+++ b/c/openai_server.py
@@ -1995,8 +1995,22 @@ def render_chat_glm53(messages, enable_thinking=False, reasoning_effort=None, to
reasoning = content.split("</think>")[0].split("<think>")[-1]
content = content.split("</think>")[-1]
opened = f"<think>{reasoning}</think>" if isinstance(reasoning, str) else "<think></think>"
- prompt.append(f"<|assistant|>{opened}{content.strip()}"
- f"{_glm53_tool_calls(message.get('tool_calls'))}")
+ body = content.strip()
+ calls = _glm53_tool_calls(message.get("tool_calls"))
+ # The template writes "\n<tool_call>". The model, on a turn that is
+ # nothing but a tool call, writes "</think><tool_call>" with no
+ # newline between them, and that one token is enough to throw away
+ # the whole cached prefix: the reuse gate in glm53.c is
+ # all-or-nothing, so the next turn re-prefills from scratch -- on a
+ # 3k-token agent history at 2.3 tok/s, twenty minutes (#1576).
+ #
+ # A turn that also has text is left exactly as it was. There the
+ # model's own trailing newline is stripped by .strip() and put back
+ # by the renderer, so the tokens already line up, and changing that
+ # case would break the one that works today.
+ if calls and not body:
+ calls = calls.lstrip("\n")
+ prompt.append(f"<|assistant|>{opened}{body}{calls}")
else:
raise APIError(400, f"unsupported message role {role!r}.", "messages")
diff --git a/c/tests/test_openai_server.py b/c/tests/test_openai_server.py
index b08d5dff..052fb944 100644
--- a/c/tests/test_openai_server.py
+++ b/c/tests/test_openai_server.py
@@ -2043,6 +2043,44 @@ class ThinkingSplitUnitTest(unittest.TestCase):
self.assertFalse(srv.starts_in_reasoning(False),
"GLM-5.2 closes the block in the prompt when thinking is off")
+ def test_glm53_bare_tool_call_turn_matches_what_the_model_wrote(self):
+ """#1576: a replayed assistant turn has to be the tokens the model made.
+
+ The official template writes "\\n<tool_call>" and GLM-5.3 does not: on a
+ turn that is nothing but a tool call it writes "</think><tool_call>".
+ Rendering the newline anyway put one extra token into the replayed
+ prefix, and the reuse gate in glm53.c is all-or-nothing, so the entire
+ cached prefix went and the turn re-prefilled from scratch. The reporter
+ measured twenty minutes of it on a 3k-token agent history.
+
+ The turn that also carries text keeps its newline, and that is not an
+ oversight: there the model's own trailing newline is stripped and put
+ back, the tokens line up, and it is the case that works today.
+ """
+ import openai_server as srv
+ bare = srv.render_chat_glm53([
+ {"role": "user", "content": "list the files"},
+ {"role": "assistant", "content": "",
+ "tool_calls": [{"type": "function",
+ "function": {"name": "bash",
+ "arguments": '{"command": "ls"}'}}]},
+ {"role": "tool", "content": "a.txt"},
+ ])
+ self.assertIn("</think><tool_call>bash", bare,
+ "a bare tool call must follow </think> with no newline")
+ self.assertNotIn("</think>\n<tool_call>", bare)
+
+ with_text = srv.render_chat_glm53([
+ {"role": "user", "content": "list the files"},
+ {"role": "assistant", "content": "Let me look.",
+ "tool_calls": [{"type": "function",
+ "function": {"name": "bash",
+ "arguments": '{"command": "ls"}'}}]},
+ {"role": "tool", "content": "a.txt"},
+ ])
+ self.assertIn("Let me look.\n<tool_call>bash", with_text,
+ "a turn with text keeps the separator it already had")
+
def test_missing_close_tag_surfaces_reasoning(self):
self.assertEqual(split_thinking_reply("thought with no end"),
("thought with no end", ""))