Skip to main content
Remember user preferences across sessions. Your Agent Tools decide what to save and when to retrieve it; Harnest does not extract memories or add them to prompts automatically.

Connect your existing database

Create lifecycle/memory.py in your agent folder. Both database providers ship with the managed Harnest runtime.
Supply the connection URL through your environment or deployment secrets. From your agent folder:
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:
Await operations within the invocation; handles cannot be retained for later use.

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.
Continue until next_cursor is None, even after an empty page. Concurrent edits can change later pages. Use a revision to avoid overwriting a concurrent update:
Catch harnest.memory.MemoryConflictError for stale revisions. Without expected_revision, put() replaces the current record unconditionally.

Retention and limits

Set expires_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:
Schedule repeated sweeps with your maintenance worker or Harnest cron. Before account erasure, authorize the owner and revoke writes to prevent new memories. Redis deletes namespaces in batches; retry after a partial failure. Deletion does not erase backups, database logs, transcripts, or previously returned copies.

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:
The mixin uses a unique application scope per test. Add test-data cleanup and provider-specific crash, rollback, and connection-loss checks; also verify your deployment’s persistence settings.

API reference

See harnest_postgres, harnest_redis for signatures, options, and return values.