Deterministic memories
Copy as MarkdownIn the previous post we gave the agent a memory by calling add_session_to_memory() in an after-agent callback. That works, but it leans on an embedding model to decide what’s worth keeping from each session — which makes it a little non-deterministic. Say “remember that …” and it almost certainly will; let it summarize a trading run on its own, and the specific trades may or may not survive.
For a trading agent, the trades are exactly the thing we want to remember, every single time. So rather than hope the model picks them out, let’s write that memory ourselves.
There are two pieces to saving a memory deterministically.
First, capture what happened — in a structured way, at the moment it happens. The trade is a tool call, so place_trade_order is the natural place to record it: we stash each confirmed fill in the tool context’s session state as the order goes through.
# Record the confirmed fill in session state so save_to_memory can
# persist a clean, structured trade record at the end of the cycle.
if tool_context is not None:
trades = tool_context.state.get("trades_this_session", [])
trades.append({
"symbol": symbol,
"side": side,
"amount_usd": amount_usd,
"qty": qty,
"reason": reason,
})
tool_context.state["trades_this_session"] = trades
Second, when the session’s memory save fires, do the embedding-based review and write our own memory for the trades. The full transcript still goes to memory for fuzzy cross-session recall — news context, the agent’s reasoning — but on top of that we build a clean, structured record from the fills we captured and save it directly with add_memory(). There’s no LLM in that second path, so the trade record lands every time.
async def save_to_memory(callback_context: CallbackContext):
# Full transcript: general cross-session recall (news context, reasoning).
await callback_context.add_events_to_memory(
events=callback_context.session.events,
custom_metadata={"force_flush": True},
)
# Structured trade record: a clean, guaranteed memory of exactly what was
# traded and why, built from the orders captured in place_trade_order.
trades = callback_context.state.get("trades_this_session", [])
if not trades:
return
lines = []
for t in trades:
size = f"${t['amount_usd']}" if t.get("amount_usd") else f"{t['qty']} shares"
rationale = f" — {t['reason']}" if t.get("reason") else ""
lines.append(f"{t['side'].upper()} {t['symbol']} {size}{rationale}")
text = "Trades executed this session:\n" + "\n".join(lines)
print(text, flush=True)
await callback_context.add_memory(
memories=[MemoryEntry(
content=types.Content(role="model", parts=[types.Part(text=text)]),
author="TradingAgent",
timestamp=datetime.now(timezone.utc).isoformat(),
)],
custom_metadata={"type": "trade", "force_flush": True},
)
Two things are worth calling out in there. We’ve swapped last post’s add_session_to_memory() for add_events_to_memory(), which lets us pass the events explicitly and force a flush — but under the hood it’s still the same embedding-based extraction, just handling the fuzzy half. And place_trade_order gains a reason argument, which the agent’s instructions now tell it to always pass: the ticker, its sentiment score, and a one-line news summary. That rationale rides along into the structured memory, so a future session can recall not just what we traded, but why.
The full diff to add this is as follows.
Now every trading run leaves behind two kinds of memory: the fuzzy transcript for the model to draw on, and a precise, structured record of exactly what we traded and why. Start a fresh session, ask “what did you recently trade?”, and it’ll answer from that record reliably — not just when the embedding model happened to hold on to it.
The tradeoff is that you’re now writing the memories that matter by hand. It’s a little more code, but for the things you can’t afford to lose — trades, decisions, anything you’ll want to audit later — deterministic beats best-effort.