3 min read

Reflection Agents: AI Self-Improvement with Mastra Framework

In one of my recent articles, I talked about Docling, a fantastic Python library that's been growing in popularity in the generative AI ecosystem. But today I want to introduce another lib that's winning over full-stack devs: the Mastra Framework.

In this article, I'll explore an agent architecture called Reflection Agent and show you how to implement it using Mastra in a practical and efficient way.


What is a Reflection Agent?

Imagine an AI agent that doesn't just respond, but thinks about its own answers and continuously improves them. That's a Reflection Agent!

The Self-Improvement Cycle

This pattern is inspired by self-reflection and self-correction techniques used in advanced AI systems, where the model not only generates content but also evaluates and continuously improves it.

Why does this matter?

  • Higher quality responses
  • Reduction of errors and inaccuracies
  • Automatic iterative improvement
  • Simulation of critical thinking

Meet the Mastra Framework

Mastra is a modern and powerful framework for building AI-based applications. Developed with TypeScript, it offers a modular and extensible architecture for creating agents, workflows, and AI tools.

💡 Real Experience: We've been using it here at DevelopsToday and it has proven to be an excellent high-level framework for various use cases.

Key Features

Why Choose Mastra?

  • Type-Safe: TypeScript end-to-end
  • Modular: Flexible and extensible architecture
  • Performance: Optimized for production
  • Developer Experience: Clean and intuitive API
  • Growing Community: Active support and rich documentation

Quick Example

Simple, elegant, and powerful!


Reflection Agent Architecture

Let's build a system with 4 main components that work in harmony:

1. Generator Agent (The Creator)

Mission: Generate the initial response based on user input.

What it does:

  • Receives a theme as input
  • Generates a short motivational phrase (15-20 words)
  • Focuses on impact and memorability

2. Reflector Agent (The Critic)

Mission: Critically analyze the response and provide constructive feedback.

What it does:

  • Evaluates quality, impact, and relevance
  • Identifies strengths and weaknesses
  • Provides concrete improvement suggestions
  • Focuses on brevity and direction

3. Refiner Agent (The Perfectionist)

Mission: Improve the original response by applying received feedback.

What it does:

  • Incorporates reflector's suggestions
  • Maintains identified strengths
  • Produces progressively better versions
  • Ensures brevity and impact

4. Orchestrator (The Conductor)

Mission: Coordinate the complete reflection cycle and decide when to stop.

Responsibilities:

  • Manages the iterative cycle
  • Controls quality criteria
  • Defines when to stop (threshold or max iterations)
  • Maintains improvement history

Complete Execution Flow


Real Execution Example

Let's see the system in action!

Input


Iteration 1

Generator:

Reflector:

  • Strengths: Positive message
  • Weaknesses: Too generic
  • Suggestions: Add emotional resonance
  • Score: 0.60 ❌

Iteration 2

Refiner:

Reflector:

  • Strengths: More impactful
  • Weaknesses: Could be more concise
  • Suggestions: Refine structure
  • Score: 0.65 ❌

Iteration 3

Refiner:

Reflector:

  • Strengths: Excellent emotional resonance, clear message
  • Weaknesses: None significant
  • Score: 0.75 ❌

🎉 Final Result

Improvement achieved: 60% → 75% (+25% quality)


Conclusion

The Reflection Agent pattern combined with the Mastra Framework offers a powerful approach to create AI systems that:

  • Think critically about their own responses
  • Continuously improve output quality
  • Maintain type-safety and developer experience
  • Scale easily to complex use cases

Next Steps

  1. Try the complete code on GitHub
  2. Explore the Mastra documentation
  3. Adapt it to your specific use case
  4. Share your results!