adk-memory
Semantic memory and search for Rust Agent Development Kit (ADK-Rust) agents.
Stability: Stable — follows semantic versioning; the
redis-memory,mongodb-memoryandneo4j-memorybackends are Alpha. See STABILITY.md.
Overview
adk-memory provides long-term memory capabilities for the Rust Agent Development Kit (ADK-Rust):
- InMemoryMemoryService - Simple in-memory memory storage
- SqliteMemoryService - SQLite-backed persistence (
sqlite-memoryfeature) - PostgresMemoryService - PostgreSQL + pgvector persistence (
database-memoryfeature) - MongoMemoryService - MongoDB-backed persistence (
mongodb-memoryfeature) - Neo4jMemoryService - Neo4j-backed persistence (
neo4j-memoryfeature) - RedisMemoryService - Redis-backed persistence (
redis-memoryfeature) - VertexAiMemoryBankService - Vertex AI Agent Engine Memory Bank (
vertex-memoryfeature) - GraphMemoryService - Bi-temporal knowledge graph of entities, relations, and observations on SQLite (
graph-memoryfeature) - MemoryService - Trait for custom storage backends
- Search - Keyword search on the in-memory, SQLite, and Redis backends; vector similarity with an
EmbeddingProvideron PostgreSQL (pgvector, with atsvectorkeyword fallback), MongoDB (Atlas Vector Search, with a text-search fallback), and Neo4j - Project-Scoped Isolation - Isolate memories by project within a user
- Schema Migrations - Versioned, forward-only migrations for the SQL, MongoDB, and Neo4j backends
Agents reach memory through the runner, which attaches a memory service to each invocation (ToolContext::search_memory), and through adk-tool's memory-tools feature: LoadMemoryTool, and PreloadMemoryTool, which also converts into a BeforeModelCallback that injects memories at the start of a turn.
Installation
[]
= "3.0.0"
Or use the meta-crate:
[]
= { = "3.0.0", = ["memory"] }
Quick Start
The examples use these dependencies:
[]
= "3.0.0"
= "3.0.0"
= "0.4"
= { = "1", = ["macros", "rt-multi-thread"] }
use Content;
use ;
use Utc;
async
The in-memory and Redis backends split text into runs of Unicode letters and digits, score each entry by the share of query words it contains (min_score filters on that score), and return the highest-scoring, then newest, entries.
Project-Scoped Memory
Memories can be scoped to a project within a user. The isolation key is (app_name, user_id, project_id?):
- Global entries (
project_id = None): visible in all project contexts and in global-only searches. - Project entries (
project_id = Some(id)): visible only when searching within that specific project. - Project search (
project_id = Some(id)): returns global entries + entries for that project. - Global search (
project_id = None): returns only global entries.
Writing project-scoped entries
use Content;
use ;
use Utc;
async
Searching with project scope
use ;
async
Project-scoped deletion
use MemoryService;
async
MemoryServiceAdapter with project scope
use Arc;
use ;
use ;
async
Project ID validation
use validate_project_id;
Feature Flags
| Feature | Backend | Description |
|---|---|---|
sqlite-memory |
SQLite | Single-node persistence via sqlx |
database-memory |
PostgreSQL | pgvector-backed semantic search |
redis-memory |
Redis | Low-latency in-memory persistence via fred |
mongodb-memory |
MongoDB | Document-oriented persistence |
neo4j-memory |
Neo4j | Graph database persistence |
vertex-memory |
Vertex AI Memory Bank | Managed memory generation and retrieval on the Gemini Enterprise Agent Platform |
graph-memory |
SQLite | GraphMemoryService knowledge graph (implies sqlite-memory) |
embedding-trait |
— | The EmbeddingProvider trait without a vector backend |
conformance |
— | adk_memory::conformance and memory_conformance_tests!, the id and expiry suite for a backend's tests |
# SQLite
= { = "3.0.0", = ["sqlite-memory"] }
# PostgreSQL + pgvector
= { = "3.0.0", = ["database-memory"] }
Backend Notes
| Backend | Behaviour |
|---|---|
| SQLite | Search matches entries containing every query word; FTS5 syntax in the query is matched literally. |
| Redis | Key segments are percent-encoded (% → %25, : → %3A); identifiers without either character keep their keys. delete_user deletes only the named user's keys. |
| Neo4j | Entry ids are scoped by app, user, project and session; add_session merges on them, so re-ingesting a session updates it. Vector search over-fetches candidates before filtering to the tenant — see Neo4jMemoryService::with_vector_candidate_cap. |
Artifact References
A memory can refer to an artifact without holding its bytes: MemoryEntry::with_artifact(uri, mime_type) adds a Part::FileData after the text, and MemoryEntry::artifact_references()
returns the references. Search matches the text alone (text::extract_text is unchanged).
Every backend that stores the entry's content keeps the references; the knowledge graph's
episodic log keeps them in a column of its own (migration 3); Vertex AI Memory Bank stores
extracted facts as text and keeps none.
Expiry and Deletion
Every backend stores an id and an expiry with each entry, never returns an expired entry from
search or list_recent, and implements delete_memory and purge_expired. The rule lives in
adk_memory::expiry; the conformance suite holds each backend to it.
| API | Behaviour |
|---|---|
MemoryEntry::with_ttl, with_expires_at |
The entry's own expiry |
with_default_ttl on each service (RedisMemoryConfig::ttl for Redis) |
The expiry of an entry written without one, measured from the write |
MemoryService::delete_memory(app, user, id) |
Removes the entry with that id, global or in any project; returns how many it removed |
MemoryService::purge_expired() |
Removes every expired entry; Redis keys, the MongoDB TTL index, and Memory Bank also expire entries natively |
expiry::spawn_expiry_sweep(service, every) |
Runs purge_expired on an interval |
use Content;
use ;
use Duration;
async
Schema Migrations
The SQLite, PostgreSQL, MongoDB, and Neo4j backends include a versioned migration system. Migrations are forward-only, idempotent, and tracked in a registry table. schema_version() returns 0 only when the registry table does not exist; a failed read is an error. PostgreSQL migrate() holds the advisory lock PostgresMemoryService::ADVISORY_LOCK_KEY on one connection. SQLite runs each step in a BEGIN IMMEDIATE transaction that re-reads the registry, and MongoDB and Neo4j create indexes and constraints idempotently and record each version once, with an upsert or MERGE, so several processes can migrate one database at once.
The example below needs the sqlite-memory feature.
| Version | Description |
|---|---|
| v1 | Initial schema (tables, indexes, FTS) |
| v2 | Add project_id column/index for project-scoped memory |
| v3 | Add memory_id and expires_at (MongoDB: a TTL index on expires_at); existing entries get ids |
use SqliteMemoryService;
async
MemoryService Trait
| Method | Default |
|---|---|
add_session(app, user, session, entries) |
required |
search(SearchRequest) |
required |
delete_user, delete_session, add_entry, delete_entries, list_recent |
Return a "not implemented" error; backends override them |
delete_memory(app, user, id), purge_expired() |
Return an error; every backend in this crate implements them |
export_user(app, user) |
Returns subject.unsupported; every backend in this crate lists the user's entries, global and project-scoped |
health_check() |
Ok(()) |
supports_project_scoping() |
false |
add_session_to_project, add_entry_to_project, delete_entries_in_project, delete_project |
Return an error |
The project methods refuse instead of falling back to global writes or deletes, which would cross the project boundary. Every backend in this crate except VertexAiMemoryBankService and GraphMemoryService implements them and returns true from supports_project_scoping().
Subject Export and Erasure
MemorySubjectData adapts any MemoryService to adk_core::subject::SubjectDataStore: it
exports a subject's entries through export_user and erases them through delete_user, for
each of the subject's user ids in each covered application. Every backend passes
adk_core::subject_data_store_conformance_tests!.
use Arc;
use ;
use ;
let rights = new
.with_app
.with_store;
Consent-Gated Memory
ConsentGatedMemoryService wraps any MemoryService with an adk_core::subject::ConsentGate:
for a gated user (a visitor id by default) without consent for persistent_profile, adds are
skipped and search returns nothing; deletes and exports always pass through. See
Subject Rights.
Related Crates
- adk-rust - Meta-crate with all components
- adk-core - Core
Memorytrait - adk-runner - Attaches the memory service to each invocation
- adk-tool -
LoadMemoryToolandPreloadMemoryTool(memory-toolsfeature) - adk-rag - RAG pipeline with vector stores
Changes in 3.0
| Change | Migration |
|---|---|
SQL backends use SQLx 0.9; sqlx and pgvector are no longer implicit features, and pgvector moves to 0.4.2 |
Depend on SQLx 0.9 when sharing a pool; enable sqlite-memory or database-memory |
adk-gcp, fred, google-cloud-auth, and reqwest are no longer implicit features |
Enable redis-memory or vertex-memory |
schema_version() returns read failures as errors |
— |
| Keyword search matches through punctuation and returns the best matches first | — |
PostgreSQL migrate() releases its advisory lock |
— |
MemoryEntry gains id and expires_at and is #[non_exhaustive] |
Build it with MemoryEntry::new and its with_* methods |
MemoryService gains delete_memory and purge_expired |
A wrapper forwards both |
RedisMemoryConfig::ttl is each entry's default TTL, not a session list's |
— |
See the 2.x to 3.0 migration guide and the changelog for the full list.
SQLx compatibility
ADK-Rust 3.0 SQL backends use SQLx 0.9. SQLx types appear in this crate's public
API — from_pool on SqliteMemoryService, GraphMemoryService and PostgresMemoryService, and the migration runners — so an application that passes in its own pool must depend on
SQLx 0.9 too. Applications that let ADK open its own connections need no SQLx
dependency. The SQLx 0.9 migration guide
lists who is affected and what changed, and its
TLS behaviour section
covers PostgreSQL certificate verification.
License
Apache-2.0
Part of ADK-Rust
This crate is part of the ADK-Rust framework for building AI agents in Rust.