Overview

4 Improving reasoning with inference-time scaling

This chapter shows that a language model’s reasoning can be improved at inference time without retraining or changing its weights. The core idea is to spend more compute while generating an answer, whether by making the model write out intermediate steps, by sampling alternative completions, or by combining those completions with voting. The chapter sets up a pre-trained base model, then adapts the text generation pipeline so different decoding strategies can be swapped in cleanly for fair comparison.

A major focus is chain-of-thought prompting, where the model is nudged to explain its reasoning step by step before giving a final answer. This often helps on math and logic tasks because it gives the model more room to self-correct and matches patterns seen in detailed training solutions, but it also costs more tokens and can sometimes hurt on simpler problems through overthinking. The chapter also explains how sampling works under the hood: logits are scaled by temperature, converted to probabilities with softmax, and then sampled with multinomial selection, which introduces diversity into outputs.

To make that diversity more useful, the chapter adds top-p filtering, which removes low-probability tokens before sampling so the model is less likely to drift into nonsense while still allowing variation. Those sampling controls then support self-consistency, where multiple answers are generated, the final boxed result is extracted from each, and the most frequent answer wins. Experiments on MATH-500 show that these inference-time methods can significantly raise accuracy, especially when chain-of-thought is combined with self-consistency, but the gains come with substantial extra runtime and compute.

A mental model of the topics covered in this book. This chapter focuses on techniques that improve reasoning without additional training (stage 3). In particular, it extends the text-generation function and implements a voting-based method to improve answer accuracy. The next chapter then introduces an inference-time scaling approach where the model iteratively refines its own answers.
Comparing inference-time scaling (this chapter) and training-time scaling (after chapter 5). Both improve accuracy by using more compute, but inference-time scaling does this on the fly, without changing the model's weight parameters. The plots are inspired by OpenAI's article introducing their first reasoning model (https://openai.com/index/learning-to-reason-with-llms/).
Overview of three inference-time methods to improve reasoning covered in this book. The first modifies the prompt to encourage step-by-step reasoning, and the second samples multiple answers and selects the most frequent one. Both are discussed in this chapter. The third method, in which the model iteratively refines its own response, is introduced in the next chapter.
The first inference-time method, chain-of-thought prompting, modifies the prompt to encourage the model to explain its reasoning step by step before producing a final answer.
The second inference-time method, self-consistency sampling, generates multiple answers and selects the most frequent one. This method relies on temperature scaling, covered in this section, which influences how the model samples its next token.
How an LLM generates the next token. As in the other process diagrams in this book, the flow runs from bottom to top. The model converts the input into token IDs, computes scores for all possible next tokens, and selects the one with the highest score as the next output.
Example of next-token logits for a 100-token slice of a language model's much larger vocabulary. Each bar represents one possible token's score within this slice, with "Berlin" having the highest logit value and being selected as the next token.
In this section, we implement the core part of temperature scaling (step 3.2), which adjusts the next-token scores. This allows us to control how confidently the model selects its next token in later steps.
The effect of temperature scaling on logits. Lower temperatures make the distribution sharper, while higher temperatures flatten it. (Please note that this visualization is shown as a line plot for readability, though a bar plot would more accurately represent the discrete vocabulary scores.)
Overview of the sampling process for generating tokens. In this section, we focus on steps 3.3 and 3.4, where the next-token scores are converted into a probability distribution, and the next token is sampled based on that distribution.
Token probabilities obtained by applying the softmax function to the rescaled logits. The token of the highest probability (corresponding to " Berlin", but with the label omitted for code simplicity) is selected as the next output.
Overview of the top-p filtering process. The filter keeps only the highest-probability tokens by sorting them, applying a cumulative cutoff, selecting the top-p subset, and renormalizing the result.
Example of token probabilities before top-p filtering. The distribution includes many low-probability tokens, which will later be truncated by applying a cumulative probability threshold.
Visualization of sorted token probabilities and their cumulative sum. This step prepares for top-p filtering by showing how probabilities accumulate when ordered from highest to lowest, which helps determine where to set the cutoff threshold.
Top-p (nucleus) filtering. Tokens are sorted by probability, and the smallest subset whose cumulative probability exceeds the threshold (p = 0.8) is kept for sampling.
Integrating top-p filtering with temperature scaling. After rescaling the next-token scores, top-p filtering is applied between steps 3.3 and 3.4 to limit sampling to the most probable tokens.
The self-consistency sampling method generates multiple responses from the LLM and selects the most frequent answer, which improves answer accuracy through majority voting across these sampled responses.
The three main steps for implementing self-consistency sampling. First, we generate multiple answers for the same prompt using a temperature greater than zero and top-p filtering to generate different answers. Second, we extract the final boxed answer from each generated solution. Third, we select the most frequently extracted answer as the final prediction.
Summary of this chapter's focus on inference-time techniques. Here, the text generation function was extended with a voting-based method to improve answer accuracy. The next chapter introduces self-refinement, in which the model iteratively improves its responses.

Summary

  • Reasoning abilities and answer accuracy can be improved without retraining the model by increasing compute at inference time (inference-time scaling).
  • This chapter focuses on two such techniques: chain-of-thought prompting and self-consistency; a third method, self-refinement, which was briefly described, will be covered in for the next chapter.
  • A flexible text generation wrapper (generate_text_stream_concat_flex) that uses different sampling strategies that can be plugged in without changing the surrounding code.
  • Next tokens are produced from logits via softmax
  • Temperature scaling changes logits to control the diversity of the generated text.
  • Top-p (nucleus) sampling filters out low-probability tokens to reduce the chance of generating nonsensical answers
  • Chain-of-thought prompting (like "Explain step by step." or similar) often yields more accurate answers by encouraging the model to write out intermediate reasoning, though it increases the number of generated tokens and thus increases the runtime cost.
  • Self-consistency sampling generates multiple answers, extracts the final boxed result from each, and selects the most frequent answer via majority vote to improve the answer accuracy.
  • Experiments on the MATH-500 dataset show that combining chain-of-thought prompting with self-consistency can substantially boost accuracy compared to the baseline without sampling, at the cost of much longer runtimes.
  • The central trade-off of inference-time scaling: higher accuracy in exchange for more compute.

FAQ

What is inference-time scaling?Inference-time scaling means spending extra compute during answer generation, after the model is already trained, to improve response quality. It does not change the model’s weights; instead, it makes the model do more work at test time, such as generating more tokens or sampling multiple answers.
What are the two main ways to improve reasoning performance?The chapter explains two main strategies: increasing training compute and increasing inference compute. Training-time scaling improves the model through more training, while inference-time scaling improves answers by using more computation when the model is generating text.
How does chain-of-thought prompting improve answer accuracy?Chain-of-thought prompting asks the model to explain its reasoning step by step before giving the final answer. This often helps because the model gets more opportunities to correct itself and because many training examples already include step-by-step solutions, especially in math and logic tasks.
Why can chain-of-thought prompting sometimes hurt performance?Although it often helps, chain-of-thought prompting is not always beneficial. On simple problems, the model may generate unnecessary or incorrect intermediate steps and end up “overthinking,” which can reduce accuracy.
What changes were made to the text generation function in this chapter?The generation wrapper was made more flexible so it can accept different text generation functions and extra keyword arguments. This allows the surrounding prompt-handling and decoding code to stay the same while swapping in different decoding strategies such as greedy decoding, temperature sampling, or top-p sampling.
What does temperature scaling do in LLM sampling?Temperature scaling rescales the model’s logits before sampling. Lower temperatures make the distribution sharper and more deterministic, while higher temperatures flatten the distribution and increase randomness, making alternative tokens more likely to be sampled.
Why is softmax used after temperature scaling?Softmax converts the rescaled logits into probabilities between 0 and 1 that sum to 1. This makes the scores interpretable and allows the next token to be sampled probabilistically with methods like multinomial sampling.
What is top-p sampling?Top-p sampling, also called nucleus sampling, keeps only the smallest set of tokens whose cumulative probability reaches a chosen threshold p. The low-probability tokens outside that set are removed, and the remaining probabilities are renormalized before sampling.
How does self-consistency improve reasoning?Self-consistency generates multiple candidate answers for the same prompt, usually using temperature and top-p sampling to encourage diversity. It then extracts the final answers and chooses the most frequent one, which often improves reliability through majority voting.
What is the trade-off of inference-time scaling methods in this chapter?The main trade-off is better accuracy in exchange for more compute, more generated tokens, and higher latency. Methods like chain-of-thought prompting and self-consistency can substantially improve results, but they also make inference more expensive and slower.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a Reasoning Model (From Scratch) ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a Reasoning Model (From Scratch) ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a Reasoning Model (From Scratch) ebook for free