Pattern: Context Manager
The context manager pattern (with reflect.trace(...)) gives you full control over when the trace is submitted and lets you decide the review result after inspecting the agent’s output. It fits naturally into multi-step workflows where a decorator would be too rigid.
Prerequisites
Full example
deepagents_exa_quickstart.py
Run it
How it works
Open a trace context
reflect.trace(task) retrieves relevant memories and opens a trace that will be submitted when the with block exits.limit controls how many memories are retrieved. lambda_ balances semantic similarity vs. utility when ranking them (0 = pure similarity, 1 = pure utility).Pass the augmented task to the agent
ctx.augmented_task is the original task with relevant memories from past runs prepended. Passing this to the agent means it can draw on what worked before.Define a tool the agent can call
The agent can call
internet_search to retrieve live data. Exa returns highlights from matching pages, which the agent cites in its report.Adding a review
Passresult to ctx.set_output to close the learning loop at submission time:
result, the trace is stored with a pending review and can be reviewed later from the dashboard.
The lambda_ parameter
When retrieving memories, Reflect ranks candidates by a blended score:
lambda_ = 0- pure semantic similarity (most relevant to this query)lambda_ = 0.5- balanced (default)lambda_ = 1- pure utility - memories from the most successful past runs)