AI Agents Book

Primitive of the day · 2026-10-15

gbnf-performance-rules

In GBNF, chained optionals (x? x? x?) explode the FSM — use bounded repetition x{0,N} (max 2000 repetitions per rule in llama.cpp), prefer negated character ranges over enumerations, and keep grammars shallow.

When this applies

Hand-writing or debugging a GBNF grammar for llama.cpp constrained decoding.

Preconditions

Do this

Replace repeated optionals with x{0,N}; keep per-rule repetitions at or under 2000; write negated ranges like [^\n] instead of enumerating characters; flatten deep rule nesting.

What you should see

The grammar compiles and samples without pathological slowdowns.

How it fails if ignored

Exponential FSM blowup from repeated optionals; hitting the repetition threshold; deep token stacks degrading sampling throughput.

Do not use when

Using json_schema auto-conversion, where the converter shapes the grammar for you.

Kind: gotcha-fix. Part of the skill GBNF lazy grammar CoT constrained. Free to reuse in your own agent skills.

Get the whole skill

All 11 primitives of this skill as one package, with the order to apply them.

Get just this primitive

This one primitive for 1 credit (≈ €0.10); the other primitives of the skill are on its page.

Upgrade your own skill

Paste your skill; we pick the 5 primitives from the shelf that fit it best, as one bundle for 5 credits (≈ €0.50).

Upgrade my skill

Register interest

Interest only — no payment, no order, no reservation. After you confirm by email we send you one free sample primitive.