Skip to main content

API Reference

MemoryEngine

The main entry point for all operations.

from openmem import MemoryEngine

engine = MemoryEngine(
db_path=":memory:",
max_hops=2,
decay_per_hop=0.5,
weights=None, # uses defaults
)

See Configuration for parameter details.


add

engine.add(
text: str,
type: str = "fact",
entities: list[str] | None = None,
confidence: float = 1.0,
gist: str | None = None,
) -> Memory

Store a new memory.

ParameterTypeDefaultDescription
textstrrequiredThe memory content
typestr"fact"One of: fact, decision, preference, incident, plan, constraint
entitieslist[str]NoneTagged entities for graph connections
confidencefloat1.0Confidence score (0-1)
giststrNoneOptional short summary (indexed by FTS5)

Returns: A Memory object.

m = engine.add(
"JWT tokens expire after 24 hours",
type="decision",
entities=["JWT", "auth"],
confidence=0.9,
gist="JWT 24h expiry",
)
print(m.id) # uuid hex string

engine.link(
source_id: str,
target_id: str,
rel_type: str = "mentions",
weight: float = 0.5,
) -> Edge

Create a directed edge between two memories. Edges are traversed bidirectionally during spreading activation.

ParameterTypeDefaultDescription
source_idstrrequiredSource memory ID
target_idstrrequiredTarget memory ID
rel_typestr"mentions"One of: mentions, supports, contradicts, depends_on, same_as
weightfloat0.5Edge weight (0-1). Higher = stronger activation spread.

Returns: An Edge object.

edge = engine.link(m1.id, m2.id, "supports", weight=0.8)

recall

engine.recall(
query: str,
top_k: int = 5,
token_budget: int = 2000,
) -> list[ScoredMemory]

Retrieve relevant memories for a query.

ParameterTypeDefaultDescription
querystrrequiredNatural language query
top_kint5Maximum number of results
token_budgetint2000Max estimated tokens across all results

Returns: A list of ScoredMemory objects, sorted by score descending.

results = engine.recall("authentication setup", top_k=5, token_budget=2000)
for r in results:
print(f"{r.score:.3f} | {r.memory.text}")
print(f" components: {r.components}")

reinforce

engine.reinforce(memory_id: str) -> None

Boost a memory's strength. Call this when a memory proves useful.

  • Increases strength by 0.1 (clamped to 1.0)
  • Increments access_count
  • Updates last_accessed timestamp
engine.reinforce(m.id)

supersede

engine.supersede(old_id: str, new_id: str) -> None

Mark a memory as outdated, replaced by a newer one.

  • Sets old memory's status to "superseded" (50% score penalty)
  • Creates a same_as link from new to old (weight 0.3)
m_old = engine.add("API uses v1 endpoints", type="fact")
m_new = engine.add("API migrated to v2 endpoints", type="fact")
engine.supersede(m_old.id, m_new.id)

contradict

engine.contradict(id_a: str, id_b: str) -> None

Flag two memories as contradicting each other. During recall, the weaker memory (by strength x confidence x recency) gets demoted to 30% of its score.

  • Creates a contradicts edge (weight 0.8) between the two memories
engine.contradict(m1.id, m2.id)

decay_all

engine.decay_all() -> None

Run a decay pass over all memories. Reduces strength based on time elapsed:

new_strength = strength * exp(-0.01 * days_elapsed)

Call this periodically (e.g. daily, or on each session start).


stats

engine.stats() -> dict

Returns summary statistics:

KeyTypeDescription
memory_countintTotal memories
edge_countintTotal edges
avg_strengthfloatAverage memory strength
active_countintMemories with status "active"
superseded_countintMemories with status "superseded"
contradicted_countintMemories with status "contradicted"

Data models

Memory

from openmem import Memory
FieldTypeDescription
idstrUUID hex string
typestrMemory type
textstrContent
giststr | NoneOptional summary
entitieslist[str]Tagged entities
created_atfloatUnix timestamp
updated_atfloatUnix timestamp
strengthfloat0-1, decays over time
confidencefloat0-1, set at creation
access_countintNumber of times recalled
last_accessedfloat | NoneLast recall timestamp
statusstractive, superseded, or contradicted

Edge

from openmem import Edge
FieldTypeDescription
idstrUUID hex string
source_idstrSource memory ID
target_idstrTarget memory ID
rel_typestrEdge type
weightfloat0-1
created_atfloatUnix timestamp

ScoredMemory

from openmem import ScoredMemory
FieldTypeDescription
memoryMemoryThe memory object
scorefloatFinal competition score
activationfloatRaw activation value
componentsdictScore breakdown: {activation, recency, strength, confidence}