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

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

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:
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

Import the async contract and result types from harnest.memory:
Implement the operations above plus 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:
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.