0
Phase 9

Reflexion

~3 min read

Concept & How It Works

    Why Does It Exist?

    Standard agents repeat the same mistakes every time. Reflexion adds a learning loop: try → fail → reflect on why → store lesson → retry with lesson in context. Agents get better at specific tasks through experience, not weight updates.

    Real-World Analogy

    Reflexion is like a student who keeps a mistake journal — after each failed test, they write what went wrong and review the journal before the next attempt.
    Loading diagram...

    Visual Workflows

    What is Reflexion?

    Loading diagram...

    Example

    Scenario

    Trial 1: Agent queries 'sales data' without date filter → wrong results. Reflection: 'Always specify date range for sales queries.' Trial 2: Agent queries 'Q3 2024 sales' → correct results. Lesson stored for future sales tasks.

    Solution

    In Agent Design Patterns, apply Reflexion to this scenario: Trial 1: Agent queries 'sales data' without date filter → wrong results. Identify the inputs, run the technique, validate the output, and note one thing you would monitor in production.

    Practice Task

    Do this before moving to the next module — reading alone is not enough.

    Open the Code Walkthrough below and run it locally. Change one parameter related to Reflexion (e.g. model, temperature, top_k, or tool name), observe the difference in output, and write 2–3 sentences explaining what changed.

    Code Walkthrough

    Highlighted lines show where Reflexion happens in the code.

    Reflexion
    1def reflexion_agent(task: str, max_trials: int = 3) -> str:  # define a reusable function2    reflections = []  # key line for Reflexion3
    4    for trial in range(max_trials):5        context = ""6        if reflections:  # key line for Reflexion7            context = "Lessons from past attempts:\n" + "\n".join(reflections)  # key line for Reflexion8
    9        result = agent_attempt(task, context)10        score = evaluate(result, task)11
    12        if score >= 0.9:13            return result  # return the result14
    15        reflection = generate_reflection(task, result, score)  # key line for Reflexion16        reflections.append(reflection)  # key line for Reflexion17        store_in_memory(task, reflection)  # key line for Reflexion18
    19    return result  # return the result

    Commands to Remember

    Commands to Remember

    • pip install langchain langchain-openai # patterns work with any LLM SDK
    • python react_agent.py # run a ReAct-style agent loop
    • pip install tenacity # retry logic for agent steps

    Common Mistakes

    • Vague reflections — 'it didn't work' teaches nothing
    • Too many trials — wasted cost after 3 failures
    • Not storing reflections for future similar tasks
    • Same reflection repeated across trials — no learning
    • No evaluator — agent doesn't know it failed

    Cheat Sheet

    Quick recap — the most important points from this module.

    Cheat Sheet

    quick ref
    • Try → reflect → retry
    • 2-3 trials typical
    • Specific reflections
    • Episodic memory store
    • max_trials=3-5
    • No retraining needed