Skip to main content

KMF — Knowledge Memory Format: portable, local, versioned AI memory

Project description

KMF — Knowledge Memory Format

Portable, local, versioned AI memory. v1.0

KMF is an open format and Python library for structured AI memory. It solves the problem that current AI systems either have no persistent memory, or store everything as unstructured text blobs with no provenance, policy, or control.


Merging Repositories

# Merge two repos (e.g. work laptop + home laptop)
kmf merge work.kmf home.kmf --output merged.kmf

# Dry run — see what would change
kmf merge work.kmf home.kmf --dry-run

# Merge only specific scopes
kmf merge personal.kmf colleague.kmf --scopes work
base = KMF.load("work.kmf")
home = KMF.load("home.kmf")
result = base.merge(home)
print(result.summary())
base.save("merged.kmf")

Merge strategy: same ID → newer wins. Conflicting facts (same subject+predicate, different value) → Conflict object created. Preferences → newer wins.


LangChain Integration

pip install "kmf[langchain]"
from langchain.chains import ConversationChain
from langchain_openai import ChatOpenAI
from kmf.integrations.langchain import KMFMemory, KMFRetriever

# Drop-in memory replacement
memory = KMFMemory.from_file("personal.kmf", api_key="sk-ant-...")
chain = ConversationChain(llm=ChatOpenAI(), memory=memory)
chain.predict(input="What did we decide about the database?")
# → loads KMF context, answers, stores new memory after each turn

# Retriever for RAG chains
retriever = KMFRetriever.from_file("personal.kmf")
docs = retriever.get_relevant_documents("Python architecture")

Semantic Search

KMF uses local embeddings for recall — no cloud, no API key needed.

# Install (one-time, ~80MB model download)
pip install "kmf[semantic]"

# Build index
kmf index personal.kmf

After indexing, recall() automatically uses semantic similarity instead of keyword matching. If sentence-transformers is not installed, KMF silently falls back to keyword search — nothing breaks.

Default model: all-MiniLM-L6-v2 — fast on CPU, works well in German and English.

# Custom model
kmf = KMF.load("personal.kmf")
bundle = kmf.recall("what did we decide about the database?")
# → uses semantic search if indexed, keywords otherwise

MCP Server (Claude Desktop / claude.ai)

KMF ships a built-in MCP server. Once configured, Claude automatically has access to your memory — no manual context injection needed.

Setup

1. Add to claude_desktop_config.json:

{
  "mcpServers": {
    "kmf": {
      "command": "kmf",
      "args": ["mcp", "/path/to/personal.kmf"]
    }
  }
}

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json Windows: %APPDATA%\Claude\claude_desktop_config.json

2. Restart Claude Desktop. KMF tools appear automatically.

Available Tools

Tool What it does
kmf_recall Load relevant memory context for a query
kmf_remember Store a fact, preference, or decision directly
kmf_ingest_text Extract and store memory from a conversation
kmf_forget Archive (soft-delete) an outdated memory
kmf_stats Repository statistics
kmf_conflicts List unresolved contradictions

Workflow

Start of conversation:
  → Claude calls kmf_recall("user preferences and recent decisions")
  → Gets compact TOON context block (~200-500 tokens vs 2000+ raw)

During conversation:
  → Nothing — chat as normal

End of conversation:
  → Claude calls kmf_ingest_text(transcript) using Haiku
  → New facts/decisions stored for next time

Install

pip install kmf

Or from source:

git clone https://github.com/you/kmf
cd kmf
pip install -e .

Quick Start

from kmf import KMF, SessionInput

# Create a repository
kmf = KMF.create("personal")

# Consolidate a session into memory
session = SessionInput(
    title="Architecture discussion",
    summary_short="Decided on local-first approach",
    proposed_facts=[{
        "subject": "project",
        "predicate": "architecture",
        "object": "local-first",
        "confidence": 0.95,
        "writer": "explicit_user_statement",
    }],
    proposed_preferences=[{
        "domain": "language",
        "key": "response_language",
        "value": "de",
        "strength": "strong",
        "writer": "explicit_user_statement",
    }],
    proposed_decisions=[{
        "title": "Local-first Architecture",
        "decision": "Repository lives locally with the user",
        "rationale": ["Control", "Portability", "Privacy"],
        "tradeoffs": ["Sync complexity"],
        "confidence": 0.95,
        "writer": "explicit_user_statement",
    }],
)
result = kmf.consolidate(session)

# Recall context for a query (token-budgeted)
bundle = kmf.recall("what did we decide about the architecture?")
print(bundle.rendered)

# Save to file
kmf.save("personal.kmf")

# Load later
kmf = KMF.load("personal.kmf")

Output:

[MEMORY CONTEXT — bundle:ctx_... — scopes: — budget:2000tok]

[PROFILE]
prefs[1]{domain,key,value,strength}:
language,response_language,de,strong

[FACTS]
facts[1]{subject,predicate,object,conf}:
project,architecture,local-first,0.95

[DECISIONS]
decisions[1]{title,decision,conf}:
Local-first Architecture,Repository lives locally with the user,0.95

[RECENT EPISODES]
• Architecture discussion: Decided on local-first approach

[/MEMORY CONTEXT]

CLI

# Create a new repository
kmf init personal --description "My personal memory"

# Consolidate a session JSON file
kmf consolidate personal.kmf session.json

# Recall context for a query
kmf recall personal.kmf "what did we decide about the database?"

# List objects
kmf list personal.kmf --type fact

# Show history
kmf history personal.kmf

# Stats
kmf stats personal.kmf

Core Concepts

Objects

Every memory item is a structured object with metadata:

Type Purpose
episode A completed session or interaction
fact A declarative statement with subject/predicate/object
preference A user preference
decision A decision with rationale and alternatives
entity A named entity (person, project, concept)
summary A condensed view of other objects
conflict An explicit contradiction between two objects
rule A user-defined policy

Every object carries: confidence, importance, sensitivity, provenance, permissions, valid_time.

Key Principles

Storing ≠ Prompting — An object can be stored but never sent to a prompt. Three separate permission layers.

Consolidation is separate from chat — The chat produces raw signal. A post-session consolidation process decides what persists. This prevents contamination from smalltalk, irony, or model errors.

Conflicts are explicit — When two facts contradict each other, a conflict object is created and surfaced in recall. No silent overwrites.

Local-first — The repository lives with the user. Cloud sync is optional, not the default.

TOON Format

Prompt bundles use TOON (Token-Optimized Output Notation) instead of JSON — saving 30–85% tokens for tabular data:

# JSON: ~580 tokens
# TOON: ~85 tokens for the same 3 decisions

decisions[3]{title,decision,conf}:
Local-first Architecture,Repository lies locally with the user,0.95
Dual-Format JSON+TOON,JSON in store / TOON in prompt,0.92
Consolidation separate from chat,No direct writes from chat,0.90

Session JSON Format

{
  "title": "Session title",
  "summary_short": "One-line summary",
  "summary_long": "Longer summary",
  "topics": ["topic1", "topic2"],
  "proposed_facts": [
    {
      "subject": "project",
      "predicate": "uses",
      "object": "PostgreSQL",
      "confidence": 0.9,
      "writer": "explicit_user_statement"
    }
  ],
  "proposed_preferences": [
    {
      "domain": "style",
      "key": "format",
      "value": "direct, no filler",
      "strength": "strong",
      "writer": "explicit_user_statement"
    }
  ],
  "proposed_decisions": [
    {
      "title": "Decision title",
      "decision": "What was decided",
      "rationale": ["reason 1", "reason 2"],
      "tradeoffs": ["tradeoff 1"],
      "confidence": 0.95,
      "writer": "explicit_user_statement"
    }
  ],
  "proposed_entities": [
    {
      "entity_type": "project",
      "canonical_name": "Project Alpha",
      "aliases": ["alpha"],
      "confidence": 0.95
    }
  ]
}

Architecture

KMF
├── models/        Pydantic object models (KMFObject, Chunk, Commit, Manifest)
├── storage/       SQLite-backed repository with .kmf import/export
├── recall/        7-stage recall pipeline with scoring formula
├── consolidation/ Post-session engine: raw input → persistent objects
├── render/        TOON renderer for prompt bundles
├── policy/        Sensitivity and access control
└── cli/           Command-line interface

Recall Scoring

score = semantic_relevance * 0.35
      + importance         * 0.20
      + confidence         * 0.15
      + recency            * 0.15
      + scope_match        * 0.10
      - sensitivity_penalty * 0.05

Roadmap

  • v0.2: Embedding-based semantic search (model-agnostic)
  • v0.2: Multi-device sync protocol
  • v0.3: Multimodal support (image captions, audio transcripts)
  • v0.3: LangChain / LlamaIndex integration
  • v1.0: Formal interoperability standard

License

MIT


KMF is an open specification. No company, no lock-in. The goal: a common standard for portable AI memory.

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

kmf-1.0.1.tar.gz (74.7 kB view details)

Uploaded Source

Built Distribution

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

kmf-1.0.1-py3-none-any.whl (68.0 kB view details)

Uploaded Python 3

File details

Details for the file kmf-1.0.1.tar.gz.

File metadata

  • Download URL: kmf-1.0.1.tar.gz
  • Upload date:
  • Size: 74.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.1

File hashes

Hashes for kmf-1.0.1.tar.gz
Algorithm Hash digest
SHA256 336e1cfcd2293852631afecfdc4447925402b4b63be0142358a40a1dfa08cb2c
MD5 f677de1d5157eff9c261149fb5e8bfed
BLAKE2b-256 6b64e2a652acb4792e5c86d1ab3e815555d77c8f0649369ff37af455d17bbabc

See more details on using hashes here.

File details

Details for the file kmf-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: kmf-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 68.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.1

File hashes

Hashes for kmf-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fef8d3536df28b68e0c269dcbfc7c6eb2073faffba2d7735b68dff395990cbe8
MD5 4e71695447940f13a54031de23e8cf92
BLAKE2b-256 bbac9be08f76e30aaaf67afd197ed559fcf613992177585f728c768fdc2e376f

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