Two-Stage CoT Distillation in Practice: Positive Learning and Negative Error Correction
Chain-of-Thought (CoT) prompting improves complex reasoning, but small models trained on unfiltered reasoning traces often perform inconsistently and incur high training costs. Two-stage CoT distillation addresses this: stage one teaches the student model to reason step by step through supervised fine-tuning, while stage two uses preference optimization to help it distinguish correct reasoning chains from incorrect ones. This article covers the workflow, data quality requirements, a code skeleton, and how the method applies to agent orchestration, code review, and multi-step retrieval QA.
Chain of Thought (CoT) lets a model break a complex problem into intermediate steps before producing an answer. This format is especially useful for mathematical derivations, multi-step logic, and multi-file code analysis. However, when small models directly learn from large volumes of unfiltered reasoning traces, results are often unstable and training costs are high. Two-stage CoT distillation is a common approach: in the first stage, the student model learns to reason step by step; in the second stage, it learns to recognize and avoid incorrect reasoning paths. This article explains the steps of this method, a code skeleton, data control requirements, and how it can be applied in agent orchestration, code review, and multi-step retrieval QA.
1. Clarifying Two Concepts First
Chain of Thought is a prompting or training format that requires the model to output intermediate reasoning steps. Its value lies in exposing errors within the intermediate steps, making them easier to inspect. But intermediate steps themselves can also be wrong, so you should not assume that “longer output means more reliable.”
Model distillation means using a more capable teacher model to generate data that trains a smaller student model. The benefit of distillation depends on the quality of the teacher’s output. If the teacher’s reasoning chain contains errors, the student will learn the same errors.
Two-stage CoT distillation splits training into two steps. The first step is supervised learning, where the goal is for the student to imitate the complete reasoning format. The second step is preference learning, where the goal is for the student to prefer the correct chain over the incorrect chain for the same problem.
2. Stage One: Positive Reasoning Data and Supervised Fine-Tuning
The input to stage one is a set of problems. The teacher model generates a reasoning process and a final answer for each problem, after which three processing steps are required.
First, keep samples whose answers are verifiable. For math problems with standard answers, coding problems that can pass unit tests, and QA items that can be checked against source text, use a program to verify whether the answer is correct. Unverifiable samples can be kept, but they should be labeled separately and distinguished from verifiable samples during evaluation.
Second, unify the format. Each sample contains an instruction, an input, and an output. Within the output, there must be a clear separator between the reasoning process and the final answer to facilitate downstream parsing.
Third, perform limited data augmentation. Rewording the same problem, adjusting numbers, or changing the context can reduce the risk of the model merely memorizing a specific passage. The augmentation ratio should not be too high. If augmented samples are highly similar to the original problem, they contribute little to generalization and may amplify certain biases.
After cleaning, use these samples for supervised fine-tuning (SFT). The learning rate, number of training epochs, and batch size need to be adjusted based on data volume. Reserve a validation set that is excluded from training to observe how the model performs on unseen problems.
3. Stage Two: Negative Sample Construction and Preference Optimization
The goal of stage two is to enable the model to distinguish correct reasoning chains from incorrect ones. The key question is how to obtain reliable negative samples.
A common practice is to raise the sampling temperature so the teacher model generates more random reasoning chains, which are then treated as negative samples. This approach has clear flaws. Text generated at high temperature may simply be incoherent in expression without necessarily containing logical errors. The model may learn that “messy text is bad” rather than “this reasoning step is wrong.”
More reliable approaches include the following three:
- Rule-based perturbation: Make targeted modifications to a correct reasoning chain, such as deleting a necessary step, replacing an intermediate value, or reversing a condition. After modification, recompute the conclusion to confirm that the negative sample’s final answer genuinely differs from the standard answer.
- Error pattern library: Collect failure cases from production or historical data, such as missing boundary conditions, confusing units, or mistaking correlation for causation. Convert these patterns into reusable perturbation templates.
- Human verification: Spot-check a portion of negative samples to confirm whether the flagged errors actually exist. The sampling ratio can start at 5% and be adjusted based on the observed error rate.
Once positive and negative sample pairs are obtained, you can train with Direct Preference Optimization (DPO) or other preference learning methods, aiming to increase the model’s relative preference for correct chains. Note that preference training mainly changes the model’s ranking tendency among candidate answers. It does not guarantee that the model will automatically detect and backtrack from wrong branches during inference. In practice, external verification is still required, such as code execution, rule checking, or retrieval evidence validation.
4. Code Skeleton
The code below is a skeleton of the data construction pipeline, intended to illustrate the structure. teacher_generate needs to be replaced with an actual model call, gold_answers should come from trusted standard answers, and perturb and is_wrong_answer need to be implemented per task. The code cannot run directly; it only works after being connected to real data.
import jsonfrom typing import Callabledef build_stage1(teacher_generate: Callable[[str], dict], prompts: list[str]) -> list[dict]:"""Stage one: keep positive reasoning data that passes answer verification."""rows = []for prompt in prompts:out = teacher_generate(prompt) # returns {"reasoning": str, "answer": str}if not out.get("answer"):continuerows.append({"instruction": prompt,"input": "","output": f"{out['reasoning']}\nFinal answer: {out['answer']}",})return rowsdef build_stage2(teacher_generate: Callable[[str], dict],prompts: list[str],gold_answers: dict[str, str],perturb: Callable[[str], str],is_wrong_answer: Callable[[str, str], b
