GBNF structured CoT: fixing Qwen3.6 rumination with a 4-rule grammar (12 new rows)

#3
by witcheer - opened

GBNF structured CoT: fixing Qwen3.6 rumination with a 4-rule grammar

this dataset now includes 12 new rows from a GBNF grammar experiment on Qwen3.6-35B-A3B (2026-05-21). the experiment validates that constrained decoding eliminates the rumination problem that made this model unusable for agentic work.

the problem

in our agentic coding tests (rows 1-17), Qwen3.6-35B-A3B was reliable but extremely slow: 18-87 minutes per task. the root cause is rumination: the model spends most of its token budget on reasoning and never gets to the actual answer. in direct code generation tests, free-form Qwen3.6 hit the 2048 token limit on all 3 easy coding tasks and produced zero usable code on 2 of 3.

the fix

a 4-rule GBNF grammar forces the model into a strict think-then-code pattern:

root  ::= think code
think ::= "<think>\n" "GOAL: " line "APPROACH: " line "EDGE: " line "</think>\n\n"
line  ::= [^\n]+ "\n"
code  ::= [\x09\x0A\x0D\x20-\x7E]+

the grammar constrains the full output stream, including reasoning_content. the model must state its goal, approach, and edge cases in exactly 3 lines, then produce code. no room for multi-paragraph deliberation.

credit to andthattoo/structured-cot for the original research showing this works on LiveCodeBench.

results: easy tasks (grammar vs free-form)

task free tokens grammar tokens compression free time grammar time code produced
fibonacci 2048 (max) 365 5.6x 63.6s 12.2s free: 0 lines, grammar: valid
fizzbuzz variant 2048 (max) 179 11.4x 63.9s 5.9s free: buried in reasoning, grammar: valid
flatten dict 2048 (max) 450 4.6x 64.5s 15.2s free: 0 lines, grammar: valid
total 6144 994 6.2x 192s 33.3s

all grammar outputs pass ast.parse() syntax validation. the code is correct: proper lru_cache for fibonacci, correct divisibility logic for fizzbuzz, recursive flattening for nested dicts.

results: hard tasks (3-field vs 5-field grammar)

we also tested a 5-field variant (GOAL/STATE/ALGO/EDGE/VERIFY) on harder problems:

task 3-field tokens 5-field tokens 3f time 5f time both valid
topological sort 437 545 15.0s 17.2s yes
LRU cache (O(1)) 964 930 31.8s 29.3s yes
merge intervals 319 341 11.5s 10.8s yes

the 5-field grammar adds marginal value: caught a node-collection edge case on topo sort, used __slots__ on the LRU cache node class. but the token cost increase is not worth it for general use. the 3-field grammar (GOAL/APPROACH/EDGE) is the sweet spot.

example: what grammar-constrained reasoning looks like

task: fizzbuzz variant (179 tokens, 5.9 seconds)

<think>
GOAL: Write a Python function that takes a list of integers and returns a new list where:
APPROACH: Iterate through the list, check divisibility by 3 and 5, replace accordingly, keep others as integers.
EDGE: Empty list, list with no multiples, list with all multiples.
</think>

def fizz_buzz_list(numbers: list[int]) -> list:
    result = []
    for num in numbers:
        if num % 3 == 0 and num % 5 == 0:
            result.append('FizzBuzz')
        elif num % 3 == 0:
            result.append('Fizz')
        elif num % 5 == 0:
            result.append('Buzz')
        else:
            result.append(num)
    return result

278 chars of reasoning, then straight to correct code. free-form used 6613 chars of reasoning and hit the token limit.

how to reproduce

  1. serve Qwen3.6-35B-A3B with llama-server (or any server supporting the grammar API parameter)
  2. pass the GBNF grammar string in the grammar field of your API request
  3. grammar files are included in source/grammars/ in this repo

server launch example:

llama-server -m Qwen3.6-35B-A3B-Q4_K_M.gguf \
  -c 16384 -ngl 999 -ncmoe 30 --jinja

API request example:

import requests

GRAMMAR = r"""root  ::= think code
think ::= "<think>\n" "GOAL: " line "APPROACH: " line "EDGE: " line "</think>\n\n"
line  ::= [^\n]+ "\n"
code  ::= [\x09\x0A\x0D\x20-\x7E]+"""

body = {
    "messages": [
        {"role": "system", "content": "You are a Python coding assistant."},
        {"role": "user", "content": "Write a function that ..."},
    ],
    "max_tokens": 2048,
    "temperature": 0.0,
    "grammar": GRAMMAR,
}
r = requests.post("http://localhost:8080/v1/chat/completions", json=body)

key findings

  1. grammar constrains the full output stream, including what llama-server reports as reasoning_content. enable_thinking has no additional effect when grammar is active.
  2. the model already knows the answer. it just needs to stop overthinking. the grammar doesn't reduce quality, it just prevents the model from wasting tokens on deliberation.
  3. 3-field is enough. GOAL/APPROACH/EDGE covers the essential structured reasoning. adding STATE/ALGO/VERIFY adds marginal value on hard tasks but not enough to justify the extra tokens.
  4. not yet tested for agentic tool calls. this grammar constrains output to think + code. applying it to an agent loop would require a grammar that also handles JSON tool calls (think (code | tool_call)).

hardware

  • GPU: NVIDIA RTX 4060 Ti 8GB
  • CPU: AMD Ryzen 5 7600X
  • RAM: 32 GB DDR5
  • OS: Windows 11 + WSL2 (Ubuntu)
  • inference: llama-server (turboquant fork of llama.cpp), ncmoe=30

data

rows 18-29 in agentic-coding-bench.jsonl contain the grammar experiment. filter by framework == "direct-api" and reasoning_mode values grammar-3field, grammar-5field, or free.

Sign up or log in to comment