BrainBank
AI Classroom/SkillDeepLearningAI

2.1 Reflection to Improve Task Outputs

8/5/2026, 5:11:47 PM

#skill#reflection

Reflection to Improve Task Outputs: when to use it, why it works, where it sits in an agentic system, and how to implement, evaluate, and harden it in production.

2.1 Reflection to Improve Task Outputs

Learning objectives

After this section, you should be able to:

  • Explain what reflection to improve task outputs contributes to an agentic system.
  • Decide when to use it and when a simpler design is sufficient.
  • Implement the pattern as observable, testable components.
  • Identify its principal cost, safety, and reliability risks.

When to use it

Use reflection when defects can be identified from the draft or with inexpensive verification tools.

Why it works

A second pass can compare output against explicit criteria and produce targeted revisions.

How it fits the system

Rendering diagram…

The arrows show control and data movement, not necessarily separate models. A deterministic function, one model called multiple times, or several models may implement the boxes. Preserve trace identifiers across the flow.

Step-by-step implementation

  1. Generate an initial output. Write the input, expected output, owner, and failure condition before implementation.
  2. Evaluate it against a rubric. Keep the decision observable in logs or structured state so it can be evaluated.
  3. Return concrete defects, not vague criticism. Apply least privilege and validate assumptions at this boundary.
  4. Revise only the affected areas. Capture the result and enough metadata to reproduce or diagnose it.
  5. Stop when thresholds pass or the iteration cap is reached. Compare the result with explicit acceptance criteria before continuing.

Technical implementation notes

  • State: Keep messages, tool calls, observations, artifacts, decision reasons, attempt count, token use, latency, and final status in a structured run record.
  • Contracts: Define each component with typed inputs, typed outputs, allowed side effects, timeouts, and error categories.
  • Controls: Use least-privilege credentials, allowlisted tools, bounded loops, input validation, output validation, and approval gates for consequential actions.
  • Observability: Record prompts or prompt versions, model and parameters, tool arguments, tool results, exceptions, timestamps, and cost—subject to privacy rules.
  • Evaluation: Test representative, edge, adversarial, and failure-recovery cases. Compare against the simplest viable baseline.

Worked example

A writing agent checks factual support, structure, tone, and length before revising.

INPUT: user goal + constraints
STATE: {run_id, step, observations, budget, status}
DECIDE: next bounded action
VALIDATE: permissions, arguments, and policy
EXECUTE: model call, deterministic code, or tool
OBSERVE: structured result or categorized error
STOP: acceptance criteria pass, budget reached, or human escalation

Failure modes and mitigations

Failure modeSignalMitigation
Vague objectiveOutput appears fluent but misses the taskConvert the request into measurable acceptance criteria
Unbounded loopRepeated calls without material progressSet iteration, token, time, and cost limits
Bad intermediate stateLater steps amplify an early mistakeValidate each component contract and retain traces
Unsafe side effectTool attempts an unauthorized changeApply authorization, least privilege, dry-run, and approval gates
Evaluation blind spotDemo succeeds but real cases failExpand the test set using production-like and adversarial cases

Verification checklist

  • The use case justifies this pattern over a simpler one-shot call.
  • Inputs, outputs, and success criteria are explicit.
  • Tool calls and side effects are validated and permissioned.
  • Loops have hard budgets and meaningful stop conditions.
  • Failures produce safe retries, fallbacks, or escalation.
  • Quality, latency, and cost are measured together.
  • Regression tests include at least one failure case.

Review questions

  1. What observable failure would show that this pattern is misapplied?
  2. Which part should be deterministic rather than delegated to an LLM?
  3. What is the minimum context required at each step?
  4. Where should a human approval or escalation gate sit?
  5. Which metric would prove that the added complexity is worthwhile?

Practical exercise

Implement a minimal version for one narrow task. Save three traces: a successful run, a recoverable failure, and a case that must stop or escalate. Write one regression test for each trace and compare the result with a direct-generation baseline.

Learning map

Page 10 of 40 in DeepLearningAI > Agentic AI. Read after "1.8 Agentic Design Patterns". Continue to "2.2 Why Not Just Use Direct Generation?" next. All 37 numbered lesson pages (1.1-5.7) share one template — state, contracts, controls, observability, and evaluation, introduced in full in 1.1 Course Overview — so this page assumes that shape and focuses on what's unique to its own topic.

Get hands-on — step by step

Complete the Practical exercise at the end of this page: implement a minimal version of reflection to improve task outputs, save a successful trace, a recoverable-failure trace, and an escalation trace, then write one regression test per trace and compare against a direct-generation baseline.

Top 3 sources

  1. 1
    Claude Docs: Prompt Engineering Overview

    Anthropic's official guidance on structuring prompts and multi-step model interactions.

    https://docs.claude.com/en/docs/build-with-claude/prompt-engineering/overview

  2. 2
    DeepLearning.AI Course Catalog

    The broader DeepLearning.AI curriculum this study guide's structure is organized around.

    https://www.deeplearning.ai/courses/

Links are AI-suggested — worth a quick sanity check before diving in.