6.2.10 · D1AI Agents & Tool Use

Foundations — Guardrails and constrained generation

2,003 words9 min readBack to topic

This page builds every symbol the parent note throws at you, starting from "what is a token". If a symbol appears in the parent note and you can't yet draw it, it lives here.


1. The token — the atom of everything

Picture a sentence broken into puzzle pieces:

Look at the figure. The sentence "rating": 5 is not one thing to the model — it is the ordered pieces "rating", :, 5. Each piece is one token. The model's whole life is: given the pieces so far, pick the next piece.

Why the topic needs it. Constrained generation works token by token — it decides "which pieces am I allowed to place next?" So we must know that text is a sequence of atoms before anything else makes sense.


2. The vocabulary and the index — every possible next piece

Look at the numbered shelf in the figure: seat holds ", seat holds }, seat holds cat, and so on. When the parent note writes "token ", it means the token sitting in seat .

Why the topic needs it. Masking says things like "set the bias for token to ." That sentence is meaningless unless is a concrete address in a fixed list. Every token in the vocabulary is a candidate for the next step, whether it makes sense or not — that's exactly why we later need to forbid some.


3. The step counter and the prefix — "where are we so far?"

Read the subscript literally: = " at positions less than ."

Why the topic needs it. The whole rule "what may come next depends on what came before" is written . The prefix is the memory the grammar-parser reads to decide the legal next moves.


4. Logits — the raw scores

Look at the bar chart. Tall bars = large logits (the model likes that token); bars dipping below the line = negative logits (the model dislikes it). Crucially, a logit of is not "off" — it's just middling, and a logit of still leaves that token possible.

Why the topic needs it. Constrained generation edits logits (not probabilities). To edit them correctly we must understand they live on the whole number line, so "shifting one down to " is the natural move — see §7.


5. Turning scores into a die: softmax and

We can't roll a die on raw scores — some are negative, they don't add to 1. We need to turn the whole score list into probabilities: all non-negative, summing to 1. The tool that does this is softmax, and it is built from the exponential function .

Look at the curve. Follow it leftward (toward very negative ): it hugs zero but stays positive. This single behaviour — "push to and becomes exactly " — is the entire mechanism of constrained generation.


6. Probability and the sampling roll

Sampling = literally rolling the weighted die this distribution describes to pick the actual next token. The parent's phrase "sample token from the distribution" means exactly this.

Why the topic needs it. Guardrails act after this roll (inspect , maybe reject). Constrained generation acts on the numbers before the roll. Same die — two different moments to intervene.


7. The mask bias — the surgical tool

Now everything is in place to state the topic's central object.

Trace it through §5's curve. A forbidden token's score becomes , so its numerator , so its die-probability is . Legal tokens keep their scores and simply re-share the whole probability among themselves.

This is the same numerical trick that safety-tuning (5.1.5-RLHF-and-preference-learning) and function-calling schemas (6.2.8-Function-calling-and-tool-use) rely on to force well-typed tool arguments.


After a JSON {, the FSM sits in a state whose only outgoing arrows are " (start a key) or } (close). Every other token gets . That's the bridge: the FSM reads the prefix and paints each seat legal (bias ) or forbidden (bias ) — see 7.3.2-Adversarial-attacks-on-LLMs for why a content attacker can slip through a format-only FSM.


Prerequisite map

Token = text atom

Vocabulary and seat index i

Logit = raw score per seat

Step t and prefix x_before_t

Exponential e^x

Softmax makes a die

Probability P and sampling roll

Grammar FSM picks legal tokens

Mask bias 0 or minus infinity

Guardrails act AFTER the roll

Constrained generation acts BEFORE

Topic 6.2.10


Equipment checklist

Cover the right side and answer out loud.

What is a token, in plain words?
A small chunk of text (word or word-piece) the model reads/writes one at a time.
What does the index refer to?
A seat number identifying one specific token in the fixed vocabulary list.
What does mean?
All the tokens already chosen before step — the prefix / text so far.
Is a logit a probability?
No — it's a raw preference score on the whole real line (can be negative or zero).
Why do we exponentiate logits in softmax?
is always positive and lets bigger scores dominate, so we get a valid probability distribution.
What happens to as ?
It goes to exactly .
What does the bar in mean?
"Given" — probability of conditioned on the prefix.
What is and how is it applied?
A number added to logit before softmax: if legal, if forbidden.
Why add instead of multiplying by ?
Multiplying leaves a middling score () and can even boost negative logits; adding forces the probability to exactly .
What does a finite-state machine contribute?
It reads the prefix and decides which next tokens are legal, feeding the mask.
When do guardrails vs constrained generation act?
Guardrails after the sampling roll (reactive); constrained generation before it, on the logits (proactive).