The reflection pattern is one of four key agentic AI design patterns (alongside tool use, planning, and multi-agent collaboration) that enable AI systems to handle complex tasks. It allows an AI agent to generate an output, critique it, and amend it. This feedback loop makes it easier for the agent to catch errors and improve output quality.

How the reflection pattern works

Let’s say a team has built a business process automation solution, such as a customer support agent chat, that uses a reflection pattern. Once the agent is triggered, it follows a three-stage loop.

1.Generate

First, the agent’s attached AI model creates an initial response to the prompt, which in this case is a customer’s question. It accesses its resources, such as product documentation or company policies, to create a “first draft” response. It then holds the output in a temporary state for self-evaluation.

2.Reflect

Next, the agent prompts either the same or a different model to critique the initial generation based on predefined criteria a developer has built into the pattern design. In this case, it may ask itself questions like “Did I consider all company policies?” and “Did I provide an answer that could be harmful or incorrect?” If the agent developer has opted to use a tool-augmented reflection pattern, the model may access external systems, such as a search engine, to complete its evaluation. The model catches and catalogs potential flaws that need to be fixed before responding to the customer.

3.Refine

The agent feeds its original draft and critique back into the generative model. It uses the additional context to revise its response. It corrects errors and removes irrelevant information to produce a higher-quality output. 

It’s important for the agent’s developer to establish stopping criteria, such as a fixed number of iterations, to stop the agent from getting stuck in an indefinite refinement loop. Without stopping criteria, the agent might burn through model tokens or iterate past the optimal response and reduce its quality. Setting the right stopping point can reduce computational cost and latency while preserving output quality.

Once the agent has reached its stopping point, it returns the refined output to the customer, answering their question.

A simplified diagram illustrating the reflect-refine pattern. Note that the Generate / Refine steps are not necessarily single-turn LLM completions. These can be multi-turn AI-agents with their own tools and RAG.

Reflection pattern variations

Agent developers should choose a reflection pattern variation that meets their needs for output quality and latency tolerance. This is particularly important as the agent parses multiple sources of feedback.

Single-model self-reflection

Single-model self-reflection is the simplest reflection pattern design strategy. In this setup, the model that the agent uses is both the generator and the evaluator. The model critiques its generative output based on predetermined criteria. The agent then feeds the model’s critique back into its generation prompt. This design pattern is straightforward and can improve an agent’s output. But the model is limited to its own internal knowledge and reasoning processes to evaluate its work. It may fail to recognize its errors and possibly reinforce hallucinations in its final output, which is often called a self-preference bias.

Multi-agent reflection

Agent developers may choose to design reflection patterns with multiple agents to reduce the quality risks associated with single-model reflection. In this approach, one agent controls the model’s generation and sends the first draft to a second agent to oversee the reflection. Think of it as a type of peer review. An agent or model with a distinct reasoning process can catch errors or hallucinations the original model may overlook. 

Tool-augmented reflection

This design pattern introduces external tools into the reflection stage. If the model determines that its internal knowledge and reasoning is insufficient to correct an error, it can invoke an external tool, such as a search engine or database. This process often improves the model’s ability to validate factual claims, but it depends on the accuracy of the external tool or data used.

AI architects and engineers can use workflow automation tools like n8n to run multi-step reasoning agents or agentic Retrieval-Augmented Generation (RAG). Engineers can work with SDKs. However, when using workflow automation tools with a visual interface, they can iterate more quickly and better track the progress of each stage. n8n is a source-available, AI-native automation platform, where engineers and nontechnical users alike can create AI agents and agentic workflows. Users can chain multiple AI agent nodes with a distinct system prompt for generation and another for critique. These nodes are connected via sub-workflows to manage complex modular processes.

When to use the reflection pattern (and when not to)

Choosing the right design pattern can have a significant impact on the performance and cost of your AI task automation. The reflection pattern can improve output quality, but it may not be the best choice for your project.

You might want to use a reflection pattern if your project meets the following criteria: 

  • Response quality is the most important factor for your team. Is it more important for you to have a model that puts output quality and logical consistency above speed? If the answer is yes, then you’ll find that this iterative pattern generates the most value for your team. 
  • You have verifiable correctness criteria. Reflection is especially effective when the output can be measured against clear standards or a pre-defined check-list. 
  • Your single-pass outputs frequently contain errors. Reflection can help detect and correct errors.

You might want to use a different design pattern if your project meets the following criteria:

  • The task is time-sensitive. Each loop of the reflection pattern requires additional model inference and sometimes external tool calls that add latency. If speed is your priority, reflection may not work for your agent.
  • First-draft quality is satisfactory. If the first version of outputs generally meets your business needs, the extra complexity and cost of reflection might not be worth it.
  • You need to keep costs down. If you’re processing a high volume of requests, the cost of sending out multiple LLM calls may become prohibitive.

If you’re interested in using the reflection pattern but are worried about cost, n8n can help. You can use the platform to track AI agent token consumption and costs with an API, easily swapping between models. If reflection ends up being too expensive for your use case, you can adjust your workflow.

Implement agentic design patterns with n8n

If you need to improve the accuracy and quality of an AI agent’s output, the reflection pattern may be a good design pattern to try. You can use n8n’s sub-workflow architecture, AI agent nodes, and conditional branching to implement reflection loops visually. Get started with a free trial today.

FAQ

What are common uses of reflection patterns?

In addition to software development, the reflection pattern is used in areas such as content creation and editing, data analysis and reporting, as well as legal and financial summaries. In all these cases, the initial result may be incomplete or contain inaccuracies, so a single cycle of automatic review and correction often helps improve the results.

What’s the difference between Reflexion and reflection patterns?

A reflection pattern is a specific design strategy for models to iterate their way to better answers. Reflexion is a general research approach that helps AI systems learn from mistakes across multiple independent trials.

Share with us

n8n users come from a wide range of backgrounds, experience levels, and interests. We have been looking to highlight different users and their projects in our blog posts. If you're working with n8n and would like to inspire the community, contact us 💌

SHARE