Memory
Memory system in AgenticX.
Memory in AgenticX
Memory is how an agent keeps identity, recent work, and a bounded window. Near's main path is workspace files (MEMORY.md, memory/*.md) plus session messages.json, then recall injected into the Meta system prompt. Optional Mem0 / other stores are library backends, not the Desktop default.
Worked example: write a preference, see it next turn
Scene. You want Meta to keep calling you a nickname and avoid a formatting habit.
- Put the fact in
~/.agenticx/workspace/MEMORY.mdormemory/*.md(or the user-preference field in Settings — that is injected via_build_user_profile_block()). - Start a new turn in the same Meta session. The system prompt should include the recall block from
_build_memory_recall_context. - Long threads may compact: older history becomes a summary system message. The file-backed facts should still be there even if the transcript was compacted.
What you should see. Workspace panel highlighting MEMORY.md. The next answer uses the nickname without you repeating it. Session title stays the persisted Chinese title, not a raw session_id.

MEMORY.md open. The file holds the preference the next turn can recall.Together these mechanisms approximate a layered memory stack: what must always be true (identity), what happened recently (episodes), what generalizes (semantics), what the model sees this turn (working set), what survives across sessions (long-term files and stores), and what is scoped to the user's workspace on disk.
Memory hierarchy (six-layer model)
The table maps a classic cognitive-style hierarchy to concrete AgenticX surfaces. Layers are conceptual; several features overlap in implementation.
| Layer | Role | AgenticX realization |
|---|---|---|
| Core memory | Stable role, name, non-negotiable facts | IDENTITY.md, SOUL.md, avatar/session system prompts, Meta-Agent identity blocks |
| Episodic memory | What was said and done in conversations | chat_history / persisted messages.json, tool traces in history |
| Semantic memory | Reusable knowledge, not tied to one chat | Curated MEMORY.md, optional Mem0 / knowledge components, indexed chunks in WorkspaceMemoryStore |
| Short-term / working memory | Scratch space for the current task | Per-session scratchpad dict, scratchpad_write / scratchpad_read tools, recent message window |
| Long-term memory | Cross-session persistence | MEMORY.md (manual + auto-append), SQLite session store for scratchpad, optional Mem0 |
| Workspace memory | Project- and user-scoped files under ~/.agenticx/workspace | USER.md, daily memory/<YYYY-MM-DD>.md, directory layout per avatar/project |
MemoryHook
MemoryHook is an AgentHook registered on AgentRuntime with priority=-10, so it runs after hooks with higher numeric priority when run_on_agent_end walks the registry.
When it runs
on_agent_endis invoked after a turn completes.- It reads
session.chat_history. Iflen(chat_history) < MIN_CHAT_TURNS * 2it returns immediately. Defaults:MIN_CHAT_TURNS = 3→ at least six chat records required. - Workspace directory:
session.workspace_dir, elseAGX_WORKSPACE_ROOT, else the user home directory.
Extraction and limits
- Heuristic only (no extra LLM call): scans the last 20 messages for Chinese user-request cues and assistant completion cues, emits bullet lines such as
- 用户请求:/- 完成事项:. - Caps output at
MAX_FACTS_PER_SESSION(8) facts.
Persistence
- Appends a dated block to
memory/<today>.mdunder the workspace. Skips append if existing daily file plus new block would exceed 8000 characters. - If
MEMORY.mdexists and is under 4000 characters, appends an## Auto-extractedsection with up to four of the facts.
Scratchpad
- Merges facts into
session.scratchpad["session_facts"](concatenated with newlines).
_maybe_compact_daily
- If today's
memory/<YYYY-MM-DD>.mdexceeds 2000 characters, rewrites it: keeps headings, drops duplicate bullet lines keyed by the first 80 characters (case-folded), preserving first occurrence. This is deterministic deduplication, not an LLM summarizer.
1# Reference constants (agenticx/runtime/hooks/memory_hook.py)2MIN_CHAT_TURNS = 3 # requires len(chat_history) >= MIN_CHAT_TURNS * 23MAX_FACTS_PER_SESSION = 8
Workspace memory
Directory layout (~/.agenticx/workspace/)
| Path | Purpose |
|---|---|
IDENTITY.md | Who the agent is; stable persona |
USER.md | User profile, preferences |
SOUL.md | Deeper style / values / tone |
MEMORY.md | Long-form durable notes; receives optional auto-extracted sections |
memory/<YYYY-MM-DD>.md | Daily session-fact log from MemoryHook |
Additional session artifacts (avatars, groups) may live alongside; the search index explicitly pulls the four root markdown files plus memory/*.md.
Automatic recall: _build_memory_recall_context
Defined in agenticx/runtime/prompts/meta_agent.py. When building the Meta-Agent system prompt it:
- Collects text from the last five user messages (up to 200 characters each).
- Builds a query string (max 500 characters).
- Calls
WorkspaceMemoryStore.search_sync(..., limit=5, mode="hybrid"). - Injects a
## 相关历史记忆(自动召回)section with snippets capped around 500 characters total.
If there are no user messages or no hits, the section is omitted.
Mem0 integration
Mem0 is optional. Install with the extra that pulls memory dependencies:
1pip install "agenticx[memory]"
At runtime, agenticx.memory exposes Mem0 / async variants when dependencies are present; the vendored integration under agenticx/integrations/mem0/ supports multiple vector stores and configs. Use Mem0 when you want managed long-term semantic memory (add/search APIs, hosted or local) instead of or alongside file-based MEMORY.md.
Session scratchpad
Each StudioSession carries a scratchpad dictionary persisted via SQLite (agenticx/memory/session_store.py). Besides user/agent keys from scratchpad_write, the runtime uses reserved patterns:
| Key pattern | Purpose |
|---|---|
subagent_result::<id> | Summary text after a subagent / delegation completes |
delegation_result::<id> | Same pipeline writes delegation outcomes for Meta-Agent follow-up |
session_facts | Incremental lines from MemoryHook |
__pending_subagent_summaries__ | Queue of pending subagent reports (internal) |
__taskspace_hint__ / __taskspace_label_hint__ | Taskspace path hints from tools |
Meta-Agent context building reads subagent_result::* entries for "historical subagent results" when registry rows are missing.
Context compaction (ContextCompactor)
Before each turn, AgentRuntime runs ContextCompactor.maybe_compact on sanitized agent_messages.
| Parameter | Default | Behavior |
|---|---|---|
threshold_messages | 20 | Compact if message count exceeds this |
threshold_chars | 48000 | Or if sum of content string lengths exceeds this |
retain_recent_messages | 8 | Oldest portion is summarized; these tail messages stay verbatim |
Process:
- Split into prefix (to compact) and suffix (retained).
- Call the runtime LLM with a Chinese compaction instruction (temperature
0,max_tokens400). - Replace the prefix with a single system message prefixed with
[compacted]and the summary. - On failure, fall back to concatenated short snippets.
Minimum enforced floors: threshold_messages >= 8, threshold_chars >= 4000, retain_recent_messages >= 4.
Memory-related configuration
| Item | Where | Description |
|---|---|---|
memory.backend | ~/.agenticx/config.yaml | Declared in docs for pluggable memory backends (sqlite, redis, postgresql, etc.) |
memory.path | ~/.agenticx/config.yaml | Workspace root for memory features |
AGX_WORKSPACE_ROOT | Environment | Fallback workspace directory for MemoryHook when session.workspace_dir is unset |
MIN_CHAT_TURNS | Code constant | MemoryHook gate: needs len(chat_history) >= MIN_CHAT_TURNS * 2 |
MAX_FACTS_PER_SESSION | Code constant | Cap on heuristic facts per on_agent_end |
WorkspaceMemoryStore DB | Default path | ~/.agenticx/memory/main.sqlite (DEFAULT_WORKSPACE_MEMORY_DB) |
ContextCompactor thresholds | Code defaults | 20 messages / 48000 chars trigger; retain 8 messages |
1# ~/.agenticx/config.yaml (illustrative memory block)2memory:3 backend: sqlite4 path: ~/.agenticx/workspace
See also
- Configuration — global
config.yamland environment variables - Architecture — high-level runtime overview