Connect your existing database
Createlifecycle/memory.py in your agent folder. Both database providers ship with the managed Harnest runtime.
Harnest owns startup and shutdown. The memory factory is optional; remove it when unused. For shared configuration or direct Python driver installation, see Task and cron storage.
Write deliberately from a tool
Save a preference only when the user asks:tools/remember_report_preference.py
context.memory is scoped to the current application and authenticated user, including calls from SubAgents. Replicas share an application identity; independent agents need distinct identities. Use namespaces to organize one user’s memories:
Read, search, update, and forget
MemoryContext supports explicit writes, reads, bounded listing, search, and deletion. See the operation reference for signatures and return values. All operations are async. Search is literal text matching, not semantic search. Treat retrieved content as untrusted data.next_cursor is None, even after an empty page. Concurrent edits can change later pages.
Use a revision to avoid overwriting a concurrent update:
harnest.memory.MemoryConflictError for stale revisions. Without expected_revision, put() replaces the current record unconditionally.
Retention and limits
Setexpires_at to hide expired memories, then purge them for physical deletion. Session deletion does not erase memory. See record and pagination limits.
Use a separate database for evaluations that write memories.
Run trusted storage maintenance
These Python provider methods are for trusted application code—not built-in HTTP endpoints or generated Agent Tools. Any route you add must enforce authentication and authorization. Use MemoryStore.delete_all for one namespace or delete_user for account erasure. Use purge_expired for bounded cleanup sweeps. Sweep expired records using your configured provider:Handle connection failures
MemoryStorageError can mean the database committed but its acknowledgement was lost. Read the key again before retrying, and use revision checks to avoid overwriting a later update. Harnest does not automatically replay Redis writes after a lost acknowledgement.
Implement a custom provider
Implement the MemoryStore contract and its associated record, scope, and error types. Register your provider with@lifecycle.storage.memory. Follow MemoryStore for isolation, atomic revisions, and detached results; subclassing is optional. Keep explicit MemoryScope values in trusted code.
Run the bundled conformance suite against an isolated real database:
API reference
Seeharnest_postgres, harnest_redis for signatures, options, and return values.