# Why JSON escaping broke llama.cpp prompt lookup (79% –> 11%), but not native MTP

> Source: <https://l8w8.github.io/videos/json-escaping-prompt-lookup-vs-mtp/>
> Published: 2026-11-29 13:17:29+00:00

**Recommendation:** 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.
More in the playbook "Inference and self-hosting": <https://wpnews.pro/playbooks/inference-serving>

# Why JSON Escaping Broke llama.cpp Prompt Lookup (79% to 11%), While Native MTP Hit +63%

If 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**.

Then 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:

``` python
{"path": "service.py", "content": "def fix(x):\n    return \"ok\""}
```

`ngram-mod` acceptance collapsed from 79% to 10.9%. Net speedup dropped to 0% on both GLM 5.3 Flash and GLM 5.3.

## Why Literal N-Gram Lookup Breaks Inside Agent Tool Calls

Prompt 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.

When a coding agent serializes that exact same file edit as a JSON string value:

- Actual newline tokens become literal two-character `\n` escape sequences.
- Double quotes become `\"` and backslashes become`\\` .
- Leading indentation merges with escaped newline tokens into completely different BPE token IDs than the unescaped source code sitting in the prompt.

Because `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.

## Native Multi-Token Prediction (MTP) Does Not Care About Escaping

Models 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.

| Model & Runtime | Spec OFF | ngram-mod (JSON) | Native MTP (JSON) | MTP Accept | 
|---|---|---|---|---|
| **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% | 
| **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% | 
| **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) | 

## Four Other Surprises From 2,400+ Runs on 512 GB Unified Memory

1. **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.
2. **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%).
3. **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).
4. **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` .

**Full Raw Data & Video Walkthroughs (CC BY 4.0 / MIT):**

• GitHub repository (2,400+ raw per-task CSV rows, pinned SHA-256s & launch flags):

[github.com/l8w8/videos](https://github.com/l8w8/videos)

• Part 1 video (22 setups baseline across 5 frontier models, 12 min):

[youtu.be/vF-LUF6kPsE](https://youtu.be/vF-LUF6kPsE)

• Part 2 video (Native MTP, JSON escaping trap, 64K cache & concurrency, 11 min):

[youtu.be/cEOhoBGDTeM](https://youtu.be/cEOhoBGDTeM)
