Agent long-term memory on Upstash Redis Search: idempotent index, per-turn recall
Build the long-term memory index once with an idempotent create, then query it per turn. Index creation is setup, not request path.
TL;DR: Build the long-term memory index once with an idempotent create, then query it per turn. Index creation is setup, not request path. Create the index once This is a setup script, not request code. Write facts On each turn, extract durable facts (preferences, decisions, events) and store them as JSON under memory:FACTID with the userId, kind, text, and createdAt fields the schema expects.
The fix
Agent long-term memory on Upstash Redis Search
1. Create the index once
This is a setup script, not request code. Make it idempotent so re-runs are safe:
from upstashredis import Redis redis = Redis.fromenv() redis.search.createindex( name="memories", datatype="json", prefixes="memory:", exists_ok=True, schema={ "text": "TEXT", "userId": "KEYWORD", "kind": "KEYWORD", "createdAt": "F64", }, )
Schema choices: text is full-text searchable, userId and kind are exact-match keywords for scoping, createdAt is a sortable number for recency.
2. Write facts
On each turn, extract durable facts (preferences, decisions, events) and store them as JSON under memory:FACTID with the userId, kind, text, and createdAt fields the schema expects.
3. Recall per turn
Query the index scoped to the user, sorted by recency, and feed the top hits into the prompt alongside the working-memory chat history. Working memory is the separate TTL-key pattern: one key per session, one-hour expiry.
Notes
- Upstash Search indexes JSON, hash, string, and stream keys via
its own SEARCH commands, separate from RediSearch FT commands.
- Keep the index name stable across deploys: the SDK command
references cover typed helpers, but the index itself persists in the database.
Verify
Store a test fact, query it back scoped to the right user and wrong user (wrong user must not see it), and re-run setup to confirm idempotency.
When to use this
- This covers exactly what the title says: Agent long-term memory on Upstash Redis Search.
- You are setting this up for the first time, or auditing an existing setup.
- You want the key gotchas in one place before you start.
When not to use this
- You are doing a different workflow with Agent; these steps are specific to the title above.
- You need the full reference docs; this is the short path, not the manual.
Compatibility
- Not pinned to a specific version; follows current Agent behavior.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.