Skip to main content

Pattern: @reflect_trace Decorator

The decorator pattern is the lowest-friction way to add Reflect to an existing agent function. Annotate your function with @reflect_trace and Reflect handles memory retrieval, trace submission, and retrieved_memory_ids tracking - you just write the agent logic.
This example uses the OpenAI Agents SDK with a WebSearchTool.

Prerequisites

Install dependencies:

Full example

openai_agents_reflect_simple.py

Run it

How it works

1

Create the client

One ReflectClient instance is shared across all decorated functions in your app.
2

Annotate your function with @reflect_trace

The decorator intercepts the call, retrieves relevant memories, and injects a TraceContext as the first argument. The task parameter tells Reflect how to extract the task string from your function’s arguments.
ctx.augmented_task contains the original question with past memories appended. Pass this to the agent instead of the raw question.
3

Run the agent with augmented context

The agent receives the memory-augmented prompt, so it can draw on what worked (or didn’t) in previous runs.
4

Convert the agent result to a trajectory

from_openai_agents converts the agent’s message history into the format Reflect expects.
Reflect ships converters for popular agent frameworks so you don’t have to map message formats manually.
5

Return a TraceResult

Return a TraceResult from the decorated function. The decorator uses it to submit the trace and passes output back to the original caller.
To add a review at submission time, include result="pass" or result="fail" and optionally feedback_text.

Adding a review

To close the learning loop immediately, include result in TraceResult:
Without result, the trace is stored with a pending review status and can be reviewed later from the dashboard.