Table of Contents

When your LLM application underperforms, the problem usually lives at one of three distinct layers: how the model selects tokens during generation (decoding), how you’ve structured the task into steps (workflow), or whether the model has enough reasoning capability for the problem at hand (reasoning). These three layers are routinely conflated in discussions, but they solve different problems and are optimized differently.

TL;DR

  • Decoding: token-level. Controls how the model samples from its probability distribution. Greedy is stable, sampling is creative, beam search finds global optima. For reasoning tasks, temperature=0 usually wins.
  • Workflow: task-level. How you decompose a problem into steps, tool calls, and agent coordination. Chain-of-thought, ReAct, multi-agent patterns live here.
  • Reasoning: model capability level. Whether the model can self-correct, explore multiple paths, and think longer. Inference-time scaling, ES-CoT, Coconut all apply here.
  • Optimize each layer separately. Don’t mix tools across layers.

Layer 1: Decoding Strategies

Decoding is how the model picks each next token from the vocabulary’s probability distribution. It’s the most overlooked optimization lever.

Greedy decoding: always pick the highest-probability token. Fast, deterministic, reproducible. Prone to local optima — one wrong choice can’t be corrected.

Sampling with temperature: sample from the distribution. Low temperature → approaches greedy; high temperature → more random and creative. Good for generation tasks, poor for reasoning.

Beam search: maintain multiple candidate sequences in parallel, select the globally highest-probability sequence. Better for reasoning-heavy tasks; compute cost scales linearly with beam width.

Top-k / Top-p (Nucleus Sampling): restrict sampling to the top-k tokens or the smallest set of tokens whose probability mass exceeds p. Balances quality and diversity.

A key 2025 finding: for RL-trained reasoning models, temperature=0 (greedy) significantly outperforms temperature>0. This is the opposite of best practice for creative generation tasks.

graph LR
    A[Model output<br>Logits] --> B[Softmax<br>Probability distribution]
    B --> C1[Greedy<br>Take max]
    B --> C2[Sampling<br>Sample by probability]
    B --> C3[Beam Search<br>Track multiple paths]
    C1 --> D[Stable — good for reasoning]
    C2 --> E[Diverse — good for creation]
    C3 --> F[Global optimum — expensive]

Layer 2: Workflow Design

Workflow is how you decompose a problem at the application layer — independent of which model you use. Good workflow design can significantly improve output quality even with a weaker base model.

Chain-of-Thought (CoT) Prompting: instead of asking for the answer directly, ask the model to write out its reasoning steps. This gives the model a chance to catch and correct errors as it writes. Simple and highly effective for math and logic problems.

ReAct (Reason + Act): interleave reasoning and tool calls. The model reasons about what to do → calls a tool (search, calculation, database query) → continues reasoning from the tool’s output. Best for tasks requiring external information.

Multi-step / Multi-agent Workflows: decompose complex tasks across specialized agents, each handling a subtask, with results aggregated. Good for tasks requiring parallel processing or distinct domain knowledge.

Self-consistency: generate multiple answers to the same question (with high temperature) and take the majority vote. Improves reasoning accuracy by leveraging sampling diversity; cost multiplies by the number of samples.

Layer 3: Reasoning Capability

Reasoning is the model’s intrinsic capability to self-explore, self-correct, and transcend the ceiling of what workflow design alone can achieve.

Inference-time Scaling: give the model more “thinking time” via longer chains of thought or explicit budget tokens. OpenAI o1/o3, Gemini Thinking, and Claude’s extended thinking all use this approach. Returns are approximately log-linear — doubling compute doesn’t double accuracy, but meaningful improvements continue well past the point where larger models stop helping.

ES-CoT (Early Stopping Chain-of-Thought): stop the reasoning chain when the answer has been stable for several consecutive steps. Research shows ~41% token reduction while maintaining accuracy comparable to full CoT. Drop-in, no retraining required.

Coconut (Chain of Continuous Thought): instead of expressing reasoning steps as natural language tokens, the model uses its last hidden state (a “continuous thought”) directly as the next input embedding. Reasoning happens in continuous latent space rather than the discrete vocabulary. Theoretically enables breadth-first search across reasoning paths rather than committing to one path as in standard CoT.

Comparison

TechniqueLayerExtra CostBest For
Greedy decodingDecodingNoneReasoning, reproducible output
Sampling + temperatureDecodingNoneCreative generation, diversity
Chain-of-ThoughtWorkflowLow (prompt)Math, logic problems
ReActWorkflowMedium (tool calls)Tasks needing external info
Self-consistencyWorkflow + DecodingHigh (3-10x inference)High-accuracy reasoning
Inference-time scalingReasoningHigh (longer output)Difficult reasoning, cost-insensitive
ES-CoTReasoningNegative (saves tokens)Cost/latency-constrained scenarios
CoconutReasoningRequires special trainingResearch stage, not yet deployed widely

Summary

The most common mistake when optimizing LLM applications is conflating layers. If your reasoning accuracy is low, you don’t necessarily need a bigger model — you might just have temperature set too high, or you’re not using CoT. If your costs are too high, you don’t need to downgrade models — ES-CoT might cut token usage by 40%.

Identify which layer the problem lives in first. Then pick the right tool.

References

Ask this article

Answers come from this article only. Click any prompt below or open the chat at the bottom right.

🇺🇸 English

Here's something that trips up almost everyone building with language models. Your app isn't performing well, the answers are wrong or the costs are through the roof, and your first instinct is "I need a bigger model." But most of the time, that's the wrong move — because the problem isn't where you think it is.

There are actually three completely separate layers where output quality gets decided. And the fastest way to fix anything is to figure out which layer your problem lives in before you touch anything.

Let me walk you through all three.

The first layer is decoding. This is the most overlooked lever of them all. When a model generates text, at every single step it's picking the next word — or token — from a probability distribution across its whole vocabulary. Decoding is just *how* it makes that pick.

The simplest approach is greedy decoding: always grab the highest-probability token. It's fast, it's deterministic, you get the same output every time. The downside? It's prone to getting stuck in local optima. Once it makes a bad choice, there's no going back.

Then there's sampling with temperature. Instead of always taking the top token, you roll the dice weighted by probability. Turn the temperature down low and it behaves almost like greedy. Crank it up high and you get wild, creative, random output. Great for writing tasks — terrible for reasoning, where you want consistency.

Beam search is more ambitious. It keeps several candidate sequences alive at once, running in parallel, and then picks the sequence with the best overall probability. It finds a more global optimum, which helps on reasoning-heavy work — but the compute cost scales linearly with how many beams you track.

And then top-k and top-p, or nucleus sampling — these are the guardrails. You restrict sampling to just the top handful of tokens, or the smallest set of tokens that together cross some probability threshold. It's the balance between quality and diversity.

Now here's the finding that flips conventional wisdom on its head. For reinforcement-learning-trained reasoning models, a 2025 result showed that temperature zero — pure greedy — significantly *outperforms* any temperature above zero. That's the exact opposite of what you'd do for creative generation. So if you copied "set temperature to 0.7" from some creative-writing tutorial and slapped it on a math problem, that alone could be sabotaging your accuracy.

Okay, layer two: workflow. This one lives entirely at the application level, and it's independent of which model you're using. In fact, good workflow design can make a *weaker* model punch way above its weight.

Chain-of-thought is the classic. Instead of asking for the answer straight up, you ask the model to write out its reasoning steps first. That simple shift gives the model room to catch its own mistakes as it goes. Dead simple, wildly effective for math and logic.

ReAct takes it further by interleaving reasoning with action. The model thinks about what it needs, calls a tool — a search, a calculation, a database query — then picks up reasoning from whatever came back. This is your go-to when the task needs external information the model doesn't carry in its head.

Then you've got multi-step and multi-agent workflows, where you break a big job across specialized agents, each owning a piece, and stitch the results together. Good for parallel work or genuinely distinct domains of knowledge.

And self-consistency — this one's clever. You ask the same question multiple times with high temperature, generate a bunch of different answers, and take the majority vote. You're leveraging sampling diversity to boost accuracy. The catch is cost: it multiplies by however many samples you run.

Layer three is reasoning — the model's *intrinsic* ability to explore, self-correct, and think longer. This is the ceiling that workflow design alone can't break through.

Inference-time scaling is the headline idea here: give the model more thinking time, through longer chains of thought or explicit budget tokens. This is what powers OpenAI's o1 and o3, Gemini Thinking, Claude's extended thinking. And the returns are roughly log-linear — doubling the compute doesn't double your accuracy, but here's the key part: those gains keep coming well past the point where just reaching for a bigger model stops helping.

ES-CoT — early-stopping chain-of-thought — is my favorite, because it's the rare technique with *negative* cost. The idea: once the answer has stayed stable for several steps in a row, you just stop reasoning. Research clocked around a 41% reduction in tokens while holding accuracy right up there with full chain-of-thought. No retraining, drop it right in.

And then there's Coconut — chain of continuous thought — which is the far frontier. Normally a model reasons in words, discrete tokens from its vocabulary. Coconut skips that. It takes the model's last hidden state — a kind of "continuous thought" — and feeds it straight back in as the next input. So the reasoning happens in continuous latent space instead of in language. In theory that lets the model explore multiple reasoning paths at once, breadth-first, rather than locking into one path the way standard chain-of-thought does. Still research-stage, not widely deployed — but it's a fascinating direction.

So let me tie the cost picture together, because it matters. Greedy decoding and temperature sampling? Basically free — they're just settings. Chain-of-thought costs a little, just a longer prompt. ReAct is medium, because of the tool calls. Self-consistency is expensive — three to ten times the inference. Inference-time scaling is expensive too, from all that extra output. ES-CoT actually *saves* you money. And Coconut needs special training, so it's not something you deploy today.

Here's what I want you to walk away with. The single most common mistake in LLM optimization is conflating these layers. If your reasoning accuracy is low, you probably don't need a bigger model — your temperature might just be too high, or you're not using chain-of-thought at all. If your costs are too high, you don't need to downgrade — ES-CoT might cut your tokens by forty percent without touching quality.

Three layers: decoding at the token level, workflow at the task level, reasoning at the model-capability level. Diagnose which one your problem lives in *first* — then, and only then, reach for the right tool.

🇹🇼 中文

我們都知道現在的語言模型很強,你給它一個輸入、它回你一個輸出,接著你告訴它「你錯了、錯在哪」,它通常有辦法把原本的錯誤修正過來。但今天要聊的是一個更難的問題——能不能在完全沒有人插手的情況下,模型自己吐出一個答案之後,自己察覺答錯了、然後自己改對?這就是所謂的 self-correction,自我修正。

這其實不是全新的想法。早在 2023 年 ChatGPT 剛出來的時候,就有人發現語言模型某種程度上會自我反省。我們今天聚焦在這一兩年比較新的進展。

先講大方向。要讓模型做到自我修正,大致有三條路。第一條是改 inference 的過程,不動模型參數,就在生成的當下去偵測跟修正錯誤。第二條是改 harness,也就是改工作流程、外部工具、互動方式。第三條最直接,改模型參數,也就是現在大家很熟悉的 reasoning、推理技術。今天主要深入第一條,在 inference 階段做自我修正。為什麼挑這條?因為它最容易被忽略,但你不用重新訓練模型就能直接套上去,門檻很低。

我們先把問題拆開。語言模型的生成流程大家不陌生:一排 token 丟進 Transformer,變成一排 representation,最後變成一個機率分布,從分布裡抽一個 token,再拿去當下一步的輸入,反覆這樣做。那自我修正就可以拆成兩件事:第一,能不能從這個過程裡的 representation 或機率分布,自動看出模型現在講錯了,這叫 error detection;第二,看出來之後,能不能自動改對,這叫 error correction。而且注意,這兩步都假設是全自動、不需要人介入。

先講結論——偵測,是可行的。2023 年有一篇論文,把模型答對跟答錯時的 representation 都收集起來,訓練一個二元分類器去判斷「這個 representation 會導向對還是錯」。結果這個分類器真的訓得起來,而且能夠 generalize,你在一批問題上訓練,換到另一批問題上還是有一定的預測力。這代表什麼?代表「答案是對是錯」這個訊號,其實藏在 representation 裡面,只是你要知道怎麼把它抽出來。

那修正呢?也可行。2024 年有一篇叫 True Facts 的論文更進一步,直接把錯的改成對的。做法是:把一大堆答錯時的 representation 取平均、答對時的 representation 取平均,兩個相減,得到一個代表「對跟錯之間差距」的向量。接著,把這個向量直接加回到模型原本會答錯的 representation 上,模型就有機會給出正確答案。

聽起來很漂亮,但這類方法有個明顯的缺點——它要額外收集資料。你得先問模型一大堆問題、記錄它答對答錯時 representation 各長什麼樣。那有沒有辦法,不收集額外資料,就把偵測跟修正一次做完?這就帶到今天的主角。

Contrastive decoding,對比式解碼。它等於把 error detection 跟 error correction 結合在一起,但完全不需要額外資料。它的核心概念是這樣:第一步,用正常的輸入問模型,得到一個你不確定對錯的輸出。第二步,對同一個問題動點手腳,刻意製造一個「模型幾乎一定會答錯」的狀態,拿到一份錯誤輸出。第三步,把正常輸出減掉錯誤輸出,得到兩者的差距,再把這個差距加回正常輸出,等於把答案往「遠離錯誤」的方向推。

實務上常見的寫法是引入一個小於 1 的參數 α。最終輸出等於「一加 α」乘上正常輸出,再減掉「α」乘上錯誤輸出。白話講,就是把正常的成分稍微放大,再減掉一點錯誤的成分。而且這個操作,每一個 token 的生成步驟都要做一次:修正出第一個 token 之後,把它當成下一步輸入,再製造正常跟錯誤兩種狀態、再相減,一路這樣下去。還有一個細節,文獻上相減的對象,通常不是中間的 hidden layer,而是模型最終輸出的 logit 或機率分布。

它的好處很清楚:完全不動模型參數,訓練好之後在 inference 直接套用。缺點也很清楚:要多花一次運算。因為你為了生出「錯誤的那份輸出」,得額外多跑一次 inference,等於拿算力去換比較正確的結果。

這招其實很老。最早提到 contrastive decoding 這個名詞的論文,可以追到 2022 年,那時候 ChatGPT 都還沒出現。它的設定是文字接龍:「歐巴馬生在檀香山……」後面要接他出生的年份。當時比較強的 GPT-2 large 覺得下一個字機率最大的是 Hawaii,但這是錯的,因為前面就已經講過檀香山了,正確答案應該是 1961。它的做法是,拿一個比較小的 GPT-2 small 產生的輸出當作「錯誤」的那一份,兩個機率分布一相減,1961 就浮上來了。這裡的關鍵洞見很重要——該被解碼出來的,不是好模型覺得機率最大的那個 token,而是好模型跟壞模型「差距最大」的那個 token。

好,那 contrastive decoding 成敗的關鍵在哪?就在於你怎麼可靠地製造出那個「錯誤」的參照。而且這裡有個隱藏陷阱:像用小模型當錯誤來源的時候,小模型有時候其實也會答對,那你怎麼保證只在它真的答錯時才相減?這是所有這類方法都得處理的問題。接下來講幾個主要流派。

第一個流派,用不同的 layer 來製造錯誤。代表作是 2023 年的 DoLa,全名是 Decoding by Contrasting Layers。這個方法用得很廣,甚至已經直接寫進 Hugging Face 的 Transformers,你用一個 flag 就能在 inference 時打開,套用成本超低。它背後靠的是一個叫 logit lens 的概念:你把最終才會用到的 LM head,接到中間的每一層 representation 上,也能 decode 出結果。文獻發現一件有趣的事,你要 Llama 2 把法文翻成中文,它中間層 decode 出來的常常是英文——代表它其實是先在心裡翻成英文,再翻成中文。DoLa 的想法就是:用 logit lens 從比較前面的 layer 去 decode,比較容易拿到錯誤答案;那我就拿最後一層的分布,減掉某個前面 layer 的分布,當作最終答案。至於要選哪一層來減,論文用了比較複雜的方法去挑。DoLa 的好處是,它不像原始版本那樣要再塞一個小模型進來、多吃記憶體跟計算,因為前面的 layer 本來就要跑,所以額外的 overhead 很小。

順帶講個有趣的背景:logit lens 其實沒有正式論文,多數人引用的是一篇部落格,但更早在 2020 年就已經有實驗室的論文記載了同樣的技術,只是當時覺得是個奇妙發現,丟上 arXiv 就沒去投稿。而 DoLa 的第一作者,正好是那個實驗室以前的專題生,畢業後去了 MIT,這篇是他在微軟實習時做的,還回頭引用了學長那篇。

同樣是靠不同 layer 製造錯誤的,還有 2025 年的 Layer Contrastive Decoding,用在影像模型上。它的例子很好懂:問「機車騎士衣服上的字是什麼顏色」,衣服是黑的,但字是白的,正確答案是白色。vision encoder 最後一層夠聰明,知道答案該是白色,但沒聰明到把白色排第一;而比較淺的 shallow layer 只抓得到表象,直覺回一堆奇怪顏色,還把黑色排前面。兩者相減之後,白色的機率就升到最高,答對了。

第二個流派,我叫它降智咒語,正式名字是 Instruction Contrastive Decoding,簡稱 ICD。這招更直白:在模型輸入的後面,多加一句貶低它的話,比如「你都給錯誤的答案」「你是一個很糟糕的模型」,模型就真的變笨、更可能答錯;然後把這份錯誤輸出跟正常輸出相減。

第三個流派,抽掉本來該給的資訊,代表是 Context-Aware Decoding,簡稱 CAD,最早 2023 年用在 RAG 上。RAG 的痛點是這樣:你把問題連同查到的相關文章一起丟給模型,但比較強的模型常常仗著自己參數裡有知識,根本不去讀那些文章。比如美國總統一直在換,它卻用舊知識回答。CAD 的想法很聰明:那我乾脆先不給文章,讓模型憑自己的知識作答,拿到一個可能過時、可能錯的答案,再拿它跟「有讀文章時的答案」相減,結果就更可能正確。

CAD 用在影像上,是最能說服人的例子。你給模型一張黑色的香蕉,問它什麼顏色。這裡要知道,影像的語言模型多半是從文字模型微調來的,帶著大量文字 prior,也就是「香蕉就該是黃色」這種成見,常常不細看圖就憑直覺回答。那 CAD 怎麼做?故意不給圖片,或者把圖片加上很強的雜訊,讓模型看不清,它就純憑成見回「黃色」,剛好是錯的,再拿正常答案去減掉它,就能把錯誤壓下來。另一個經典例子是海灘照片:模型先入為主覺得海灘就該有衝浪板,於是幻覺出根本不存在的衝浪板;你給它一張很模糊的圖,它一樣憑成見給衝浪板很高的機率,相減之後,衝浪板的機率就被壓得很低。

原始 CAD 只是在圖片上加一般的雜訊,後續研究就在鑽研「加什麼雜訊才有效」。有的把圖片切塊再打亂,效果比一般雜訊更好;也有 2025 年的論文,先分析模型作答時到底在看圖片的哪些位置,把最重要的物件抹掉之後再做對比解碼。而且這套思路還能搬到語音上,有個叫 Audio-Aware Decoding 的工作,是一位普渡大學的學生到實驗室 visit 時做出來的:正常給音訊拿一份輸出,再把音訊拿掉、或換成靜音,拿另一份,兩者相減。實驗顯示,這招在語音語言模型上一樣有效。

最後講成本。前面說了,contrastive decoding 的老問題就是拿算力換表現,每個 token 都要多跑一次 inference。有個叫 MTI 的方法,全名 Minimum Test-Time Intervention,就想省下這筆錢。它的假設很有意思:在 decode 的過程裡,可能只有少數幾個 token 是真正關鍵的轉折點,選錯就滿盤皆輸,而其他大多數 token,你選什麼其實都不太影響最後結果。就像人生大部分的決定不會改變結局,只有少數關鍵抉擇才會。所以 MTI 說,不必每個 token 都動它的機率,只在那些特別重要的 token 上介入,就能用更少的算力,保住 contrastive decoding 的大部分好處。

好,我們收一下。今天的主軸就是「語言模型能不能自我修正」,而在 inference 這一層,答案相當成熟。第一,偵測跟修正都能自動進行,訊號本來就藏在 representation 裡,早期靠分類器跟 True Facts,但要額外收資料。第二,contrastive decoding 打破了這個限制,不用收資料,核心就一句話——刻意製造一個會答錯的狀態,把答案往遠離錯誤的方向推;差別只在於「錯誤怎麼製造」:用小模型、用淺層 layer、用降智咒語、或是抽掉該有的資訊,而且這一套可以橫跨文字、影像跟語音。第三,這些方法多半不動參數、能直接套在 inference 上,代價是額外運算,而像 MTI 這樣的方法,正在想辦法把這筆成本再壓下來。

Tags

Related Articles

Harness Engineering (2): Five Engineering Answers from OpenAI's Million-Line Experiment

Three OpenAI engineers, five months, one million lines of AI-generated code, zero hand-written. The real value of this experiment isn't the numbers — it's the proof that Harness design can be engineered. Five concrete practices: making the app legible to agents, treating the repo as the source of truth, mechanizing architectural constraints, rewriting merge philosophy, and background entropy management.

Harness Engineering (3): Industry Consensus, Four Pillars, and a Three-Phase Rollout

Distilling Harness Engineering from concept and benchmark case into something you can start executing today: the four fixed failure modes of Agents, the 40% context sweet spot, the four-pillar framework the industry has converged on, and a three-phase roadmap from 'this afternoon' to 'fully automated in two weeks' — closing with six industry consensus points and three still-unsolved problems.