Decoding, Prompting, and Agents
In Posts 38 and 39 we built the text pipeline and trained a transformer from scratch. We know how tokens flow through the model and how the weights get updated. What we have not done is ask the model a question and read the answer. At inference time we feed the model a prompt and choose each next token. That choice is where decoding, prompting, and tools meet. In this post we load a small instruct model, SmolLM-135M-Instruct, and put it to work on GSM8K, the grade-school math benchmark. We will steer its output with decoding controls, measure it two ways, build a chain-of-thought prompt (ask for intermediate reasoning steps), add a calculator tool, and watch it fail on purpose. The headline is honest and a little humbling: on our six-problem evaluation slice, greedy chain-of-thought scores an exact match (the final number must equal the gold number) of 0.000, and self-consistency (sample several chains and take the majority final number) scores 0.000 too.
The through-line for the whole post is the gap between what a model says and what is true. Every technique we touch, from temperature to grammar masks to watermarking, is a way of closing that gap or a way of measuring how wide it still is.
We build the whole thing on GSM8K, about 8,500 grade-school word problems, each with a short question and a chain-of-thought answer that ends in a #### marker followed by the final number. That structure is exactly what we need: it supports few-shot prompting, chain-of-thought, self-consistency, exact-match scoring, and a calculator loop. The train split loads with 7,473 rows and the test split with 1,319, both with just question and answer columns.
What the data told us
Before touching the model, we looked at the data the way we would look at any dataset. The first thing we checked was the answer distribution. The final numeric answer is extractable for every row, and the base rate of answers below 100 is 0.665. The 0.665 base rate means about two-thirds of gold answers sit below 100; that sets the scale of the answer space, but exact match is still strict because it requires the exact final number.

Fig. 1: Most gold answers fall below 100 and the long tail runs past 200; the plot clips at 200, but scoring uses raw values.
The histogram shows the long right tail we expected. Most answers cluster low, but a handful run far out, which is why the plot clips at 200 while the evaluation itself uses the raw gold values. We also checked token lengths, and the picture there is just as useful.

Fig. 2: Questions center near 58 tokens and answers near 120; the 95th percentiles of 100 and 220 justify the 96-token generation cap.
Questions average 58.3 tokens with a 95th percentile of 100, and answers average 119.7 with a 95th percentile of 220. That told us a 96-token generation cap would keep CPU runs bounded without cutting off most answers. Two more findings shaped the evaluation. There were no duplicate training questions and no train/test question overlap in this load, but the pipeline drops both anyway so the evaluation stays honest. And the tokenizer shows an out-of-vocabulary rate of 0.0, so byte-level fallback is not a blocker here.
The full test set is too large to generate against on a CPU, so we work with six test problems drawn with seed 42. That is a teaching slice, not a benchmark, and we will say so again when the numbers come in.
Decoding
The model does not produce text. It produces a probability over the next token, and decoding is the rule we use to pick one. Greedy decoding picks the top token every step; temperature scales the logits before sampling; top-k and nucleus both restrict the candidate set, one by count and one by cumulative probability. Repetition and frequency penalties down-weight tokens already in the context, with the frequency version growing the penalty by how often a token has appeared. Contrastive decoding subtracts the logits of a weaker amateur model, and typical decoding keeps tokens close to the entropy of the distribution rather than the most probable ones.
We can see the difference immediately on a simple arithmetic prompt. Greedy decoding collapses into a loop, printing "Answer:" over and over. Temperature 0.3 gives a coherent but wrong answer, claiming the distance is 2 hours. Temperature 1.2 wanders into nonsense about admission charges and tanks. Top-k 20 actually gets the arithmetic right, printing "60 mph * 2 hours = 120 miles" before drifting into a rounding instruction. Nucleus at p=0.9 restates the setup twice and never finishes. Same model, same prompt, four different behaviors, and only one of them is useful.
The repetition controls are worth seeing as code, because the frequency penalty is not a built-in flag in the same way. We implement it as a logits processor that subtracts a penalty scaled by each token's count in the context.
class FrequencyPenaltyLogitsProcessor(LogitsProcessor):
def __init__(self, penalty):
self.penalty = penalty
def __call__(self, input_ids, scores):
for b in range(input_ids.shape[0]):
counts = torch.bincount(input_ids[b], minlength=scores.shape[-1]).float().to(scores.device)
scores[b] = scores[b] - self.penalty * counts # penalize tokens by how often they appeared
return scores