Skip to main content

Human-like memory for AI applications

Project description

recollect

Cognitive memory system for AI applications. Activation-based retrieval with time decay, spreading activation, and token-budgeted recall.

Install

pip install recollect                 # Includes Anthropic provider
pip install recollect[openai]         # Adds OpenAI/OpenAI-compatible provider support
pip install recollect[pydantic-ai]    # Adds pydantic-ai multi-provider support

Quick Start

import asyncio
from recollect import CognitiveMemory

async def main():
    memory = CognitiveMemory()
    await memory.connect()

    await memory.experience(
        "The team decided to migrate from Redis to PostgreSQL for persistence."
    )

    thoughts = await memory.think_about("database decisions", token_budget=500)
    for thought in thoughts:
        print(f"[{thought.activation:.2f}] {thought.content}")

    await memory.close()

asyncio.run(main())

API

Method Description
connect(db_url=None) Connect to PostgreSQL. Uses DATABASE_URL env var if no argument.
experience(content) Store a memory trace. LLM extracts entities, concepts, significance.
think_about(query, token_budget) Retrieve memories that fit within a token limit. Returns list[Thought].
consolidate(threshold=None) Merge and prune weak traces.
forget(trace_id) Remove a trace.
reinforce(trace_id, factor=1.1) Strengthen a trace.
facts(subject=None) List persona facts.
start_session(user_id) Begin a scoped session.
close() Disconnect and release resources.

Environment Variables

Variable Required Default Description
DATABASE_URL Yes postgresql://localhost:5432/memory_sdk PostgreSQL connection string.
ANTHROPIC_API_KEY For Anthropic provider -- Anthropic API key. SDK resolves automatically.
OPENAI_API_KEY For OpenAI provider -- OpenAI API key. Required by OpenAIProvider.
ANTHROPIC_MODEL No claude-haiku-4-5-20251001 Override default Anthropic extraction model.
OPENAI_MODEL No gpt-5-mini Override default OpenAI extraction model.
OLLAMA_MODEL No qwen3.5 Override default Ollama extraction model.
ANTHROPIC_BASE_URL No api.anthropic.com Override Anthropic API endpoint.
OPENAI_BASE_URL No api.openai.com Override OpenAI API endpoint.
OLLAMA_BASE_URL No http://localhost:11434/v1 Ollama API endpoint.
PYDANTIC_AI_MODEL No -- pydantic-ai model string in provider:model format (e.g., ollama:ministral-3, anthropic:claude-haiku-4-5-20251001).
MEMORY_EXTRACTION_MAX_TOKENS No 4096 Max tokens for LLM extraction. Increase for reasoning models that consume thinking tokens.
OPENAI_REASONING_EFFORT No low Reasoning effort for OpenAI structured output (low, medium, high).
OPENAI_STRUCTURED_MAX_TOKENS No 1024 Token cap for OpenAI structured output. Reasoning tokens consume this budget.
MEMORY_CONFIG No -- Path to custom TOML config file.
MEMORY_EXTRACTION_INSTRUCTIONS No -- Override extraction prompt instructions.
MEMORY_RECALL_TOKENS_ENABLED No true Enable write-time token stamping and query-time activation.
MEMORY_RECALL_TOKENS_TOP_K No 5 Max related traces to consider for token assessment.
MEMORY_RECALL_TOKENS_THRESHOLD No 0.3 Min cosine similarity to consider a trace as related.
MEMORY_RECALL_TOKENS_STRENGTH_THRESHOLD No 0.1 Min token strength to activate at query time.
MEMORY_RECALL_TOKENS_SCORE_BONUS No 0.1 Gated additive bonus: token_strength * bonus * effective_sim.
MEMORY_RECALL_TOKENS_REINFORCE_BOOST No 0.1 Strength increment on token activation (capped at 1.0).
MEMORY_RECALL_TOKENS_DECAY_FACTOR No 0.9 Multiply inactive token strength by this during consolidation.

Configuration

Defaults ship in config.toml. Override by placing a memory.toml in your working directory, or set MEMORY_CONFIG to a custom path. Only include keys you want to change:

[memory]
decay_rate = 0.05

[retrieval]
max_retrievals = 10

[extraction]
max_tokens = 2048
pydantic_ai_model = "ollama:ministral-3"   # pydantic-ai provider:model format

Config sections

Section Controls Key parameters
[database] PostgreSQL connection url
[memory] Core memory model initial_strength, consolidation_threshold, decay_rate
[working_memory] Working memory capacity capacity (default 7, range 5-9)
[retrieval] Retrieval pipeline tuning max_retrievals, search_limit, selection_threshold
[extraction] LLM extraction max_tokens, max_concepts, max_relations, pydantic_ai_model, openai_reasoning_effort, openai_structured_max_tokens
[embedding] Local embedding model model, dimensions
[persona] Persona fact management auto_extract, confidence_threshold
[session] Session summaries summary_strength, summary_max_tokens

Full defaults: config.toml

Or pass a path directly:

from recollect.config import MemoryConfig

config = MemoryConfig(config_path=Path("./my-config.toml"))
memory = CognitiveMemory(config=config)

LLM Providers

Four providers behind the LLMProvider protocol. The first three share the same constructor signature: (model, api_key, base_url, defaults).

Anthropic (default)

from recollect.llm.anthropic import AnthropicProvider
from recollect.extraction import PatternExtractor

provider = AnthropicProvider()  # uses ANTHROPIC_API_KEY
extractor = PatternExtractor(provider)
memory = CognitiveMemory(extractor=extractor)

OpenAI

Requires pip install recollect[openai].

from recollect.llm.openai import OpenAIProvider

provider = OpenAIProvider()  # uses OPENAI_API_KEY

Ollama / LM Studio (OpenAI-compatible)

from recollect.llm.openai_compat import OpenAICompatProvider

# Ollama
provider = OpenAICompatProvider(
    model="qwen3.5",
    base_url="http://localhost:11434/v1",
)

# LM Studio
provider = OpenAICompatProvider(
    model="your-model",
    base_url="http://localhost:1234/v1",
)

pydantic-ai (multi-provider)

Requires pip install recollect[pydantic-ai]. Routes calls through pydantic-ai's Agent abstraction, giving access to 20+ model providers through a single dependency.

The model string uses pydantic-ai format (provider:model). Credentials are read from the environment by the underlying provider (e.g., ANTHROPIC_API_KEY for Anthropic, OLLAMA_BASE_URL for Ollama). Provider-specific model overrides like OLLAMA_MODEL or ANTHROPIC_MODEL are not used -- the model is embedded in PYDANTIC_AI_MODEL.

from recollect.llm.pydantic_ai import PydanticAIProvider

# Anthropic via pydantic-ai
provider = PydanticAIProvider(model="anthropic:claude-haiku-4-5-20251001")

# Ollama via pydantic-ai (reads OLLAMA_BASE_URL from env)
provider = PydanticAIProvider(model="ollama:ministral-3")

# OpenAI via pydantic-ai
provider = PydanticAIProvider(model="openai:gpt-4o")

Reasoning models

Models that use internal chain-of-thought (OpenAI o1/o3, Qwen3, DeepSeek-R1) consume thinking tokens from the max_tokens budget. If extraction returns empty responses, increase the token budget:

# memory.toml
[extraction]
max_tokens = 8192

The default is 4096, which provides sufficient headroom for most models.

Requirements

  • Python 3.12+
  • PostgreSQL 17 with pgvector
  • DATABASE_URL environment variable

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

recollect-0.5.0.tar.gz (66.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

recollect-0.5.0-py3-none-any.whl (80.3 kB view details)

Uploaded Python 3

File details

Details for the file recollect-0.5.0.tar.gz.

File metadata

  • Download URL: recollect-0.5.0.tar.gz
  • Upload date:
  • Size: 66.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.7 {"installer":{"name":"uv","version":"0.10.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for recollect-0.5.0.tar.gz
Algorithm Hash digest
SHA256 c023ca2662bf8646b57d8ecf12be28173a8660202b3606996548c8b6edb40a86
MD5 a1cd04425a20d4c9e9ea32b3aca0b955
BLAKE2b-256 3fa0aa979639a955e82b8621dc7b74d81007fa535d25be675d35fc7b9323513f

See more details on using hashes here.

File details

Details for the file recollect-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: recollect-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 80.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.7 {"installer":{"name":"uv","version":"0.10.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for recollect-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 94a2bb04fa27b61d3f9288f620a4b311c5d4e5262168d71c9fdc8c980faa5d32
MD5 081f553b48ce4ac746d348c2542f2ea8
BLAKE2b-256 7272740e3d67681230b3c4a279b131e8d26e33ebe18bae418f0e252914783b66

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page