Skip to main content
This guide runs the complete memory loop end to end: recall context before a turn, stream the agent response, persist the turn, and optionally extract a durable fact. Run this cycle on every turn and your agent builds continuous memory across sessions.

1. Recall context before the turn

Ask the memory system for context relevant to the user’s next message. You get back a prompt string ready to inject.
The context string contains ranked, deduplicated items from past turns and stored facts — packed to a character budget so it’s ready for the system prompt.

2. Stream the agent response

Run the turn as normal. The context you recalled is already available to the agent through its runtime memory; for custom integrations you can also inject it into your own prompt assembly.

3. Persist the turn

Record the exchange so the next recall can draw on it. This is fire-and-forget — it never blocks the response you already streamed.

4. Optionally extract a durable fact

If the turn revealed something worth remembering permanently — a preference, an identity, a long-lived state — store it explicitly. Pinned facts survive across threads and sessions.

Why this creates continuous memory

Each step feeds the next: After a few turns, the agent recalls prior decisions without being told again. Across a new thread, durable facts still surface — so the agent remembers who the user is and what they care about even months later.

Running the loop in one call

If you want the SDK to orchestrate all three steps, use sdk.memory.loop():

Next

  • Memory — the three-step loop explained
  • Recall — context assembly in detail
  • Facts — durable facts across sessions