Building an agent memory layer on RedDB
Store and retrieve a small agent memory record with native RedDB SQL, and plan for retention, access control, and safe updates.
On this page
Memory is application data
An agent can save notes outside its context window and load the relevant ones in a later session. A useful design begins with an owner, a type, a value, and a clear rule for what may be retrieved. The database stores records; deciding what to remember and when to forget remains an application responsibility.
Try the examples locally
Start RedDB with its HTTP endpoint bound to your local machine:
red server --http-bind 127.0.0.1:5000 --path ./example.rdb --dev--dev disables authentication and is only for a trusted local environment. In a second terminal, define a helper to send a query. It requires curl and jq:
query() {
curl -fsS http://127.0.0.1:5000/query \
-H 'content-type: application/json' \
--data "$(jq -cn --arg query "$1" '{query: $query}')"
}The examples below were executed against RedDB 1.26.8. Use a fresh database for this walkthrough; reusing names from another example can change the results.
Store a memory
query 'CREATE TABLE memories (id TEXT, agent_id TEXT, memory_type TEXT, content TEXT)'
query "INSERT INTO memories (id, agent_id, memory_type, content) VALUES ('memory-1', 'agent-1', 'note', 'Customer prefers email')"
query "SELECT id, memory_type, content FROM memories WHERE agent_id = 'agent-1'"The result includes the saved memory-1 record. Here memory_type is an application field; kind is a reserved RedDB system field, so this example does not use it as a user-defined column.
Decide what to retrieve
The application supplies the agent ID and selects the records appropriate for a request. A production integration should use parameterized queries rather than interpolating user input into SQL. Scope every read to the authorized tenant and user; an agent ID by itself is not an authorization boundary.
This example demonstrates exact filtering. For semantic retrieval, see the separately tested vector search example; embedding generation and deciding which memories to attach to a prompt are application choices.
Updates, retention, and recovery
Choose stable application-generated IDs, decide how a new observation supersedes an older one, and test your update and conflict-handling approach under concurrency. Set retention and deletion rules for sensitive content, keep an audit trail where needed, and verify backup and restoration in your deployment. Do not assume an agent can safely retrieve every stored note or that a queue gives it automatic durable memory.
The documents guide and vector guide describe additional data models. Test the exact operations, isolation guarantees, and client version you plan to deploy.
Ready to build with RedDB?
Create an account and explore the Always Free database.
Create your account →