Chain of Thought and Self-Critique
Complete the full lesson to earn 25 points
Work through each section, then tap “Mark as Complete” on the last one.
✦ Skip the page breaks and see fewer ads — read each lesson on a single page with Pro
Mastering Chain of Thought and Self-Critique in Generative AI
Introduction: The Evolution of Prompt Engineering
In the early days of working with Large Language Models (LLMs), users often treated them like simple search engines. You would ask a question, and the model would generate an answer. However, as we began to rely on these models for complex reasoning, logical deduction, and multi-step problem solving, we hit a wall. When asked to solve a difficult math problem or write a complex piece of code in a single "shot," the models often produced "hallucinations"—confidently stated but factually incorrect outputs. This is where the concept of Chain of Thought (CoT) and Self-Critique emerged as critical methodologies for building reliable agentic systems.
Chain of Thought is a prompting technique that encourages the model to generate intermediate reasoning steps before arriving at a final answer. Instead of jumping to a conclusion, the model "thinks out loud," breaking down the problem into manageable logical segments. Self-Critique, on the other hand, is the process of having the model review its own output, identify potential flaws or inconsistencies, and iterate on the result. By combining these two techniques, we move away from simple input-output interactions and toward a collaborative reasoning process where the AI acts more like a thoughtful human partner.
This lesson explores how to implement these techniques, why they are essential for production-grade AI applications, and how you can structure your agentic workflows to maximize accuracy and minimize errors.
Understanding Chain of Thought (CoT)
Chain of Thought is not just a "trick"; it is a fundamental shift in how we interact with LLMs. At its core, CoT forces the model to allocate more "compute" to the reasoning process. When you ask a model to solve a problem without guidance, it is essentially trying to predict the final token immediately. By providing a structure that requires reasoning steps, you allow the model to build an internal context that supports the final conclusion.
Why Chain of Thought Works
When a model generates text, each word is conditioned on the words that came before it. If the model generates a reasoning step, that step becomes part of the context for the next step. If the reasoning is logical, the final answer is naturally more likely to be correct. This mirrors how humans solve problems: we don't calculate the square root of a complex number in our heads instantly; we write down the steps, perform the arithmetic, and check our work.
Implementing Basic Chain of Thought
The simplest way to implement CoT is through "Few-Shot Prompting." You provide the model with examples of how to break down a problem.
Example: The "Let's Think Step-by-Step" approach
Tip: The Zero-Shot CoT Trigger Researchers discovered that simply adding the phrase "Let's think step-by-step" to a prompt can significantly improve performance on reasoning tasks. This acts as a trigger for the model to adopt a sequential reasoning pattern without requiring extensive examples.
Consider a scenario where you want an agent to determine if a customer's request for a refund is valid based on a set of rules.
- Bad Prompt: "Is this refund request valid? [Request details] [Policy details]"
- Good Prompt (CoT): "Analyze the refund request based on the policy provided. Break your analysis down into these steps:
- Identify the purchase date and policy window.
- Determine if the item is in the category of non-refundable goods.
- Check if the customer has provided the required proof of purchase.
- Provide a final conclusion. Let's think step-by-step."
By explicitly defining the steps, you constrain the model's output to a logical flow, reducing the chance that it will gloss over important details like the purchase date or the item category.
Deep Dive: Self-Critique and Iterative Refinement
While Chain of Thought improves the reasoning process, it doesn't guarantee perfection. A model might follow a chain of logic that is flawed from the beginning. Self-Critique is the mechanism we use to catch these errors. This involves a multi-turn conversation where the model (or a second, specialized model) reviews the initial output and searches for logical gaps, factual inaccuracies, or tone violations.
The Self-Critique Workflow
A standard Self-Critique pipeline usually follows these three stages:
- Drafting: The model generates an initial response based on the instructions.
- Evaluation: The model is prompted to critique its own response. We ask it to act as an auditor or a devil’s advocate.
- Refinement: The model uses the critique to generate a final, improved version of the response.
Practical Example: Writing Technical Documentation
Imagine you are building an agent that drafts technical documentation for your software. You don't want the documentation to be ambiguous.
Step 1: Initial Draft
"To install the software, run the command npm install. Then, you are done."
Step 2: Self-Critique Prompt "Critique the previous installation instructions. Are there any missing prerequisites? Is the command specific enough? Are there potential errors a user might encounter that weren't addressed?"
Step 3: Refinement The model might respond: "The critique shows the instructions were too brief. I failed to mention that Node.js must be installed, and I didn't specify the directory. I also should mention common permission errors. Here is the revised version..."
Callout: The "Critic" Persona When implementing Self-Critique, assigning a specific persona to the model significantly improves results. Instead of saying "Review your work," use "You are a senior software engineer with a focus on documentation clarity. Review this draft for technical accuracy and user-friendliness." This helps the model access a more precise subset of its training data.
Implementing Agentic Loops in Code
To move beyond simple prompts, we need to build agentic loops. An agentic loop is a programmatic flow where the output of one model call is fed back into another, often with a "tool" or "judge" involved.
Below is a Python-like conceptual implementation of a self-critique loop.
def generate_and_critique(task_prompt):
# Step 1: Draft
initial_draft = llm.generate(f"Task: {task_prompt}. Please provide a detailed answer.")
# Step 2: Critique
critique = llm.generate(f"Review this draft for errors: {initial_draft}. List any issues.")
# Step 3: Refinement
final_output = llm.generate(f"Draft: {initial_draft}. Critique: {critique}. Please rewrite the draft to address these issues.")
return final_output
Why this structure matters
This code snippet illustrates the "Agentic" nature of the solution. The agent isn't just a static function; it is a process that iterates. In a production environment, you would add logic to handle the "critique" output programmatically. For example, if the critique identifies a missing fact, the agent could be programmed to trigger a search tool to find that information before refining the answer.
Comparing Chain of Thought vs. Self-Critique
It is important to understand when to use these techniques and how they differ.
| Feature | Chain of Thought (CoT) | Self-Critique |
|---|---|---|
| Purpose | Improving reasoning during generation | Improving quality via post-generation review |
| Timing | Happens during the initial response | Happens after the initial response |
| Complexity | Low (adding instructions) | High (requires multiple model calls) |
| Primary Benefit | Better accuracy on logic tasks | Better polish, tone, and fact-checking |
Warning: The Cost of Iteration Every time you run a self-critique loop, you are sending more tokens to the API. This increases both the latency (time it takes to get an answer) and the financial cost. Do not use self-critique for every simple request. Reserve it for high-stakes tasks where accuracy is more important than speed.
Best Practices for Building Agentic Systems
1. Define Clear Evaluation Criteria
If you want the model to critique itself, you must give it a rubric. A generic "check this for errors" will result in generic feedback. Instead, provide a structured list:
- Accuracy: Are the facts verified?
- Completeness: Did we miss any required steps?
- Tone: Is the language professional?
- Safety: Does the output follow our content guidelines?
2. Keep the Context Window Clean
When performing multiple rounds of critique, the context window can become cluttered. If you are using a very long history, the model might get confused by its own previous errors. Periodically summarize the progress or keep only the "Draft" and the "Critique" in the prompt for the final refinement step.
3. Use "Chain of Verification" (CoVe)
A more advanced version of self-critique is the Chain of Verification. In this workflow, the model first generates a draft, then generates "verification questions" to test its own assertions, then answers those questions independently, and finally updates the draft based on the answers. This is highly effective for reducing hallucinations in fact-heavy tasks.
4. Provide Examples (Few-Shot)
Even with CoT, the model benefits from seeing examples of a good critique. Include a "Before" (a poor response) and an "After" (the corrected response) in your system prompt. This helps the model understand the "style" of critique you expect.
Common Pitfalls and How to Avoid Them
The "Looping" Problem
Sometimes, a model gets stuck in a loop where it critiques its own work, makes a minor change, and then the next critique identifies a new "error" that wasn't there before.
- Solution: Limit the number of iterations (e.g., max 2-3 rounds). If the model hasn't improved after three tries, it’s likely that the underlying model is incapable of solving the specific problem or the prompt is fundamentally flawed.
The "Over-Correction" Problem
In an effort to fix a minor issue, the model might accidentally rewrite the entire text and lose the original intent.
- Solution: Use "In-place editing" instructions. Tell the model: "Only modify the sections identified in the critique. Keep the rest of the text exactly as it is."
The "Hallucinated Critique"
Sometimes, the model will invent errors that don't exist in its own output, essentially gaslighting itself.
- Solution: Use a separate model instance for the critique if possible, or provide a grounded source (like a document or database result) for the model to compare against. Don't rely solely on the model's internal knowledge for the verification step.
Advanced Implementation: Designing a Multi-Agent System
As you scale your implementation, you may find that one model "brain" isn't enough. In a more sophisticated setup, you can assign different roles to different instances of the model.
- The Architect: Receives the user request and breaks it into a plan (Chain of Thought).
- The Worker: Executes the steps defined by the Architect.
- The Auditor: Reviews the output from the Worker against the original request (Self-Critique).
This modular approach ensures that the "reasoner" is focused on logic, the "worker" is focused on content generation, and the "auditor" is focused on verification. This separation of concerns is a hallmark of professional-grade agentic systems.
Step-by-Step Guide: Implementing a Basic Chain-of-Thought Agent
If you are building an application that requires high reliability, follow these steps to implement a robust CoT and Critique pipeline.
Step 1: Define the System Prompt
Create a "System Prompt" that sets the persona. Include the instruction to always use a chain-of-thought process.
- "You are an expert assistant. For every request, first outline your plan, then execute the steps, and finally review your work for errors."
Step 2: Structure the Output
Use structured formatting (like Markdown or JSON) to ensure the model output is machine-readable.
- Example:
[PLAN]: 1. ... 2. ... [EXECUTION]: ... [CRITIQUE]: ... [FINAL_OUTPUT]: ...
Step 3: Implement the Logic
Use your application code (Python, Node.js, etc.) to parse these sections. If the [CRITIQUE] section indicates that the [EXECUTION] was poor, have your code trigger a re-run of the [EXECUTION] phase with the critique included as context.
Step 4: Monitoring and Logging
You cannot optimize what you don't measure. Log the [PLAN], [EXECUTION], and [CRITIQUE] sections for every interaction. Review the logs periodically to see where the model is failing. Is it failing at the planning stage? Or is the execution correct but the self-critique is too harsh? This data is the key to refining your prompt engineering.
The Role of Grounding
A common mistake is assuming that Chain of Thought can fix a lack of knowledge. If you ask a model to solve a problem that requires information it doesn't have, no amount of "thinking out loud" will help.
Note: Grounding is not Reasoning Chain of Thought helps the model reason through information it possesses. It does not provide the model with new information. If your agent needs to answer questions about proprietary internal data, you must provide that data through RAG (Retrieval-Augmented Generation) before the reasoning process begins.
Effective systems combine RAG with CoT. The workflow looks like this:
- Retrieve: Fetch relevant documents based on the user query.
- Reason (CoT): Use the retrieved documents as context to formulate an answer.
- Verify (Self-Critique): Check if the answer actually uses the retrieved information correctly.
Industry Standards and Best Practices
In the industry, we often refer to the "Reasoning-Action-Observation" (ReAct) pattern. This is a specific type of Chain of Thought where the agent is allowed to interact with the world (via APIs or tools).
- Reasoning: The model explains what it needs to do.
- Action: The model calls a tool (e.g.,
get_weather(city)). - Observation: The model receives the output of the tool.
When you add Self-Critique to a ReAct loop, you create an agent that can correct its own tool-use mistakes. For example, if the agent calls a search tool and gets no results, it can critique its own query, realize it was too specific, and try a broader search. This is the gold standard for building agents that function in the real world.
Summary and Key Takeaways
Implementing Generative AI and agentic solutions requires moving beyond simple "chat" interfaces. By incorporating Chain of Thought and Self-Critique, you move toward a model of "collaborative reasoning" that significantly improves the reliability and safety of your applications.
Key Takeaways:
- Chain of Thought is a Compute Multiplier: By forcing the model to articulate its logic, you provide it with the space to build a coherent, step-by-step solution, reducing the probability of logical errors.
- Self-Critique Adds a Quality Gate: Use self-critique to catch hallucinations, tone issues, and factual errors before the final output reaches the user.
- Context Management is Critical: When building these workflows, be mindful of token usage and context length. Summarize or prune your history to keep the model focused on the task at hand.
- Adopt a Modular Architecture: Don't try to do everything in one prompt. Separate your planning, execution, and verification into distinct steps or even distinct model agents.
- Use Rubrics for Evaluation: Don't rely on vague instructions like "check for errors." Provide a clear, structured rubric that tells the model exactly what a "correct" answer looks like.
- Grounding Before Reasoning: Remember that these techniques improve reasoning, not knowledge. Ensure your model is grounded in accurate, retrieved data before asking it to think through complex problems.
- Iterate Based on Logs: Treat your prompts like code. Log your agent's reasoning steps, review the failures, and iteratively improve your system prompts based on real-world performance data.
By mastering these techniques, you move from being a user of AI to an architect of intelligent systems. The future of this field lies not in bigger models, but in better-structured workflows that leverage the inherent reasoning capabilities of existing technology. Focus on building these robust loops, and you will find that your AI applications become significantly more reliable and useful for your users.
Frequently Asked Questions (FAQ)
Q: Does Chain of Thought work on all models? A: CoT is most effective on larger, more capable models (like GPT-4, Claude 3.5 Sonnet, etc.). Smaller, specialized models may struggle to follow complex reasoning chains and might produce worse results. Always test with your specific model choice.
Q: How many steps should a Chain of Thought be? A: There is no magic number. A good rule of thumb is to break the problem into the smallest logical units possible without making the prompt excessively long. If the model is failing, try breaking the steps down further.
Q: Is Self-Critique too slow for real-time applications? A: Yes, it can be. If you are building a real-time chat interface, you might want to perform the critique asynchronously or only on critical tasks. For non-real-time tasks (like generating reports or processing documents), the extra time is almost always worth the increase in quality.
Q: Can I use a smaller model for the critique to save money? A: Absolutely. This is a common pattern. You can use a very capable model (the "Expert") to generate the initial draft, and a cheaper, faster model (the "Auditor") to perform the critique and refinement. This is a great way to balance cost and quality.
Q: What if the model refuses to critique itself? A: Occasionally, a model may be "too polite" and refuse to find flaws in its own work. You may need to use a "jailbreak-style" prompt or a very firm persona, such as: "You are a professional auditor. Your job is to find at least two potential improvements in this text, regardless of how good it seems."
Enjoying the courses?
Everything stays free. Pro shows fewer ads, doubles your daily points limit so you progress twice as fast, and lets you read each lesson on one page.
- ✓ Fewer advertisements
- ✓ 2× daily points limit
- ✓ Distraction-free lessons