VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
D7: Prompt & Context Design · Advanced Prompting — Reasoning, Sampling Many Answers and Constrained Output
Phase 5/Domain 7/Lesson 2

Advanced Prompting — Reasoning, Sampling Many Answers and Constrained Output

15 min read·Intermediate·Free lesson
Advanced Prompting — Reasoning, Sampling Many Answers and Constrained Output

You already know: prompt anatomy, few-shot examples and structured outputs (Prompt fundamentals); RL with verifiable rewards and pass@k (D6 RL for LLMs); every generated token costs a forward pass (D5); the binomial distribution (D2 Probability).

Why this matters for Atlas. The fundamentals make a prompt clear. The techniques here give a model more chances to be right: more computation per answer (reasoning), more independent attempts (self-consistency), smaller steps with checks between them (decomposition), and outputs that physically can't be malformed (constrained decoding). Each one costs tokens, latency or engineering effort, so you need to know when it pays off. The simulations below give you numbers to reason with.


1. Reasoning before answering: buying computation with tokens

The question. Why does "think step by step" make models better at multi-step problems?

Start from nothing. A transformer does a fixed amount of computation per token: one forward pass. If it must output the answer to a hard problem as its very first token, all of the work has to happen inside one forward pass. If it first writes out intermediate steps, every step is another forward pass, and each one can build on the written results of the previous ones. Writing the reasoning down turns tokens into computation. That's why chain-of-thought prompting (Wei et al., 2022) improved multi-step maths and logic so dramatically.

Today the idea is built into the models: reasoning models are trained with RL (D6) to think before answering, and APIs expose how much they think as an effort setting. Thinking isn't free, though, so here is what it buys and what it costs:

python
# Every generated token costs a full forward pass: about 2 x (active parameters) FLOPs.
# "Thinking" before answering therefore buys the model more computation for the same question.
active_params = 100e9            # illustrative: a model with 100B active parameters
price_per_m_output = 20.00       # $ per million output tokens (Claude Opus 5.5 list price, Nov 2026)
tokens_per_second = 60           # illustrative decoding speed for one request

print(f"{'strategy':<34}{'tokens':>8}{'TFLOPs':>9}{'cost $':>9}{'seconds':>9}")
for name, thinking, answer in [("answer directly", 0, 40),
                               ("short reasoning", 400, 40),
                               ("deep reasoning (hard problem)", 8000, 60),
                               ("5 samples x short reasoning", 5 * 400, 5 * 40)]:
    tokens = thinking + answer
    print(f"{name:<34}{tokens:>8,}{2 * active_params * tokens / 1e12:>9,.0f}"
          f"{tokens * price_per_m_output / 1e6:>9.4f}{tokens / tokens_per_second:>9.1f}")
  • Deep reasoning on a hard problem uses about 200× the compute of a direct answer, at 200× the cost and latency. That's worth it for a proof or a debugging session, wasteful for "what's the capital of France".
  • Five samples cost 5× in tokens but not 5× in wall-clock time: they can run in parallel, so latency stays close to a single sample (about 7 s here, not 37 s).

Engineering reality.

  • With current Claude models, thinking is adaptive: the model decides how much to think, and you steer it with effort (low to max) rather than with "think step by step" phrases. Use low effort for classification and chat, and high effort for maths, code and planning. Measure where higher effort actually helps (Prompt evaluation).
  • Don't ask a reasoning model to reveal or follow a specific hidden chain of thought. Ask for what you need in the answer: "show the key steps", "state your assumptions". The visible explanation is what you can check and show to users.

2. Self-consistency: sample several answers and vote

The question. If one sampled answer is right 60% of the time, can several samples do better?

Start from nothing. Self-consistency (Wang et al., 2022): sample k independent reasoning paths and take the most common final answer. It works when correct reasoning tends to converge on the same answer while mistakes scatter in different directions. Whether that holds depends on how the wrong answers are distributed:

python
import numpy as np

rng = np.random.default_rng(0)

def vote_accuracy(answer_probs, k, trials=20_000):
    """answer_probs[0] is the correct answer's probability per sample; others are wrong answers.
    Sample k independent answers, take the most common (ties broken at random)."""
    samples = rng.choice(len(answer_probs), size=(trials, k), p=answer_probs)
    correct = 0
    for row in samples:
        counts = np.bincount(row, minlength=len(answer_probs))
        winners = np.flatnonzero(counts == counts.max())
        correct += rng.choice(winners) == 0
    return correct / trials

cases = {
    "right 60%, errors scattered": [0.60] + [0.40 / 8] * 8,
    "right 35%, errors scattered": [0.35] + [0.65 / 13] * 13,
    "right 35%, one popular wrong answer at 45%": [0.35, 0.45, 0.20],
}
print(f"{'model behaviour':<45}" + "".join(f"{'k=' + str(k):>8}" for k in [1, 3, 5, 9, 15]))
for name, probs in cases.items():
    print(f"{name:<45}" + "".join(f"{vote_accuracy(probs, k):>8.0%}" for k in [1, 3, 5, 9, 15]))
Model behaviour k = 1 k = 5 k = 15
Right 60%, errors scattered 60% 88% 100%
Right 35%, errors scattered 35% 57% 89%
Right 35%, one popular wrong answer at 45% 35% 34% 31%
  1. Voting amplifies the plurality, whatever it is. If the correct answer is the most likely single answer, more votes push accuracy toward 100%, even from only 35% per sample.
  2. Voting can make things worse. When one wrong answer is more popular than the right one, which is common with a misleading question or a typical misconception, more votes lock in the mistake.
  3. It needs a comparable final answer. Voting works for numbers, labels and multiple choice. For free text, use a judge or a clustering step, or the "universal self-consistency" variant that asks a model to pick the most consistent response.

Engineering reality. Self-consistency is test-time compute in its simplest form. It's closely related to pass@k (D6): if any of k samples can be verified (unit tests, a checker), pick a verified one instead of voting. That's best-of-n with a verifier, and it beats voting whenever a verifier exists.

3. Decomposition and prompt chaining

The question. Should a complex task be one big prompt or several small ones?

Start from nothing. Prompt chaining splits a task into steps, each a separate model call with its own focused prompt: extract → analyse → draft → review. Small prompts are easier to write, test and debug, and each step can use a different model or effort level. The catch: errors compound. A pipeline is only as reliable as the product of its steps, unless you check between steps:

python
import random

random.seed(0)

def run_chain(steps, p_step, check_recall=0.0, retries=0):
    """One pass through a pipeline of `steps` model calls, each correct with probability p_step.
    A checker catches a wrong step with probability `check_recall`; a caught step is retried."""
    calls = 0
    for _ in range(steps):
        for attempt in range(retries + 1):
            calls += 1
            ok = random.random() < p_step
            caught = (not ok) and random.random() < check_recall
            if ok or not caught or attempt == retries:
                break                     # accept this attempt (a wrong one slips through if not caught)
        if not ok:
            return False, calls
    return True, calls

print(f"{'pipeline':<53}{'task success':>13}{'calls':>7}")
for steps in [1, 3, 5, 10]:
    for name, recall, retries in [("no checks", 0.0, 0), ("checker (80% recall) + 2 retries", 0.8, 2)]:
        runs = [run_chain(steps, 0.9, recall, retries) for _ in range(50_000)]
        ok = sum(r for r, _ in runs) / len(runs)
        calls = sum(c for _, c in runs) / len(runs)
        print(f"{steps:>2} steps at 90% each, {name:<32}{ok:>8.0%}{calls:>9.1f}")
Steps (90% each) No checks Checker (80% recall) + 2 retries
1 90% 98%
5 59% 90%
10 35% 80%
  • Without checks, 10 steps at 90% each succeed only 0.9¹⁰ ≈ 35% of the time.
  • A checker that catches 80% of bad steps, plus up to 2 retries, cuts each step's failure rate from 10% to about 2.2%. That roughly doubles end-to-end success at 10 steps, for about 50% more calls. (The "no checks" pipeline makes fewer calls only because it gives up at the first failure.)

What makes a good checker. Deterministic checks first: schema validation, unit tests, "does the number in the summary appear in the source?". Then a model-based check with a different prompt (and ideally a different model) from the step it checks; a model reviewing its own answer with the same framing tends to agree with itself.

Engineering reality. Chains are the bridge to agents (D9). In a chain, your code decides the sequence of steps. In an agent, the model decides. Start with a fixed chain whenever the steps are known in advance: it's cheaper, faster and far easier to test.

a five-box pipeline with probability labels multiplying to 59%, and the same pipeline with a small "check" diamond after each box looping back on failure, labelled 90%.
Diagram— a five-box pipeline with probability labels multiplying to 59%, and the same pipeline with a small "check" diamond after each box looping back on failure, labelled 90%.

4. Constrained decoding: making invalid output impossible

The question. Prompting and retries make valid output likely. Can it be guaranteed?

Start from nothing. At every step the model produces scores for every token, and sampling picks one. Constrained decoding masks out — sets to −∞ — every token that would break a formal grammar (a JSON schema, an enum, a regular expression) before choosing. The model can only ever produce valid output. Here a noisy toy "model" must output one of four team labels, one character at a time:

python
import numpy as np

rng = np.random.default_rng(0)
VOCAB = list("abcdefghijklmnopqrstuvwxyz_") + ["<end>"]
ALLOWED = ["billing", "bug", "content", "privacy"]

def toy_model_logits(prefix):
    """Stand-in for an LLM's next-token scores: mildly prefers the letters of 'billing'
    but adds noise, so a single wrong character can slip in."""
    logits = rng.normal(0, 1.0, len(VOCAB))
    target = "billing<"
    nxt = target[len(prefix)] if len(prefix) < len(target) else "<"
    logits[VOCAB.index("<end>") if nxt == "<" else VOCAB.index(nxt)] += 3.5
    return logits

def allowed_next(prefix):
    """Characters that keep `prefix` on a path to some allowed string (a trie walk)."""
    nxt = {s[len(prefix)] for s in ALLOWED if s.startswith(prefix) and len(s) > len(prefix)}
    if prefix in ALLOWED:
        nxt.add("<end>")
    return nxt

def decode(constrained, max_len=10):
    out = ""
    for _ in range(max_len):
        logits = toy_model_logits(out)
        if constrained:                                   # mask every token that would break the grammar
            mask = np.full(len(VOCAB), -np.inf)
            for tok in allowed_next(out):
                mask[VOCAB.index(tok)] = 0.0
            logits = logits + mask
        tok = VOCAB[int(np.argmax(logits))]
        if tok == "<end>":
            break
        out += tok
    return out

for constrained in [False, True]:
    outs = [decode(constrained) for _ in range(2000)]
    valid = np.mean([o in ALLOWED for o in outs])
    common = max(set(outs), key=outs.count)
    invalid = [o for o in outs if o not in ALLOWED][:3]
    print(f"constrained={constrained!s:<5}: valid {valid:6.1%} | most common {common!r} | invalid examples {invalid}")

Unconstrained, the toy model gets the full label right only 47% of the time: small per-character errors compound over the 8 characters into near-misses like billipg. With the mask, 100% of outputs are valid labels, and the model's preference still decides which label. This is what structured outputs do inside the API, with a grammar compiled from your JSON schema.

Engineering reality.

  • Constraints guarantee form, not truth. A constrained model can still pick the wrong team; it just can't pick a non-existent one. Evaluation still matters.
  • Heavy constraints can hurt quality if they force the model to commit before it has reasoned. With reasoning models the thinking happens first and the constrained answer comes after, which avoids most of this.
  • The same masking trick powers tool calling with strict schemas, and the "regex/grammar" options of open-source inference servers (D12).

5. Putting it together on a real API

python
from collections import Counter
from concurrent.futures import ThreadPoolExecutor
import anthropic
from pydantic import BaseModel

class Solution(BaseModel):
    final_answer: str
    key_steps: list[str]          # the checkable explanation we show the student

client = anthropic.Anthropic()

def solve(problem: str, effort: str) -> Solution:
    response = client.messages.parse(
        model="claude-opus-5-5",
        max_tokens=16000,
        thinking={"type": "adaptive"},                 # the model decides how much to think...
        output_config={"effort": effort},              # ...within the effort level we choose
        system="You are Atlas's maths tutor. Solve the problem; list the key steps a student needs.",
        messages=[{"role": "user", "content": f"<problem>{problem}</problem>"}],
        output_format=Solution,                        # constrained decoding to this schema
    )
    return response.parsed_output

# Routine question: low effort. Hard question: high effort plus a small parallel vote.
quick = solve("What is 15% of 240?", effort="low")
HARD_PROBLEM = "A GPU serves 3 models whose KV caches grow at 0.5, 1.2 and 2 GB per 1k tokens. ..."
with ThreadPoolExecutor(5) as pool:
    answers = [s.final_answer for s in pool.map(lambda _: solve(HARD_PROBLEM, "high"), range(5))]
winner, votes = Counter(answers).most_common(1)[0]

Other techniques worth knowing.

Technique Idea Use when
Role / persona "You are a senior reviewer…" Setting tone and expertise level; small effect on raw accuracy
Self-critique / reflection Ask the model to critique and revise its draft Writing quality; weaker for factual errors the model can't see
Least-to-most Solve easier sub-problems first, feed their answers forward Problems with natural sub-structure
Generated knowledge First ask the model to list relevant facts, then answer Open questions where recall helps
Meta-prompting / prompt optimization Have a model (or a search like DSPy) rewrite the prompt against a metric You have an evaluation set (Prompt evaluation)

Quick check

When does asking the model to reason step by step help most?

  1. Never with modern models
  2. For every task, always
  3. Only with small models
  4. For multi-step problems (maths, logic, planning) where intermediate steps matter; for simple lookups it adds cost and latency without gains
Show the answer

D. For multi-step problems (maths, logic, planning) where intermediate steps matter; for simple lookups it adds cost and latency without gains — Extra reasoning tokens give the model room to compute intermediate results; tasks without intermediate steps don't benefit.


Check yourself

  1. Why does writing out reasoning give a transformer more ability, not just more words?
  2. Self-consistency with k = 15 improved a 35%-accurate model to 89% in one case and made it worse in another. What was the difference?
  3. A 6-step pipeline has steps that are each 95% reliable. What's the end-to-end success rate without checks? What single change would you make first?
  4. Structured outputs guarantee valid JSON. Name one failure they don't prevent.
  5. When is best-of-n with a verifier better than majority voting?
Answers
  1. Each generated token is a full forward pass. Intermediate tokens let the model spend more total computation on the problem and condition on its own written intermediate results, like scratch paper.
  2. Where the wrong answers were scattered, the correct answer was still the single most common one, so voting amplified it. Where one wrong answer was more common (45%) than the right one (35%), voting amplified the wrong answer.
  3. 0.95⁶ ≈ 74%. Add checks between steps, starting with cheap deterministic ones (schema, tests, cross-checking against the source) with a retry on failure. Also consider merging steps that don't need to be separate.
  4. Wrong content in a valid shape: the wrong label, a hallucinated value, a summary that misstates the source.
  5. Whenever a reliable verifier exists (tests, a proof checker, exact recomputation). Then a single verified answer beats any vote, and it still works when the correct answer is a minority among samples.

Exercise

Self-consistency is expensive. Make it cheaper without losing accuracy: write vote(max_samples, rule) that samples answers one at a time and stops early.

  • exact: stop as soon as the remaining samples can no longer change the winner. This can never change the result.
  • lead of 3: a heuristic that stops once the leader is 3 votes ahead.

Compare accuracy and average samples used against always taking all 9.

Solution
python
import random
from collections import Counter

random.seed(0)

def sample_answer(p_correct=0.6, n_wrong=8):
    """Stand-in for one sampled reasoning chain's final answer."""
    return "42" if random.random() < p_correct else f"wrong-{random.randrange(n_wrong)}"

def vote(max_samples, rule):
    counts = Counter()
    for n in range(1, max_samples + 1):
        counts[sample_answer()] += 1
        top = counts.most_common(2)
        lead = top[0][1] - (top[1][1] if len(top) > 1 else 0)
        if rule == "exact" and lead > max_samples - n:   # remaining samples can't change the winner
            break
        if rule == "lead of 3" and lead >= 3:            # heuristic: stop once clearly ahead
            break
    return counts.most_common(1)[0][0], n

for rule in ["none", "exact", "lead of 3"]:
    runs = [vote(9, rule) for _ in range(20_000)]
    acc = sum(a == "42" for a, _ in runs) / len(runs)
    avg = sum(n for _, n in runs) / len(runs)
    print(f"stopping rule {rule:<10}: accuracy {acc:.1%} | average samples {avg:.2f} of 9")

The exact rule saves about 19% of samples at the same accuracy (the 0.1-point difference is sampling noise; the rule provably can't change the winner). The heuristic saves about 31% with no measurable loss in this setting, because errors are scattered and a 3-vote lead is rarely overturned. With a popular wrong answer, a heuristic stop can lock in mistakes earlier; check it against your evaluation set before shipping. Combined with parallel sampling (send 3 at once, then more only if needed), this is how production systems keep voting affordable.


This unlocks:

  • Context engineering — reasoning and votes only help if the right information is in the context.
  • Prompt evaluation — measuring whether effort, voting or a chain actually improves results.
  • D9 Reasoning & test-time compute — scaling these ideas: search, verifiers, process reward models.
  • D9 Agent architectures — from fixed chains to model-directed loops.

Sources: Wei et al., Chain-of-Thought Prompting Elicits Reasoning in Large Language Models (2022); Kojima et al., Large Language Models are Zero-Shot Reasoners (2022); Wang et al., Self-Consistency Improves Chain of Thought Reasoning (2022); Chen et al., Universal Self-Consistency (2023); Aggarwal et al., Let's Sample Step by Step: Adaptive-Consistency (2023); Zhou et al., Least-to-Most Prompting (2022); Madaan et al., Self-Refine (2023); Khattab et al., DSPy (2023); Willard & Louf, Efficient Guided Generation for LLMs (Outlines, 2023); Snell et al., Scaling LLM Test-Time Compute Optimally (2024); Anthropic, Extended thinking and Structured outputs documentation.

Build Atlas · Part 6 of 9: Talk to it wellUse this lesson in the project: Prompts as code with untrusted text escaped, one interface for every model, and an eval harness that names the cases a change breaks.