API Reference¶
Auto-generated from source code docstrings. For conceptual guides on how these components work together, see the Concepts section.
Client¶
MemoryClient¶
MemoryClient
¶
Main entry point for the arandu SDK.
Usage::
from arandu import MemoryClient, MemoryConfig
from arandu.providers.openai import OpenAIProvider
provider = OpenAIProvider(api_key="sk-...")
memory = MemoryClient(
database_url="postgresql+psycopg://user:pass@localhost/mydb",
llm=provider,
embeddings=provider,
)
await memory.initialize()
result = await memory.write(agent_id="my_agent", message="I live in São Paulo", speaker_name="Pedro")
context = await memory.retrieve(agent_id="my_agent", query="where does Pedro live?")
write
async
¶
write(agent_id: str, message: str, speaker_name: str, source: str = 'api', *, session_id: str = 'default', config_overrides: dict | None = None, dry_run: bool = False, recent_messages: list[str] | None = None, verbose: bool = False, occurred_at: datetime | None = None) -> WriteResult
Extract facts from a message and store them in memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Identifier for the agent whose memory this is. All facts, entities, and relationships are scoped to this agent. Use the same agent_id to share memory across sessions. |
required |
message
|
str
|
The message text to extract knowledge from. |
required |
speaker_name
|
str
|
Name of the speaker (the person talking). Required.
This identifies WHO is speaking — the person whose words are being
memorized. First-person pronouns ("I", "me", "my") resolve to
this name. Example: |
required |
source
|
str
|
Source channel identifier (default: "api"). |
'api'
|
session_id
|
str
|
Identifier for the conversation session (default: "default"). Use different session_ids to separate conversation contexts. The agent's memory persists across sessions, but session_id helps track which conversation each event came from. |
'default'
|
config_overrides
|
dict | None
|
Optional dict of MemoryConfig field overrides for this request.
Only the provided keys are changed; all others inherit from the client config.
Example: |
None
|
dry_run
|
bool
|
If True, run extraction + resolution + reconciliation but skip the upsert stage. No data is persisted. Useful for benchmarking. |
False
|
recent_messages
|
list[str] | None
|
Optional conversation context (last N messages) for resolving pronouns and anaphora references in extraction. |
None
|
verbose
|
bool
|
If True, include pipeline trace with intermediate data. |
False
|
occurred_at
|
datetime | None
|
Optional timestamp for when this message was sent. Defaults to now. Use for importing historical conversations so the extraction can resolve relative dates correctly. |
None
|
Returns:
| Type | Description |
|---|---|
WriteResult
|
WriteResult with details of extracted and stored facts. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If speaker_name is empty or whitespace. |
retrieve
async
¶
retrieve(agent_id: str, query: str, *, session_id: str | None = None, config_overrides: dict | None = None, verbose: bool = False, entity_keys: list[str] | None = None) -> RetrieveResult
Retrieve relevant memory context for a query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Identifier for the agent whose memory to search. |
required |
query
|
str
|
The query to search memory for. |
required |
session_id
|
str | None
|
Optional session filter. If None (default), searches across all sessions. If provided, may be used for context prioritization in future versions. |
None
|
config_overrides
|
dict | None
|
Optional dict of MemoryConfig field overrides for this request.
Only the provided keys are changed; all others inherit from the client config.
Example: |
None
|
verbose
|
bool
|
If True, include pipeline trace with intermediate data. |
False
|
entity_keys
|
list[str] | None
|
Optional list of entity keys to filter by (OR logic).
When provided, only facts linked to at least one of the specified
entities are returned. Example: |
None
|
Returns:
| Type | Description |
|---|---|
RetrieveResult
|
RetrieveResult with ranked facts and formatted context string. |
get
async
¶
Return a single fact by ID, or None if not found.
Fetches any fact belonging to agent_id regardless of its
valid_to status — even facts that have been soft-deleted by
the reconciliation pipeline are returned when looked up by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Agent that owns the fact. |
required |
fact_id
|
str
|
UUID of the fact to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
FactDetail | None
|
A |
FactDetail | None
|
if no matching fact exists. |
get_all
async
¶
get_all(agent_id: str, *, limit: int = 100, offset: int = 0, entity_keys: list[str] | None = None) -> list[FactDetail]
List active facts for an agent with pagination.
Only facts with valid_to IS NULL (i.e. not soft-deleted by the
reconciliation pipeline) are returned. Results are ordered by
created_at descending (newest first).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Agent whose facts to list. |
required |
limit
|
int
|
Maximum number of facts to return (default 100). |
100
|
offset
|
int
|
Number of facts to skip (default 0). |
0
|
entity_keys
|
list[str] | None
|
Optional list of entity keys to filter by (OR logic).
When provided, only facts linked to at least one of the specified
entities are returned. Aliases registered in
|
None
|
Returns:
| Type | Description |
|---|---|
list[FactDetail]
|
A list of |
delete
async
¶
Hard-delete a single fact by ID.
The row is physically removed from the database. Associated
MemoryFactEntityLink rows are removed automatically via the
CASCADE foreign-key constraint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Agent that owns the fact. |
required |
fact_id
|
str
|
UUID of the fact to delete. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
fact was found. |
delete_all
async
¶
Hard-delete all memory for an agent.
Removes all facts, entities, aliases, and relationships belonging
to agent_id. MemoryFactEntityLink rows are removed
automatically via the CASCADE foreign-key constraint on
MemoryFact. MemoryEvent rows are preserved (immutable
audit log).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Agent whose memory to delete. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of facts deleted. |
entities
async
¶
List known entities for an agent.
Returns active entities (is_active = True) ordered by
last_seen_at descending (most recently seen first), with
id as tiebreaker for stable pagination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Agent whose entities to list. |
required |
limit
|
int
|
Maximum number of entities to return (default 100). |
100
|
offset
|
int
|
Number of entities to skip (default 0). |
0
|
Returns:
| Type | Description |
|---|---|
list[EntityDetail]
|
A list of |
WriteResult¶
WriteResult
dataclass
¶
Result of a memory write operation.
RetrieveResult¶
RetrieveResult
dataclass
¶
Result of a memory retrieve operation.
The warnings field contains user-facing strings for any entity_keys
the caller passed that did not resolve to a canonical entity or alias.
When the filter matches fully, warnings is empty.
FactDetail¶
FactDetail
dataclass
¶
Detailed view of a stored fact — used by CRUD operations (get, get_all).
Unlike ScoredFact (which carries retrieval scores), FactDetail
exposes the full metadata of a fact as stored in the database.
EntityDetail¶
EntityDetail
dataclass
¶
Detailed view of a known entity — used by the entities() method.
ScoredFact¶
ScoredFact
dataclass
¶
A fact with retrieval scores.
The scores dict contains the breakdown of individual scoring signals.
Possible keys (not all are always present):
semantic— cosine similarity from pgvector (0.0–1.0)keyword— fraction of query words found in fact text (0.0–1.0)recency— exponential decay based on fact age (configurable half-life)importance— base importance value of the fact (typically 0.5)confidence— effective confidence with temporal decaypattern— additive boost for reinforced facts (up to 0.1)graph— score from BFS 2-hop knowledge graph traversalreranker— LLM-based relevance score (0.0–1.0, only when reranker is enabled)
PipelineTrace¶
PipelineTrace
dataclass
¶
Collects intermediate data from pipeline stages.
Usage in pipeline functions::
trace = PipelineTrace()
t0 = trace.start()
# ... run extraction ...
trace.add_step("extraction", {"entities": [...]}, trace.elapsed(t0))
add_step
¶
Record a pipeline step with its data and timing.
Configuration¶
MemoryConfig¶
MemoryConfig
dataclass
¶
Configuration for the memory system.
All fields have sensible defaults. Override only what you need.
Protocols¶
LLMProvider¶
LLMProvider
¶
Bases: Protocol
Protocol for LLM completion calls.
complete
async
¶
complete(messages: list[dict], temperature: float = 0, response_format: dict | None = None, max_tokens: int | None = None) -> LLMResult
Send a chat completion request and return the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list[dict]
|
List of message dicts with 'role' and 'content' keys. |
required |
temperature
|
float
|
Sampling temperature (0 = deterministic). |
0
|
response_format
|
dict | None
|
Optional format spec (e.g. {"type": "json_object"}). |
None
|
max_tokens
|
int | None
|
Optional max tokens for the response. |
None
|
Returns:
| Type | Description |
|---|---|
LLMResult
|
LLMResult with the response text and optional token usage. |
EmbeddingProvider¶
EmbeddingProvider
¶
Bases: Protocol
Protocol for text embedding generation.
embed
async
¶
Generate embeddings for a batch of texts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
list[str]
|
List of text strings to embed. |
required |
Returns:
| Type | Description |
|---|---|
list[list[float]]
|
List of embedding vectors (one per input text). |
embed_one
async
¶
Generate an embedding for a single text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text string to embed. |
required |
Returns:
| Type | Description |
|---|---|
list[float] | None
|
Embedding vector, or None if the text is empty/invalid. |
Providers¶
OpenAIProvider¶
OpenAIProvider
¶
OpenAI implementation of LLMProvider + EmbeddingProvider.
Implements both protocols with a single class via composition (not inheritance). The openai SDK is imported lazily to keep it an optional dependency.
Usage::
from arandu.providers.openai import OpenAIProvider
provider = OpenAIProvider(api_key="sk-...")
memory = MemoryClient(
database_url="postgresql+psycopg://...",
llm=provider,
embeddings=provider,
)
complete
async
¶
complete(messages: list[dict], temperature: float = 0, response_format: dict | None = None, max_tokens: int | None = None) -> LLMResult
Send a chat completion request and return the result with usage.
embed
async
¶
Generate embeddings for a batch of texts.
embed_one
async
¶
Generate an embedding for a single text.
AnthropicProvider¶
Built-in LLM provider for Anthropic Claude models. Implements LLMProvider only - does not provide embeddings (use OpenAIProvider or another embedding provider alongside).
from arandu.providers.anthropic import AnthropicProvider
llm = AnthropicProvider(
api_key="sk-ant-...", # Anthropic API key
model="claude-sonnet-4-20250514", # Default model
timeout=30.0, # Request timeout in seconds
max_tokens=4096, # Default max tokens per response
)
Install: pip install arandu[anthropic]
Key behaviors:
- System messages are extracted from the messages list and passed via Anthropic's
systemparameter response_format={"type": "json_object"}appends a JSON instruction to the system prompt (Anthropic doesn't support this natively)- Token usage is tracked via
LLMResult.usage - Markdown code fences are stripped automatically
Exceptions¶
All SDK exceptions inherit from MemoryError. The SDK is fail-safe by default - most errors are caught internally and result in graceful degradation (empty results, logged warnings). These exceptions are raised only when explicitly documented.
MemoryError (base)
├── ExtractionError — LLM extraction failed
├── ResolutionError — entity resolution failed
├── ReconciliationError — fact reconciliation failed
├── RetrievalError — memory retrieval failed
└── UpsertError — database persistence failed
MemoryError¶
Base exception for all arandu SDK errors. Catch this for catch-all handling.
from arandu import MemoryError
try:
result = await memory.write(agent_id, message, speaker_name="Rafael")
except MemoryError as e:
print(f"SDK error: {e}")
print(f"Caused by: {e.__cause__}") # original exception, if any
ExtractionError¶
Raised when fact/entity extraction from a message fails (LLM timeout, invalid JSON response, rate limit). In practice, the write pipeline catches this internally and returns an empty WriteResult with success=True - the event is still logged.
ResolutionError¶
Raised when entity resolution fails (embedding provider down, LLM disambiguation timeout). The pipeline catches this internally - unresolved entities are created as new entities.
ReconciliationError¶
Raised when fact reconciliation against existing knowledge fails (LLM call fails for similarity evaluation). The pipeline catches this internally - defaults to ADD (better to have a near-duplicate than lose information).
RetrievalError¶
Raised when memory retrieval fails (semantic search error, reranker timeout). The read pipeline catches this internally - returns empty results rather than crashing.
UpsertError¶
Raised when upserting facts or entities into the database fails (constraint violation, connection error). Individual fact upserts use savepoints - if one fact fails, the others proceed normally.
Error handling example¶
from arandu import MemoryError, ExtractionError, RetrievalError
# Write — fail-safe by default
result = await memory.write(agent_id, message, speaker_name="Rafael")
if not result.success:
print(f"Write failed: {result.error}")
# No try/except needed — the pipeline handles errors internally
# Retrieve — also fail-safe
result = await memory.retrieve(agent_id, query)
# Empty facts = nothing found OR error occurred
# Check result.duration_ms to detect timeouts
Background Functions¶
Clustering¶
cluster_user_facts¶
cluster_user_facts
async
¶
cluster_user_facts(session: AsyncSession, agent_id: str, embedding_provider: EmbeddingProvider, llm_provider: LLMProvider, config: MemoryConfig) -> ClusteringResult
Build or update clusters for a user's facts. Idempotent.
Groups active facts by (entity_type, entity_key), creates or updates
clusters with LLM-generated summaries and embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
AsyncSession
|
Database session. |
required |
agent_id
|
str
|
User identifier. |
required |
embedding_provider
|
EmbeddingProvider
|
Injected embedding provider. |
required |
llm_provider
|
LLMProvider
|
Injected LLM provider. |
required |
config
|
MemoryConfig
|
Memory configuration. |
required |
Returns:
| Type | Description |
|---|---|
ClusteringResult
|
ClusteringResult with stats. |
detect_communities¶
detect_communities
async
¶
detect_communities(session: AsyncSession, agent_id: str, embedding_provider: EmbeddingProvider, llm_provider: LLMProvider, config: MemoryConfig) -> CommunityDetectionResult
Find cross-entity themes by comparing cluster embeddings.
Uses Union-Find to group clusters from different entity_keys with similar embeddings into communities, materialized as MemoryMetaObservation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
AsyncSession
|
Database session. |
required |
agent_id
|
str
|
User identifier. |
required |
embedding_provider
|
EmbeddingProvider
|
Injected embedding provider. |
required |
llm_provider
|
LLMProvider
|
Injected LLM provider. |
required |
config
|
MemoryConfig
|
Memory configuration. |
required |
Returns:
| Type | Description |
|---|---|
CommunityDetectionResult
|
CommunityDetectionResult with stats. |
Consolidation¶
run_consolidation¶
run_consolidation
async
¶
run_consolidation(session: AsyncSession, agent_id: str, llm_provider: LLMProvider, config: MemoryConfig, *, embedding_provider: EmbeddingProvider | None = None) -> ConsolidationResult
L2 Periodic consolidation: detect patterns from events and facts.
Loads events within the configured lookback window, detects patterns via LLM, and creates/reinforces meta-observations with dedup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
AsyncSession
|
Database session. |
required |
agent_id
|
str
|
User identifier. |
required |
llm_provider
|
LLMProvider
|
Injected LLM provider. |
required |
config
|
MemoryConfig
|
Memory configuration. |
required |
embedding_provider
|
EmbeddingProvider | None
|
Optional embedding provider for dedup. |
None
|
Returns:
| Type | Description |
|---|---|
ConsolidationResult
|
ConsolidationResult with stats. |
run_profile_consolidation¶
run_profile_consolidation
async
¶
run_profile_consolidation(session: AsyncSession, agent_id: str, llm_provider: LLMProvider) -> dict[str, Any]
L3 Profile consolidation: refresh entity summaries via LLM.
For each active MemoryEntity, fetches active facts and generates a summary. Caps at 20 entities per run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
AsyncSession
|
Database session. |
required |
agent_id
|
str
|
User identifier. |
required |
llm_provider
|
LLMProvider
|
Injected LLM provider. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with |
Memify¶
run_memify¶
run_memify
async
¶
run_memify(session: AsyncSession, agent_id: str, llm_provider: LLMProvider, embedding_provider: EmbeddingProvider, config: MemoryConfig) -> MemifyResult
Run the memify pipeline: vitality scoring → staleness → edge management.
Step 1: Score all active facts for vitality; mark stale if below threshold. Step 2: Reinforce/decay KG edge strengths based on recency of use. Step 3: (Optional) Detect and execute entity merges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
AsyncSession
|
Database session. |
required |
agent_id
|
str
|
User identifier. |
required |
llm_provider
|
LLMProvider
|
Injected LLM provider. |
required |
embedding_provider
|
EmbeddingProvider
|
Injected embedding provider. |
required |
config
|
MemoryConfig
|
Memory configuration. |
required |
Returns:
| Type | Description |
|---|---|
MemifyResult
|
MemifyResult with pipeline stats. |
compute_vitality¶
compute_vitality
¶
compute_vitality(fact: object, now: datetime | None = None, *, weights: dict[str, float] | None = None) -> float
Compute vitality score for a single fact (0.0-1.0).
Components: - retrieval (0.30): log-scale of times_retrieved - recency (0.25): exponential decay from last_retrieved_at - confidence (0.20): raw confidence value - reinforcement (0.15): bounded reinforcement count
Correction penalty: 0.8 ^ user_correction_count. Superseded facts (valid_to is not None) return 0.0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fact
|
object
|
A MemoryFact-like object. |
required |
now
|
datetime | None
|
Current timestamp (defaults to UTC now). |
None
|
weights
|
dict[str, float] | None
|
Optional custom weight dict. |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Vitality score between 0.0 and 1.0. |
Sleep-Time Compute¶
compute_entity_importance¶
compute_entity_importance
async
¶
compute_entity_importance(session: AsyncSession, agent_id: str, config: MemoryConfig) -> EntityImportanceResult
Compute importance scores for all active entities.
Formula: weighted sum of fact_density, recency, retrieval_frequency,
relationship_degree. Updates MemoryEntity.importance_score in-place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
AsyncSession
|
Database session. |
required |
agent_id
|
str
|
User identifier. |
required |
config
|
MemoryConfig
|
Memory configuration. |
required |
Returns:
| Type | Description |
|---|---|
EntityImportanceResult
|
EntityImportanceResult with stats and top entities. |
refresh_entity_summaries¶
refresh_entity_summaries
async
¶
refresh_entity_summaries(session: AsyncSession, agent_id: str, llm_provider: LLMProvider, config: MemoryConfig) -> SummaryRefreshResult
Refresh entity summaries via LLM for stale entities.
An entity is stale if its summary_text is NULL or
summary_refreshed_at is older than summary_refresh_interval_days.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
AsyncSession
|
Database session. |
required |
agent_id
|
str
|
User identifier. |
required |
llm_provider
|
LLMProvider
|
Injected LLM provider. |
required |
config
|
MemoryConfig
|
Memory configuration. |
required |
Returns:
| Type | Description |
|---|---|
SummaryRefreshResult
|
SummaryRefreshResult with stats. |
detect_entity_communities¶
detect_entity_communities
async
¶
detect_entity_communities(session: AsyncSession, agent_id: str, config: MemoryConfig) -> dict[str, Any]
Connected-component clustering on entity relationship graph.
Groups entities connected by relationships with strength >= 0.3 into communities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
AsyncSession
|
Database session. |
required |
agent_id
|
str
|
User identifier. |
required |
config
|
MemoryConfig
|
Memory configuration. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with |
Result Dataclasses¶
ClusteringResult¶
ClusteringResult
dataclass
¶
Result of fact clustering.
Attributes:
| Name | Type | Description |
|---|---|---|
clusters_created |
int
|
Number of new clusters created. |
clusters_reinforced |
int
|
Number of existing clusters updated. |
summaries_generated |
int
|
Number of cluster summaries generated via LLM. |
facts_assigned |
int
|
Number of facts assigned to clusters. |
CommunityDetectionResult¶
CommunityDetectionResult
dataclass
¶
Result of cross-entity community detection.
Attributes:
| Name | Type | Description |
|---|---|---|
communities_created |
int
|
New community observations created. |
communities_reinforced |
int
|
Existing community observations reinforced. |
clusters_in_communities |
int
|
Total clusters assigned to communities. |
skipped |
bool
|
Whether detection was skipped. |
skip_reason |
str | None
|
Reason for skipping, if any. |
ConsolidationResult¶
ConsolidationResult
dataclass
¶
Result of L2/L3 consolidation.
Attributes:
| Name | Type | Description |
|---|---|---|
events_processed |
int
|
Number of events analyzed. |
observations_created |
int
|
New meta-observations created. |
observations_reinforced |
int
|
Existing observations reinforced. |
skipped |
bool
|
Whether consolidation was skipped. |
skip_reason |
str | None
|
Reason for skipping, if any. |
MemifyResult¶
MemifyResult
dataclass
¶
Result of the memify pipeline.
Attributes:
| Name | Type | Description |
|---|---|---|
facts_scored |
int
|
Number of facts scored for vitality. |
facts_marked_stale |
int
|
Number of facts marked as stale. |
edges_reinforced |
int
|
Number of KG edges reinforced. |
merges_executed |
int
|
Number of entity merges executed. |
EntityImportanceResult¶
EntityImportanceResult
dataclass
¶
Result of entity importance scoring.
Attributes:
| Name | Type | Description |
|---|---|---|
entities_scored |
int
|
Number of entities scored. |
top_entities |
list[tuple[str, float]]
|
Top entities by score (key, score) pairs. |
SummaryRefreshResult¶
SummaryRefreshResult
dataclass
¶
Result of entity summary refresh.
Attributes:
| Name | Type | Description |
|---|---|---|
summaries_refreshed |
int
|
Number of summaries generated. |
summaries_skipped |
int
|
Number of entities skipped (no facts or LLM failure). |
See Also: Advanced API¶
For documentation of internal pipeline functions, sub-module exports, and additional data types not covered here, see the Advanced section:
- Write Pipeline API -- extraction strategy, canonicalization, entity helpers, correction detection, pending operations, and
run_write_pipeline(). - Read Pipeline API -- retrieval agent, query expansion, graph retrieval, spreading activation, context compression, emotional trends, dynamic importance, procedural memory, and
run_read_pipeline(). - Database Utilities --
create_engine(),create_session_factory(),init_db(), and schema overview. - Data Types Reference -- all enums, dataclasses, and result types across write, read, and background modules.