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
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
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.
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
Import the async contract and result types fromharnest.memory:
start() and close(), then register your provider with @lifecycle.storage.memory. Subclassing is optional; follow MemoryStore’s signatures and guarantees for datastore-side isolation, atomic revision checks, and detached results. Explicit MemoryScope values belong in trusted code, never model-supplied arguments.
Run the bundled conformance suite against an isolated real database: