DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Fresh Data for AI With Spring AI Function Calls
  • Who Should Grade Your AI? Rules vs LLM Judges vs Humans
  • Threat Modeling Context-Aware AI: A STRIDE Walkthrough
  • Classification Never Left. It Just Got a New Home in LLMs.

Trending

  • Beyond the Wall of Charts: How Advanced Observability Displaced the Telemetry Dashboard
  • A Non-Expiring AKS Deployment Token Could Read Secrets Across Three Namespaces
  • Guess the Speed: Building a Family Internet Speed Guessing Game
  • After the AI Commit: What Eight Retrospectives Taught Me About Working With Coding Agents

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Constrained Decoding Models Explained: Encoders, Decoders and When to Use Them

Constrained Decoding Models Explained: Encoders, Decoders and When to Use Them

A constrained decoding model (CDM) is a normal decoder LLM with a rule engine attached. The engine blocks every word piece that would break your schema.

By 
Vidyasagar (Sarath Chandra) Machupalli FBCS
DZone Core CORE ·
Nov. 05, 26 · Analysis
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
77 Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Have you ever asked an LLM for JSON and got back "Sure! Here is your JSON:" followed by the JSON? Your parser fails, your pipeline stops, and someone adds a retry loop.

I have been there. For a long time, the fix was a polite prompt. "Reply with valid JSON only. No extra words." It works most days. On the other days, it breaks.

There is a better fix, and it does not rely on the model behaving well. It is called constrained decoding.

In this article, you will learn:

  • What encoders and decoders are, and when to use each
  • How constrained decoding works, in plain words
  • Which tools do it today
  • What research says about its strengths and limits
  • How it compares with a plain LLM, and with new decision models like Jev and Laya
  • How to use it safely inside an agent

A quick note on the name. In this article, CDM means constrained decoding model: a decoder LLM run together with a constraint engine. CDM is the label I use for this setup. It is not a product name.

Encoder and Decoder in Plain Words

Almost every modern language model is built from blocks called transformers. What changes is how the blocks are arranged. A survey of language models for software work describes three layouts: BERT as encoder-only, LLaMA as decoder-only, and CodeT5+ as encoder plus decoder (arXiv 2408.02479).

Encoder and Decoder

Encoder and Decoder

Here is the easy way to remember it.

An encoder is a reader. It looks at the whole message at once, and every word can see the words on both sides. The output is understanding, a set of numbers that capture meaning. That is why encoders are good at jobs like classifying, routing, searching, and filtering. Researchers who built small safety checkers note that encoder models are efficient at narrow jobs such as classification, name tagging, and sentiment (arXiv 2410.08442).

A decoder is a writer. It produces one small piece of text, called a token, at a time. Each new piece depends on what it already wrote. Chat models like Llama work this way. Decoders became popular because they are flexible: almost any task can be turned into "write the answer."

An encoder plus decoder reads one thing and writes another, like a translation or a summary. T5 and CodeT5+ are examples.

If you need to Use Why
Label, filter or route text Encoder, or a decision model built on one Reads once, small, cheap
Search by meaning Encoder Produces meaning numbers
Write, explain, plan or code Decoder (LLM) Generates new text
Make a decoder answer in a fixed shape Decoder plus constrained decoding Keeps the writer inside the rules
Exact lookups, math or dates Plain code Fast and easy to check


A CDM sits in the fourth row. Jev and Laya are closer to the encoder idea. Laya, for example, is built on a ModernBERT encoder with a decision layer on top (Laya model card).

What Is Constrained Decoding?

A decoder picks its next token by scoring every token it knows. Constrained decoding adds a step. Before the model picks, a rule engine blocks the tokens that would break your rules. The XGrammar paper describes this as restricting the available tokens at each step (arXiv 2411.15100).

How CDM works

How CDM works


The loop has five steps:

  1. Score: the model scores every possible next token.
  2. Check: the rule engine works out which tokens are legal at this spot.
  3. Mask: illegal tokens are blocked.
  4. Pick: the model chooses from the legal tokens only.
  5. Repeat: the token is added, and the loop runs again until the answer is complete.

The values in the close-up part of the picture are made up to show the idea. What matters is the shape of the process: blocked tokens disappear, and the remaining choices share the probability.

What Kinds of Rules Can You Use?

The common ones are a JSON schema, a regular expression, a fixed list of choices, or a grammar. vLLM, a popular serving engine, lists all four (vLLM docs).

The Outlines library shows how simple this can be. For a yes-or-no answer, you use a Literal["Yes", "No"] type. For a sentiment label, you use Literal["Positive", "Negative", "Neutral"] (Outlines on GitHub). The model can no longer return anything else.

That means a choice constraint turns a general LLM into a classifier that can never invent a label.

One Thing to Remember

Masking controls what the model is allowed to write. It does not change what the model understands. Everything in the research section below comes back to this point.

Tools You Can Use Today

The tools fall into three groups: engines that compute the mask, servers that run them, and hosted APIs that hide them.

Tool What it is Start here
Outlines A Python library that turns types, schemas or regex into a state machine that masks tokens Paper, GitHub
XGrammar An engine that prepares most of the mask ahead of time to keep overhead low. XGrammar-2 targets rules that change at runtime Paper, XGrammar-2
llguidance (Guidance) A fast engine written in Rust that handles full grammars and JSON schemas GitHub
llama.cpp grammars Grammar support for local models, using GBNF Grammar guide
vLLM A serving engine that uses XGrammar or Guidance as backends Docs
NVIDIA NIM Containerized serving with JSON schema, regex, grammar, and choice support Docs
OpenAI Structured Outputs A hosted option that enforces a JSON schema in strict mode Announcement
Claude structured outputs A hosted option that compiles your JSON schema into a grammar Docs


Two things to watch for.

Hosted APIs support a part of JSON Schema, not all of it. For example, Google's documentation for Claude on Vertex AI says some constraints, such as numeric and string-length rules, are not supported (Vertex AI docs). If a rule is not enforced by the mask, check it in your own code.

Your backend can change under you. vLLM's configuration notes say the automatic backend choice can change between releases (vLLM config). If you need repeatable behavior, set the backend yourself and pin the version.

What the Research Says

I read the main papers so you do not have to. Several 2026 papers are preprints, which means they have not been peer-reviewed yet. Here are the five points that matter most.

1. A valid format becomes a guarantee. A 2026 preprint on small open models found that constrained decoding removed format failures such as wrong types and missing fields (arXiv 2609.23742). If a program must parse the output, this is the main reason to use it.

2. Valid is not the same as right. The same paper found that the content can still be wrong or thin. A model that skips a required step still passes the schema if the schema allows it. The authors describe rescued answers that look complete but carry little meaning. The short version: constraints fix form, not understanding.

3. Real schemas are harder than demos. JSONSchemaBench tested several engines, including Guidance, Outlines, llama.cpp, XGrammar, OpenAI, and Gemini, on a large set of real-world schemas. Engines differ in which schema features they support (arXiv 2501.10868). That benchmark is from early in this wave of tools, and engines have improved, so test your own schemas on the engine you plan to use.

4. Strict formats can hurt reasoning. The paper "Let Me Speak Freely?" found that format restrictions can weaken reasoning, and stricter formats can do more harm. It also found that constraints can help classification (arXiv 2408.02442). In reasoning tasks, JSON mode often did not keep the order of "think first, answer second." So if the model must think, let it think first.

5. Masking can nudge what the model prefers. Research on grammar-aligned decoding shows that blocking tokens can distort the model's own probabilities, so some outputs are valid but lower in quality (arXiv 2405.21047). A 2026 preprint on draft-conditioned decoding targets a related problem, where masking pushes the model down a path that is valid but wrong (arXiv 2603.03305).

Finding What it means What to do
Format failures disappear Output always fits the rules Use it wherever a program parses the output
Valid can still be wrong Meaning is not checked Test the content, not only the format
Engines support different schema features "Supports JSON schema" is not one yes or no Test your schemas on your engine
Strict formats can hurt reasoning Early constraints take away thinking room Let the model think, then constrain the final answer
Masking can bend probabilities Open-ended content can lose quality Watch quality on long or creative output


CDM vs. LLM vs. Jev and Laya

Now we can compare the designs. All three can give you a valid answer from a fixed list. They get there in different ways.


LLM CDM Decision model
Core design Decoder Decoder plus mask Encoder plus decision layer
How it answers Writes token by token Writes token by token, legal tokens only Scores every allowed answer in one pass
Output always valid No Yes, inside the rules the engine supports Yes, it picks from your list
Probabilities Not by default Token level, not trained to be calibrated Trained to be calibrated
Can write or explain Yes Yes, inside the shape No
Open shapes like SQL or nested JSON Yes Yes No, typed answers only
Main risk Broken format Valid but wrong, reasoning loss Confident wrong picks, forced answers


What Are Jev and Laya?

They are decision models, sometimes called System One models, after the fast, gut-level mode of thinking. You give them an input, a question, and the allowed answers. They return probabilities for each answer in one pass. They do not write text.

Jev comes from TypeSafe AI and is offered as a hosted service. Its evaluation paper describes typed answers: a choice, a position on a rubric, or a true-or-false probability (arXiv 2609.37647). Laya comes from Convai Innovations and is open, so you can host and fine-tune it yourself. Its model card explains that it is trained so that its probabilities are honest, a method it calls RLCD (Laya model card). I covered both in Classification Never Left. It Just Got a New Home in LLMs.

How Do They Differ From a CDM?

Think of a quiz with four answers.

A CDM is a writer who is only allowed to write one of the four answers. It still goes through the writing loop, and it can also handle open-ended formats like SQL.

A decision model reads the question once and hands you a score for each of the four answers. It cannot write anything else, and it is built for calibrated scores.

ScaleDown's CDMs, covered in the next section, are a third variant. They are small models trained for one task, so the limit on what they can return is built in during training instead of added by a mask at run time.

What About a Head-to-Head Test?

I looked for a direct comparison of a constrained LLM against Jev or Laya on the same data and did not find one. So I will not name a winner. If you are choosing between them, test both on a few hundred of your own inputs.

One weakness is shared. A closed list forces an answer. If a message fits none of your categories, both a decision model and a constrained LLM will still pick one. This follows from how the list works, and it is why you should always add an "other" option and test it on purpose.

When to Use What

When to use what - LLM vs CDM vs Decision Model

When to use what - LLM vs CDM vs Decision Model


Walk the questions from left to right and stop at the first yes.

Job Best fit Why
Tool call arguments for an agent CDM Strict JSON that must parse
SQL, regex or a small language CDM with a grammar Open shape with clear rules
Pulling fields out of text into a schema CDM Type and structure errors disappear
Ticket routing at high volume Decision model, or a trained encoder classifier One pass, cheap, probabilities
Urgency score or yes or no flag Decision model Typed scores
Reasoning, then a structured answer LLM thinks freely, then a constrained last step Avoids the reasoning loss
Summaries, replies and explanations LLM You need words
Exact rules, lookups, math and dates Plain code Fast and easy to check


Two rules of thumb:

  1. If the model must think, let it think first. Give it a free reasoning step, then constrain only the final answer. If you must use one schema, put the reasoning field before the answer field.
  2. If the answer is a pick, constrain it tightly. Picks are where constraints help most.

How ScaleDown Brought CDMs Into Agents

A recent post from LangChain shows where this idea is heading. Long-running agents spend a lot of effort on context management: summarizing old history, shrinking large tool results, and pulling the right values out of long inputs. Each of those is a small decision, and making them with a large LLM on every turn is slow and costly.

ScaleDown's answer is a set of CDMs. Instead of writing any string one token at a time, each model is trained for one job and only returns answers that job allows, such as a label from your list or pieces of the input text. ScaleDown offers models to summarize, compress, extract, and classify, and the langchain-scaledown package wraps them as middleware for LangChain and Deep Agents. The big LLM keeps the planning and writing, and the CDMs handle the high-volume steps around it (LangChain blog).

Its classify model will feel familiar if you have read about Jev. Both return a typed answer with a probability, and the post says ScaleDown also adds a short reason, which helps when you debug why an agent stopped or routed a task. The post also notes that ScaleDown is now available through the LangSmith LLM Gateway, so its calls are traced like your other model calls.

One caution. This is a vendor-written post, and the speed and cost claims come from ScaleDown's own benchmarks, so test them on your own workload before you rely on them.

Using a CDM Inside an Agent

The two designs work well together. A CDM keeps the agent's tool call valid. A decision model, or a second choice constraint, decides whether that call is safe to run.

CDM inside an agent

CDM inside an agent


Here is how it flows:

  1. A user or system sends a request.
  2. The LLM agent plans the next step and proposes a tool call.
  3. Constrained decoding makes sure the call matches the schema, so it always parses.
  4. A decision gate scores the call as allow, review, or block.
  5. Allowed calls run, unsure calls go to a person, and risky calls are rejected and logged.

Three habits keep this honest:

  1. Test two things. Check that the output is valid, and check that it is right. They are not the same.
  2. Log every step. Keep the proposed call, the schema it matched, the gate's scores, and who approved it.
  3. Treat the gate as one control. Give the agent only the access it needs, and require a human for high-impact actions.

I wrote more about these controls in AI-Era Cybersecurity, Part 2, and about the tools that sit beside them in Part 1.

A Simple Checklist

Step Action
1 List every place your system needs a fixed shape or a pick from a list
2 Move anything plain code can decide out of the model
3 Choose a rule type: choice, regex, JSON schema or grammar
4 Stay inside what your engine supports, and check the rest in code
5 Add an "other" or "unknown" option to every closed list
6 Put reasoning before the answer, or reason in a separate step
7 Pin the engine, backend and model version
8 Test format and meaning on real inputs, including ones that fit no category
9 Cache compiled schemas, or use an engine built for rules that change per request
10 Log decisions and recheck on a schedule


Limits of This Article

  • Some of the newest evidence is in preprints and small studies. Treat it as a signal, not a final answer.
  • Hosted APIs do not publish their full mask engines, so I rely on their documentation.
  • I found no same-data comparison between a constrained LLM and a decision model, so the comparison here rests on design differences and separate sources.
  • Tools in this area change quickly. Check each official page before you decide.

Related Articles From Me

Article How it connects
Classification Never Left. It Just Got a New Home in LLMs The decision model side of this comparison
Shipping Production-Grade AI Agents (Refcard #403) Guardrails and evaluation, where a CDM fits
A Field Guide to AI Agent Frameworks Where structured output plugs into frameworks
Graph Engineering: The Layer After Loop Engineering A constrained choice can pick the next node in a graph
AI-Era Cybersecurity, Part 2 Agent identity, approval gates and measuring risk
AI-Era Cybersecurity, Part 1 The bug-fixing tools these gates sit beside
How AI Is Actually Changing SRE Tools, Part 2 Alert triage is a pick-from-a-list problem
From Prompts to Harnesses: How AI Engineering Has Grown Up The harness around the model is where constraints live


Conclusion

A CDM is a decoder with guardrails. It gives you a promise that is easy to state and easy to test: the output will always fit the rules. It does not promise that the answer is right, and strict rules can cost you reasoning if you apply them too early.

Decision models like Jev and Laya make a different trade. They give up writing in return for fast, one-pass scoring with probabilities built to be honest. They have their own risks, such as confident wrong picks and forced answers.

So match the tool to the shape of the job. Constrain tightly when the output must parse, or the answer is a pick. Leave the model free when it needs to think. And test both the format and the meaning on your own data before you trust either one.

If you try this in your own system, I would like to hear which part you tried first. Share it in the comments.

References

Papers

  • Efficient Guided Generation for Large Language Models (Outlines)
  • XGrammar: Flexible and Efficient Structured Generation Engine
  • XGrammar-2: Dynamic and Efficient Structured Generation Engine for Agentic LLMs
  • JSONSchemaBench
  • Let Me Speak Freely? Format Restrictions and LLM Performance
  • Grammar-Aligned Decoding
  • The Hidden Cost of Structured Generation: Draft-Conditioned Constrained Decoding
  • Constrained Decoding Eliminates Structural Failures in Small LLMs
  • From LLMs to LLM-based Agents for Software Engineering: A Survey
  • JurEE not Judges: Small, Specialized Encoder Ensembles
  • Evaluating and Benchmarking the System One Model Jev

Tools and Documentation

  • Outlines
  • XGrammar
  • llguidance
  • llama.cpp grammars
  • vLLM structured outputs
  • NVIDIA NIM structured generation
  • OpenAI Structured Outputs announcement
  • Claude structured outputs
  • Claude structured outputs on Vertex AI

ScaleDown and LangChain

  • Building Long-Running Agents with ScaleDown and Deep Agents, LangChain blog
  • ScaleDown

Decision Models

  • Laya model card, Hugging Face
  • Jev: Decision Models on Trial, Cisco Blogs
AI JSON large language model

Opinions expressed by DZone contributors are their own.

Related

  • Fresh Data for AI With Spring AI Function Calls
  • Who Should Grade Your AI? Rules vs LLM Judges vs Humans
  • Threat Modeling Context-Aware AI: A STRIDE Walkthrough
  • Classification Never Left. It Just Got a New Home in LLMs.

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook