Skip to main content

Reflexion Agent

Language Agents with Verbal Reinforcement Learning

中文文档 | English

Architecture • Installation • Quick Start • API Reference


A Python implementation of Reflexion, a framework that equips language agents with the ability to reflect on their failures and learn from them.

Architecture

Reflexion Architecture
│
├── ReflexionAgent (Main Agent)
│   ├── Solves tasks using solve function
│   ├── Evaluates results using evaluate function
│   └── Learns from failures via reflection
│
├── Reflector (Reflection Generator)
│   ├── Analyzes failed attempts
│   ├── Extracts key insights
│   └── Generates improvement suggestions
│
├── ReflexionMemory (Reflection Storage)
│   ├── Stores reflections by task
│   ├── Retrieves relevant reflections
│   └── Formats for prompt inclusion
│
└── Types (Data Structures)
    ├── Task - Problem to solve
    ├── Attempt - Single solution attempt
    ├── Reflection - Failure analysis
    └── EvaluationResult - Success/failure status

Core Workflow

┌─────────────────────────────────────────────────────────────────┐
│                    Reflexion Execution Loop                      │
├─────────────────────────────────────────────────────────────────┤
│                                                                  │
│   Task ──▶ Solve ──▶ Evaluate ──▶ Success? ──▶ Return Answer    │
│               │            │                                     │
│               │            └── No ──▶ Reflect ──▶ Store Memory   │
│               │                                    │             │
│               └────────────────────────────────────┘             │
│                         (Retry with reflections)                 │
│                                                                  │
└─────────────────────────────────────────────────────────────────┘

Installation

pip install reflexion-agent

Quick Start

from reflexion import ReflexionAgent, Task, EvaluationResult, EvaluationStatus

# Define your solve function (typically uses an LLM)
def solve_fn(task, reflections):
    context = ""
    if reflections:
        context = f"\nPrevious insights:\n{reflections[0].to_prompt()}"

    # Your LLM call here
    # response = llm.generate(f"{task.question}{context}")
    return "your_answer"

# Define your evaluation function
def evaluate_fn(task, answer):
    # Check if answer is correct
    if answer == "expected_answer":
        return EvaluationResult(status=EvaluationStatus.SUCCESS, score=1.0)
    return EvaluationResult(
        status=EvaluationStatus.FAILURE,
        score=0.0,
        feedback="Answer is incorrect"
    )

# Create and run agent
agent = ReflexionAgent(solve_fn, evaluate_fn, max_attempts=3)
result = agent.run(Task(question="What is 2+2?"))

print(f"Success: {result.success}")
print(f"Answer: {result.final_answer}")
print(f"Attempts: {result.total_attempts}")

CLI Usage

# Run a single task
reflexion run "What is the capital of France?"

# Start interactive mode
reflexion interactive

# Export trajectory
reflexion run "Solve this" --export trajectory.json

API Reference

ReflexionAgent

Method Description
run(task) Run Reflexion process for a task
run_with_context(question, context) Convenience method with string input
get_memory() Get the reflection memory
get_reflections(task_id) Get stored reflections
clear_memory() Clear all stored reflections

EvaluationStatus

  • SUCCESS - Task completed correctly
  • FAILURE - Task failed
  • PARTIAL - Partially correct

Academic Reference

Implementation of the Reflexion paper:

Reflexion: Language Agents with Verbal Reinforcement Learning

Noah Shinn, Federico Cassano, Ashwin Gopinath, Karthik R. Narasimhan, Shunyu Yao

NeurIPS 2023

Paper: https://arxiv.org/abs/2303.11366

@inproceedings{shinn2023reflexion,
  title={Reflexion: Language Agents with Verbal Reinforcement Learning},
  author={Shinn, Noah and Cassano, Federico and Gopinath, Ashwin and Narasimhan, Karthik R and Yao, Shunyu},
  booktitle={Advances in Neural Information Processing Systems (NeurIPS)},
  year={2023}
}

License

MIT License


Made with ❤️ by AI Agent Research Team

Metadata

Release files for reflexion-agent 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for reflexion-agent 0.1.0
File Size Uploaded Provenance Actions
reflexion_agent-0.1.0.tar.gz 14.7 kB Without provenance Details for reflexion_agent-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for reflexion-agent 0.1.0
File Interpreter ABI Platform Actions
reflexion_agent-0.1.0-py3-none-any.whl Python 3 none any Details for reflexion_agent-0.1.0-py3-none-any.whl

Total release size: 27.8 kB

Release files / reflexion_agent-0.1.0.tar.gz

Download URL reflexion_agent-0.1.0.tar.gz
Size 14.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4f49b5f11a31ba705c7cb3610296bc137431543a6517cbab56e235162358fe36
BLAKE2b-256 checksum
How to use checksums
0d5c806110cbc5a511fbc4337d5b87b7ae9023ffbe7b9449c2706a76448df002
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release files / reflexion_agent-0.1.0-py3-none-any.whl

Download URL reflexion_agent-0.1.0-py3-none-any.whl
Size 13.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f66423a31516431b20d66104d396128e1dcfe28dc0e1c7a4cd43138757f39d14
BLAKE2b-256 checksum
How to use checksums
cae06cf2b4455e9db21dc49bd0aa4d8372a14cc0f61d31009d562033504976ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Security for reflexion-agent 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page