🇨🇳 中文

OpenClaw Memory Strategy: Tool-Driven RAG and On-Demand Recall

How OpenClaw implements agent memory using tool-driven RAG instead of prompt injection — combining BM25 and vector search, chunk-based indexing, and incremental session indexing for scalable recall.

Bruce

OpenClawRAGVector SearchAI AgentMemory System

AI Guides

812  Words

2026-01-31


OpenClaw memory strategy overview

Why Memory Matters for AI Agents

Modern AI agents need more than conversation ability. Users expect agents to:

  • Remember who you are — your preferences, project context, and past interactions
  • Use that knowledge when it matters — without stuffing everything into the context window every turn

Many chatbot platforms solve this by maintaining a long-term user profile and injecting it into the system prompt on every request. It works, but it wastes tokens and scales poorly.

OpenClaw takes a fundamentally different approach: memory is not automatically injected — it is retrieved on demand through tool calls. The agent decides when to recall, what to search for, and how much to retrieve.

This article breaks down how OpenClaw’s memory strategy works and what it means for anyone building agent systems.

Core Design: Memory as Tool Calls, Not Prompt Injection

OpenClaw’s memory retrieval relies on two primary tools:

  • memory_search — semantic search combining BM25 and vector similarity
  • memory_get — precise retrieval of text chunks by file path and line range

The key insight:

Memory is not “auto-injected every turn.” It is searched only when needed, and only the relevant fragments are retrieved.

This delivers two immediate benefits:

  1. Token efficiency — irrelevant history never enters the context window
  2. Agent autonomy — the decision of when and what to recall becomes part of the agent’s reasoning process

Indexing Strategy: Beyond Chat History

One easily overlooked but critical detail: OpenClaw indexes not just conversation history, but also workspace-generated documents.

Any .md files the agent produces — notes, summaries, SOPs, articles — are chunked and added to the memory index.

Think of it like a competent colleague. When you ask “how did we handle this last time?”, they don’t scroll through old chat messages. They open the document they wrote about it.

This means the memory system captures:

  • Raw conversation data
  • Agent-generated structured outputs
  • Any markdown artifacts in the workspace

The implementation follows a clean, production-friendly RAG data layer:

  • chunks — stores text content with metadata (file path, line range, source)
  • chunks_vec — stores embedding vectors
  • chunks_fts — stores full-text search (FTS) indexes

This separation provides three critical properties:

  • Explainability — search results trace back to specific files and line numbers
  • Tunability — BM25/FTS and vector recall can be mixed, weighted, and reranked independently
  • Auditability — when something goes wrong, you can inspect exactly what was indexed

Incremental Session Indexing

OpenClaw stores session data as .jsonl files and performs incremental chunking:

  • Monitor JSONL files for changes
  • When new content crosses a threshold, read the delta and index it

An interesting design choice here: many systems use history compression (like /compact to generate summaries) that replaces or deletes old messages. OpenClaw keeps both the original session data and any compressed summaries as indexable sources.

The philosophy behind this:

  • Memory works more like log retrieval than a curated persona
  • Summaries are a compression tool, but they don’t need to be injected into every context window

What This Means for Agent System Design

The most important takeaway from OpenClaw’s approach:

Memory chunks are never automatically concatenated into the system prompt or per-turn context. Everything is on-demand and tool-driven. This is what it truly means to hand control to the agent.

Consider the analogy:

  • Auto-injection is like someone reading your entire resume aloud before every sentence you speak
  • Tool-driven retrieval is like reaching for your notebook, searching your notes, or checking a document when you actually need it

The second approach mirrors how humans work and scales far better in production.

Practical Recommendations for Your Own Agent

If you are building a similar system, consider these patterns:

  1. Anchor long-term preferences in explicit files — use a Profile or MEMORY.md that the agent can reliably reference
  2. Index agent outputs, not just conversations — documents the agent produces are often more valuable than raw chat logs
  3. Teach the agent a retrieval workflow:
    • First, decide whether recall is needed for this task
    • Then, determine what keywords or concepts to search
    • Finally, retrieve only the necessary fragments

Summary

OpenClaw’s memory strategy is defined by three principles: minimal, engineered, and agent-controlled.

  • Memory is not a magic prompt — it is a searchable knowledge base
  • The system does not chase automatic injection — it prioritizes retrievability and explainability

If you are building your own agent system, this tool-driven approach to memory is well worth adopting.


References

Comments

Join the discussion — requires a GitHub account