Skip to content

Module 5: Agent Memory


What you're building

Without memory, every conversation starts from zero. Your agent has no idea who it's talking to or what was discussed before.

Agent Memory gives your agent two types of memory:

Memory type What it stores Lifetime
Session Every message in the current conversation Expires when the session ends
Long-term User preferences and facts Persists across conversations

When someone asks "What kind of appointment should I schedule?", the agent searches long-term memory by meaning and recalls "prefers morning appointments" and "primary care provider is Dr. Sofia Martinez" to give a personalized answer.


Cloud setup

1. Create an Agent Memory store

  1. In the Redis Cloud console, find Agent Memory in the left sidebar.
  2. Click Quick Create.

    Agent Memory Quick Create

2. Save your API key

Copy the API key immediately. You won't see it again.

Agent Memory service key

Save it in your .env file:

MEMORY_API_KEY=<your-api-key>

3. Save your Endpoint and Store ID

Find the remaining credentials on the store details page:

Agent Memory credentials

Save them in your .env file:

MEMORY_API_BASE_URL=<endpoint>
MEMORY_STORE_ID=<store-id>

4. Seed long-term memories

make seed-memories
.\workshop.ps1 seed-memories

This seeds two facts for the demo patient: "Prefers morning appointments when available" and "Primary care provider is Dr. Sofia Martinez at Downtown Medical."


Exercise

  1. Open exercises/healthcare/agent_memory.py in your IDE.
  2. Replace return None in the long_term_search_payload method with:
return {
    "text": text,
    "similarityThreshold": 0.2,
    "filterOp": "all",
    "limit": limit or 5,
    "filter": {
        "ownerId": {"eq": sanitize_owner_id(owner_id)},
        "namespace": {"eq": "healthcare-demo"},
    },
}

The key field here is similarityThreshold. It controls how closely a memory needs to match the current question. Lower means the agent recalls more loosely related memories. Higher means it only recalls very direct matches.


Verify

Stop the running server (Ctrl+C) and restart:

make dev
.\workshop.ps1 dev

Open localhost:3040.

  1. Ask: "Based on what you know about my preferences, what kind of appointment should I schedule?"
  2. Click Agent-memory in the right activity panel. The agent recalled "morning appointments" and "Dr. Sofia Martinez" from long-term memory to personalize its answer.
  3. Ask a follow-up: "Can you check my referrals?". The agent remembers what you just discussed. That's session memory keeping the conversation coherent.
  4. Now try changing similarityThreshold to 0.8, restart, and ask the same question. Notice how a stricter threshold changes what the agent remembers.