# Constrained sampling

> Constrained sampling: At each generation step, masking every token that would break a rule, grammar or schema, then renormalising the remaining…

Canonical: https://llmdojo.dev/glossary/constrained-sampling

Also called constrained decoding, grammar-constrained sampling.

At each generation step, masking every token that would break a rule, grammar or schema, then renormalising the remaining probabilities before sampling. Guarantees format, not correctness.

Learn it in the lesson: [Constrained sampling: mask, then renormalise](https://llmdojo.dev/learn/prompt-context-engineering/tools-and-constrained-outputs/masking-the-next-token) (Prompt and Context Engineering · Tool Instructions and Constrained Outputs).

## Interview questions about Constrained sampling

- [Explain one step of constrained sampling. What does it guarantee, and what doesn't it?](https://llmdojo.dev/interview/q/explain-one-step-of-constrained-sampling-what-does-it-guarantee-and-what)
- [Should you turn on grammar-constrained decoding for every structured output? What would you watch?](https://llmdojo.dev/interview/q/should-you-turn-on-grammar-constrained-decoding-for-every-structured-output-what)
- [Where in the stack can you push a model towards a format, and what would you use for a classifier that must never invent a label?](https://llmdojo.dev/interview/q/where-in-the-stack-can-you-push-a-model-towards-a-format)
- [Your LLM writes documents that must follow a strict Word formatting schema: styles, heading levels, tables. How do you make its output conform?](https://llmdojo.dev/interview/q/your-llm-writes-documents-that-must-follow-a-strict-word-formatting-schema)

## Related
- [Tool calling](https://llmdojo.dev/glossary/tool-calling)
- [Glossary of every term](https://llmdojo.dev/glossary)
