{"slug": "why-json-escaping-broke-llama-cpp-prompt-lookup-79-11-but-not-native-mtp", "title": "Why JSON escaping broke llama.cpp prompt lookup (79% –> 11%), but not native MTP", "summary": "Prompt lookup speculative decoding in llama.cpp (--spec-type ngram-mod) collapsed from 79% to 10.9% draft acceptance and delivered 0% net speedup when the same 8 Python bug-fix tasks were wrapped in JSON tool-call payloads, because escaped newlines and quotes break literal n-gram matching, according to a benchmark of more than 2,400 runs on a Mac Studio M5 Ultra 512 GB. Native Multi-Token Prediction heads in GLM 5.3 Flash, GLM 5.3 and DeepSeek V4.1 Flash were unaffected, reaching 52.9 tok/s (+63%), 37.2 tok/s (+61%) and 73.1 tok/s (1.65x wall) respectively at 79-82% acceptance. The author also found that late-November engine upgrades alone were flat (~0%), while 64K warm prompt caching cut DeepSeek cold-to-warm latency from 28.9 s to 2.0 s (14.3x) and GLM 5.3 Flash from 115.3 s to 0.58 s (198x).", "body_md": "# Why JSON Escaping Broke llama.cpp Prompt Lookup (79% to 11%), While Native MTP Hit +63%\n\nIf you run local coding agents against `llama.cpp`, prompt lookup speculative decoding (`--spec-type ngram-mod`) looks like a free lunch in plain-text code-edit benchmarks. When a model rewrites an existing Python file in plain Markdown, `ngram-mod` echoes matching spans straight out of the prompt buffer. On my Mac Studio M5 Ultra 512 GB, plain-code rewrites hit **79% draft acceptance** and **+56% faster decode**.\n\nThen I wrapped the exact same 8 automated-test Python bug-fix tasks inside real JSON tool-call payloads - the format actual terminal coding agents use to write files:\n\n``` python\n{\"path\": \"service.py\", \"content\": \"def fix(x):\\n    return \\\"ok\\\"\"}\n```\n\n`ngram-mod` acceptance collapsed from 79% to 10.9%. Net speedup dropped to 0% on both GLM 5.3 Flash and GLM 5.3.\n\n## Why Literal N-Gram Lookup Breaks Inside Agent Tool Calls\n\nPrompt lookup works by scanning the prompt's token ID buffer for exact matching n-grams and proposing the following tokens as speculative drafts. In plain Markdown code edits, the edited file in the model's output shares long identical token sequences with the source file in the prompt.\n\nWhen a coding agent serializes that exact same file edit as a JSON string value:\n\n- Actual newline tokens become literal two-character `\\n` escape sequences.\n- Double quotes become `\\\"` and backslashes become`\\\\` .\n- Leading indentation merges with escaped newline tokens into completely different BPE token IDs than the unescaped source code sitting in the prompt.\n\nBecause `ngram-mod` matches raw token IDs literally, almost every multi-token prefix disappears. Roughly 89% of drafted tokens are rejected, and wall-clock decode falls right back to speculative decoding OFF.\n\n## Native Multi-Token Prediction (MTP) Does Not Care About Escaping\n\nModels shipped with embedded Multi-Token Prediction heads (`GLM 5.3 Flash`, `GLM 5.3`, and `DeepSeek V4.1 Flash`) do not copy literal token spans from the prompt. A small learned draft head predicts upcoming tokens directly from the model's hidden states, so it predicts `\\n` and `\\\"` just as accurately as raw code.\n\n| Model & Runtime | Spec OFF | ngram-mod (JSON) | Native MTP (JSON) | MTP Accept | \n|---|---|---|---|---|\n| **GLM 5.3 Flash** (`UD-Q6_K_XL` ,`llama.cpp` ) | 32.4 tok/s | 32.4 tok/s (0%, 10.9% acc) | **52.9 tok/s (+63%)** | 79-82% | \n| **GLM 5.3** (`UD-Q2_K_XL` ,`llama.cpp` ) | 23.2 tok/s | 23.2 tok/s (0%, 10.9% acc) | **37.2 tok/s (+61%)** | 79-82% | \n| **DeepSeek V4.1 Flash** (`oQ4e-mtp` ,`oMLX` ) | 49.8 tok/s | N/A | **73.1 tok/s (1.65x wall)** | 81.3% (89.6% copy) | \n\n## Four Other Surprises From 2,400+ Runs on 512 GB Unified Memory\n\n1. **Late-November engine upgrades alone were flat (~0%).** Isolating`oMLX 0.7.3 -> 0.8.3` (`49.5 -> 49.8 tok/s` on DeepSeek V4.1 Flash) and`llama.cpp b12359 -> b12726` (`31.9 -> 32.5 tok/s` on GLM 5.3 Flash) with speculation OFF showed that Native MTP - not base engine updates - produced almost all of the real speedup.\n2. **64K warm prompt caching + MTP scaled cleanly:** DeepSeek dropped from`28.9 s` cold to`2.0 s` warm (`14.3x` ) at`77.1 tok/s` (+58%, 100% Nominal thermals); GLM 5.3 Flash dropped from`115.3 s` cold to`0.58 s` warm (`198x` ) at`54.8 tok/s` (+79%).\n3. **Two real places to turn MTP OFF on DeepSeek:** (a) Low effort + MTP hit a 4,096-token whitespace loop on a strict nested-enum`json_schema` test (High + MTP passed`5/5` , reported in[jundot/omlx#5710](https://github.com/jundot/omlx/issues/5710) ); (b) DeepSeek MTP peaked at 2 simultaneous clients (`92.1 tok/s` ) and saturated at 4 clients (`p95`` 63.8 -> 93.5 s` , peak`361.5 W` ), while GLM 5.3 Flash`draft-mtp` scaled smoothly to 4 clients (`77.8 agg tok/s` ,`170 GiB` RAM spare).\n4. **Warm up `oMLX` MTP once after boot:** The first MTP call compiles Metal verifier shaders (`~62.5 s` cold TTFT); subsequent warm calls drop to`0.3 s` .\n\n**Full Raw Data & Video Walkthroughs (CC BY 4.0 / MIT):**\n\n• GitHub repository (2,400+ raw per-task CSV rows, pinned SHA-256s & launch flags):\n\n[github.com/l8w8/videos](https://github.com/l8w8/videos)\n\n• Part 1 video (22 setups baseline across 5 frontier models, 12 min):\n\n[youtu.be/vF-LUF6kPsE](https://youtu.be/vF-LUF6kPsE)\n\n• Part 2 video (Native MTP, JSON escaping trap, 64K cache & concurrency, 11 min):\n\n[youtu.be/cEOhoBGDTeM](https://youtu.be/cEOhoBGDTeM)", "url": "https://wpnews.pro/news/why-json-escaping-broke-llama-cpp-prompt-lookup-79-11-but-not-native-mtp", "canonical_source": "https://l8w8.github.io/videos/json-escaping-prompt-lookup-vs-mtp/", "published_at": "2026-11-29 13:17:29+00:00", "updated_at": "2026-11-29 13:44:43.708347+00:00", "lang": "en", "topics": ["ai-tools", "ai-infrastructure", "large-language-models", "ai-agents", "mlops"], "entities": ["llama.cpp", "GLM 5.3 Flash", "GLM 5.3", "DeepSeek V4.1 Flash", "oMLX", "Mac Studio M5 Ultra", "jundot/omlx"], "also_reported_by": [], "takeaway": "Disable ngram-mod prompt lookup for JSON tool-call workloads — acceptance fell from 79% to 10.9% and speedup to 0% — and use native MTP heads instead, which held 79-82% acceptance and delivered +61% to +63% decode on GLM 5.3 Flash and GLM 5.3.", "recommendation": {"advice": "Turn off ngram-mod speculative decoding when coding agents emit JSON tool calls, and enable native MTP heads instead; the source measured 10.9% versus 79-82% acceptance and +61% to +63% decode.", "playbook": "inference-serving", "playbook_url": "https://wpnews.pro/playbooks/inference-serving"}, "alternates": {"html": "https://wpnews.pro/news/why-json-escaping-broke-llama-cpp-prompt-lookup-79-11-but-not-native-mtp", "markdown": "https://wpnews.pro/news/why-json-escaping-broke-llama-cpp-prompt-lookup-79-11-but-not-native-mtp.md", "text": "https://wpnews.pro/news/why-json-escaping-broke-llama-cpp-prompt-lookup-79-11-but-not-native-mtp.txt", "jsonld": "https://wpnews.pro/news/why-json-escaping-broke-llama-cpp-prompt-lookup-79-11-but-not-native-mtp.jsonld"}}