Skip to main content
Long-term memory persists important information across sessions and over time — user preferences, task history, key facts, or long-lived state. Short-term memory restores a session history, while long-term memory retrieves relevant content by query; long-term memory lets an agent remember facts across different sessions. Why you need it:
  • Continuous conversation experience across sessions;
  • Retain learnings and user-specific information over many interactions;
  • Avoid repetitive questions, improving satisfaction and efficiency;
  • Support long-term strategy optimization such as personalization or task tracking.

Single entry point: LongTermMemory

Whatever backend you use, you interact with veadk.memory.long_term_memory.LongTermMemory. It plugs directly into an agent as its memory service and selects the storage backend through backend.

Parameters

Vector backends (local, opensearch, and redis) embed memories locally and require the extensions extra and an embedding model. viking, mem0, openviking, and tos_context use external services and do not need local embedding.

Choosing a backend

Use local for debugging. In production, select viking, mem0, or tos_context according to your existing services and data-governance requirements.

Binding to an Agent

Passing long_term_memory to an Agent auto-injects the load_memory tool so the agent can retrieve past sessions at run time.

Managing memory

Write: add_session_to_memory

When a session ends or hits a checkpoint, call the async add_session_to_memory to persist it. LongTermMemory filters events according to the auto-save policy — by default keeping only user text events to improve retrieval quality. You can customize the filtering rules with the auto_save_memory_policy parameter before passing events to the backend.
add_session_to_memory accepts an optional auto_save_memory_policy keyword argument to override the default filtering policy:

Retrieve: search_memory

Besides the agent’s automatic retrieval via load_memory, you can call the async search_memory directly for semantic search — useful for debugging or custom RAG:
get_user_profile(user_id) is supported only by the viking backend; others return an empty string.

Auto-save sessions

Set auto_save_session=True on the Agent with long-term memory configured, and VeADK persists sessions automatically — no manual add_session_to_memory.
Auto-save checks thresholds in the end-of-run callback; VeADK exposes MIN_MESSAGES_THRESHOLD and MIN_TIME_THRESHOLD env vars to tune the save cadence: by default it saves after 10 accumulated events or a 60-second interval; additionally, when you switch session_id and start a new turn, VeADK saves the previous session to long-term memory. Auto-save uses incremental writes: each save only persists the events that are new since the last save, rather than the entire session. If there are no new events between two saves, the write is skipped.

Controlling what gets saved: auto_save_memory_policy

The Agent accepts an auto_save_memory_policy parameter that controls which events are written to long-term memory during auto-save. The same value is passed as the auto_save_memory_policy keyword argument to add_session_to_memory. The parameter type is MemoryAutoSavePolicyInput, which accepts one of:
  • A string preset: "default", "all", or "custom";
  • A MemoryAutoSavePolicy instance;
  • A dict with the same fields as MemoryAutoSavePolicy;
  • None (equivalent to "default").
Using a preset string:
Using a MemoryAutoSavePolicy instance for fine-grained control:
Full fields of MemoryAutoSavePolicy: MemoryEventType includes the following event types: text, thought, function_call, function_response, tool_call, tool_response, media, executable_code, code_execution_result, transcription, error.
String presets provide a quick configuration shortcut. When you pass a MemoryAutoSavePolicy instance or dict, the policy starts from the base preset indicated by preset; only the fields you explicitly set override the preset, while all other fields keep the preset’s default behavior.

Cross-session example

First complete model configuration and local memory embedding configuration, then run this end-to-end flow: session #1 tells the agent a fact and auto-archives it, then a brand-new session #2 asks a question — and the agent recalls the fact via memory retrieval (not the context window). This uses the local backend, which needs pip install "veadk-python[extensions]".
In session #2 the agent recognizes the same user’s preferences left in session #1 and answers coherently and personally (e.g. a peanut-free vegetarian dish).

Isolation and save boundaries

Long-term memory does not guarantee permanent storage: local keeps data in one instance and does not filter retrieval by user. Mem0 does not use index as a service-side isolation key; OpenViking requires explicit owner/context and peer mapping. Follow each backend page to establish application and user isolation The time threshold is not a background timer and does not guarantee a final save before process exit. Explicitly save and verify retrieval for information that must be retained. An empty search_memory() result can also indicate a backend error. Repeatedly saving an entire session manually can duplicate writes; auto-save incremental behavior does not apply to arbitrary manual calls The memory-management snippets belong inside the same async workflow: define APP_NAME, USER_ID, and SESSION_ID, and verify a nonempty Session before saving. The cross-session example provides a complete entry point. The all policy can send thoughts, tool inputs and outputs, and media information to storage; establish retention requirements before enabling it
Last modified on September 19, 2026