Back to AI Recipes

Self-Critique

ReflectionEvaluation

Reflection

Enable agents to evaluate and improve their own outputs through structured self-reflection.

Summary

The Self-Critique pattern enables LLMs to evaluate their own outputs against criteria and iteratively improve them. A generate-critique-refine cycle allows agents to identify weaknesses, understand errors, and produce higher-quality results. This reflection capability is essential for tasks requiring accuracy and reliability.

How it works

  1. Generate: Produce initial output
  2. Critique: Evaluate output against explicit criteria
  3. Identify Issues: Flag specific weaknesses or errors
  4. Refine: Generate improved version addressing critiques
  5. Repeat: Continue until quality threshold met

Evaluation criteria

  • Accuracy: Are facts correct? Is reasoning valid?
  • Completeness: Did I address all requirements?
  • Coherence: Is the response well-organized?
  • Safety: Are there harmful or biased elements?
  • Usefulness: Is the response actionable?

When to use

  • Tasks requiring high accuracy (code, math, facts)
  • Content generation with strict quality standards
  • Scenarios where self-correction adds value
  • Applications where multiple iterations are acceptable
Code View

Self-Critique Implementation

// Self-Critique recipe using OpenAI
// Install: bun add openai

import OpenAI from "openai";

async function main() {
  const input = "Add your prompt here.";
  const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
  const system = "You are a senior AI engineer and technical writer. Apply the agent recipe to structure the reasoning and produce a useful result. Recipe: Self-Critique. Description: Enable agents to evaluate and improve their own outputs through structured self-reflection. Focus: Reflection Provide actionable, implementation-ready guidance.";
  const user = `Request: ${input}`;

  const openaiResponse = await openai.chat.completions.create({
    model: "gpt-4o-mini",
    messages: [
      { role: "system", content: system },
      { role: "user", content: user },
    ],
  });

  const openaiText = openaiResponse.choices[0]?.message?.content?.trim() ?? "";

  console.log(openaiText);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});