跳到正文
DEV Community· Priyesh Dave·· 13 天前AI 評分45

開源 AI 智能體的實戰記憶模式:向量、圖與 SQL 方案比較

Agentic Memory Patterns in the Wild: Vector, Graph, and SQL Approaches from Recent Open-Source Systems

AI 導讀

開源 AI 智能體的記憶架構並非可互換;文章比較向量、SQL 與圖記憶在可追溯性、效能及營運成本上的差異。10k-row 資料庫測試中,向量搜尋每次需 10–40ms,SQLite FTS5 每次需 20–110ms,LIKE 後備搜尋則需 150–650ms。向量記憶速度快但難以追溯,SQL 記憶較易稽核但寫入較慢,圖記憶能明確呈現關係,代價是建模和維運工作。

正文

Agentic Memory Patterns in the Wild: Vector, Graph, and SQL Approaches from Recent Open-Source Systems

Open-source AI agents break without auditable, resilient memory. Most “agent frameworks” claim to address this, but actual open implementations diverge in key ways. Agentic memory is nowhere near generic cache—with embedding, context refetch, audit surface, and latent failure modes baked in. Below is what happens in practice: vector, SQL, and graph each break for specific, predictable reasons.

Memory Architecture Locks You In

Agent loops aren’t memory-agnostic. Every choice—vector, SQL, or graph—creates tradeoffs that surface in API quirks and catastrophic failures. Control loop code from https://github.com/tinyhumansai/tinymemory and https://github.com/delicious-roundarch520/openclaw-memory-kit shows: memory isn’t pluggable. Backend coupling governs how you propagate errors, batch queries, and chunk user context.

Example from TinyMemory:

from tinymemory import MemoryStore

memory = MemoryStore(persist_dir="your_data")
agent = Agent(memory=memory)

OpenClaw’s SQL memory (source):

from sql_memory import SQLMemory

memory = SQLMemory(sqlite_path="data/memory.sqlite")
agent = Agent(memory=memory)

The agent loop must handle batch embedding (vector) vs. transaction and race issues (SQL). Query batching, windowing, and failure reporting—none are abstracted from memory backend.

Vector Stores: Fast Embeddings, Zero Traceability

Works until you need to audit the source of an answer.

Vector store memory (TinyMemory, Butter) offers constant-time embedding lookups, fuzzy retrieval, and easy scaling. Integration is trivial:

from tinymemory import MemoryStore

memory = MemoryStore(persist_dir="memdb")
observation = {
    "text": "User asked about Q3 revenue targets.",
    "tags": ["finance", "Q3"],
}
memory.add(observation)
res = memory.search("What were last quarter's sales targets?", top_k=2)
for r in res:
    print(r['text'])

Retrieval is fuzzy and unauditable.

A rephrased query can yield surprising or irrelevant matches. There’s no join, no trace, and no way to map answers back to specific context. Try tracing an LLM mistake to the original input—vector stores are black boxes.

Index corruption is silent.

Check TinyMemory’s persist: no transaction log, no locking. A crash mid-write yields a partial, undetectably corrupt index. Butter is similar—fast by default, frail during edge cases.

Benchmark: Local 10k-row db, MemoryStore.search: 10–40ms per query, scaling linearly. For small projects: fast and simple. If you need traceability, look elsewhere.

SQL Memory: Slow, But Auditable

SQL backends (UltraContext, OpenClaw) are slower but make every memory transaction transparent. UltraContext and OpenClaw expose full logs and joins, not just what answer, but why.

Example (based on OpenClaw-memory-kit):

from sql_memory import SQLMemory

memory = SQLMemory(sqlite_path="data/memory.sqlite")
memory.add({
    "text": "CEO confirmed Q4 layoffs.",
    "source": "transcript_2024_03_10.txt",
    "timestamp": "2024-03-10T12:17:00"
})
results = memory.search("layoffs", limit=5)
for r in results:
    print(r["text"], r["source"])

Advantages: SQL is natively auditable.

Transactions, rollbacks, FTS indices, and permissioning come for free. You can run SELECT * FROM observations WHERE ... and reconstruct provenance.

Performance: 10k-row SQLite with FTS5: 20–110ms per query. LIKE-based fallback: 150–650ms. Overhead is linear; no built-in replication.

Failure:

  • Serialization bottlenecks: Frequent writes block the agent loop. OpenClaw has no default queue.
  • Schema drift: Add new keys and old queries fail without explicit migrations.

Every fact is traceable, but speed suffers.

Graph Memory: Relations, at Operational Cost

Graph memory is the “explainability” darling, but shifting from event logs or embeddings to subject-predicate-object isn’t free. It exposes relationship structure—for audit and queries—at the cost of data modeling and operational pain.

Sample pattern using networkx (inline, not pseudocode):

import networkx as nx

class GraphMemory:
    def __init__(self):
        self.graph = nx.DiGraph()
    def add_observation(self, entity, attr, value):
        self.graph.add_edge(entity, attr, value=value)
    def query(self, entity):
        return list(self.graph[entity].items())

memory = GraphMemory()
memory.add_observation("CEO", "announced", "Q4 layoffs")
memory.add_observation("Q4 layoffs", "affects", "Engineering")

result = memory.query("CEO")
print(result)  # [('announced', {'value': 'Q4 layoffs'})]

Problems:

  • Few agents natively output data ready for S-P-O ingestion.
  • Edge duplication, cycles, and graph traversals take real engineering effort.
  • Querying is expressive but unfamiliar—without Cypher, you’ll hand-roll depth limits and anti-cycles.

Upsides:

  • Relationship queries are explicit and maintainable—“trace all downstream consequences of X” is straightforward.
  • Traversals yield audit chains impossible in vanilla vector or basic SQL.

Architectural Integration: Where the Memory Hooks Bite

Vector Store

[Prompt] → [Embedding Query] → [Top-K Search] → [Prompt Augmented] → [LLM]
                   |
           (Opaque match, no join trace, silent failures)

Plug-and-play integration. Failures are silent and fuzzy—no recovery path except trial/error.

SQL

[Prompt] → [SQL Query] → [Selected Rows] → [Prompt Augmented] → [LLM]
                   |
           (Traceable, schema-bound, exceptions available)

You pay in sync logic and schema migrations, but failures are explicit and auditable.

Graph

[Prompt] → [Graph Traverse] → [Nodes/Edges] → [Prompt Augmented] → [LLM]
                   |
           (Rich context, expensive queries, model complexity)

Relationship expressivity wins, but debugging cycles or bad traversals is non-trivial.

Production Tradeoffs: When Each Fails

  • Vector: Fast for prototyping, irrelevant for auditing. Silent mismatches and opaque origin.
  • SQL: Mandatory for “where did you get that?” moments. High write or schema churn will break you unless you build queuing or async layers. Latency penalty increases linearly.
  • Graph: Only worthwhile if relations—not just facts—matter. Brings migration and cyclicity headaches, but explainability is unmatched if you can afford the complexity.

Bottom line:

Agentic memory isn’t one hot-swappable line. Everything you care about—latency, audit, recovery, explainability—traces back to this backend choice.

Annotated Benchmarks (local SSD, Python 3.10, no RAMdisk):

Backend Query Time (10k rows) Traceability Failure Reporting
Vector/TinyMemory 10–40ms ❌ Silent/Fuzzy
SQL/OpenClaw 20–110ms (FTS5) ✅ Exception, rollback
Graph (networkx) 30–500ms* ✅ Traversal/cycle

*Graph: varies by topology.

Real agent reliability lives and dies on memory transparency. Choose audit trails (SQL), fast hacks (vector), or explainability (graph), but know what breaks first. The abstraction is a lie; architecture is destiny.

來源:DEV Community · dev.to