Skip to main content

A provider-agnostic middleware that gives LLM API calls persistent, human-readable memory using local Markdown files

Project description

ContextMD

A provider-agnostic middleware that gives OpenAI, Anthropic, and LiteLLM API calls persistent, human-readable memory using local Markdown files.

Installation

pip install contextmd

# With provider support
pip install contextmd[openai]      # OpenAI only
pip install contextmd[anthropic]   # Anthropic only
pip install contextmd[litellm]     # LiteLLM (100+ providers)
pip install contextmd[all]         # All providers

Quick Start

OpenAI

from openai import OpenAI
from contextmd import ContextMD

# Wrap your existing client
client = ContextMD(OpenAI(), memory_dir=".contextmd/")

# Use exactly like normal - memory is automatic
response = client.chat.completions.create(
    model="gpt-5.2",
    messages=[{"role": "user", "content": "Hello!"}]
)

Anthropic

from anthropic import Anthropic
from contextmd import ContextMD

client = ContextMD(Anthropic(), memory_dir=".contextmd/")

response = client.messages.create(
    model="claude-opus-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}]
)

LiteLLM (100+ providers)

import litellm
from contextmd import ContextMD

client = ContextMD(litellm, memory_dir=".contextmd/")

# Works with any LiteLLM-supported model
response = client.completion(
    model="gpt-5.2",
    messages=[{"role": "user", "content": "Hello!"}]
)

# Or use Claude, Gemini, etc.
response = client.completion(
    model="claude-opus-4-6",
    messages=[{"role": "user", "content": "Hello!"}]
)

How It Works

ContextMD intercepts your API calls and:

  1. Bootstrap Loading: Injects stored memory into every request
  2. Response Processing: Tracks token usage and extracts memorable facts
  3. Memory Storage: Saves facts to human-readable Markdown files

Memory Types

  • Semantic: Permanent facts (preferences, tech stack, project context)
  • Episodic: Time-stamped events (decisions, tasks completed)
  • Procedural: Learned workflows ("Always use pnpm")

File Structure

.contextmd/
├── MEMORY.md              # Semantic facts (200 line cap)
├── config.md              # Configuration
├── memory/
│   ├── 2025-03-01.md      # Daily episodic logs
│   └── 2025-03-02.md
└── sessions/
    └── 2025-03-01-auth.md # Session snapshots

API Reference

Manual Memory

# Remember something explicitly
client.remember("User prefers dark mode", type="semantic")
client.remember("Completed auth feature", type="episodic")
client.remember("Always run tests before commit", type="procedural")

Session Management

# Create a named session
with client.new_session("auth-implementation") as session:
    response = client.chat.completions.create(...)
    # Session snapshot saved automatically on exit

# Or manually
session = client.new_session("feature-work")
# ... do work ...
session.end()  # Saves snapshot

Configuration

from contextmd import ContextMD, ContextMDConfig

config = ContextMDConfig(
    memory_line_cap=200,           # Max lines in MEMORY.md
    bootstrap_window_hours=48,     # Hours of episodic memory to load
    compaction_threshold=0.8,      # Token threshold for extraction
    snapshot_message_count=15,     # Messages in session snapshots
    extraction_frequency="session_end",  # When to extract
)

client = ContextMD(openai_client, config=config)

CLI

# Initialize in current directory
contextmd init

# View memory
contextmd show

# View recent activity
contextmd history --hours 24

# List sessions
contextmd sessions

# Add memory manually
contextmd add "User prefers TypeScript" --type semantic

# View statistics
contextmd stats

# Reset all memory
contextmd reset

Architecture

┌─────────────────────────────────────────────────────────────┐
│                     Your Application                         │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│                    ContextMD Wrapper                         │
│  ┌─────────────┐  ┌──────────────┐  ┌───────────────────┐  │
│  │   Client    │  │   Memory     │  │    Extraction     │  │
│  │   Wrapper   │──│   Router     │──│    Engine         │  │
│  └─────────────┘  └──────────────┘  └───────────────────┘  │
│         │                │                    │             │
│         │                ▼                    │             │
│         │        ┌──────────────┐             │             │
│         │        │   Storage    │◄────────────┘             │
│         │        │   Layer      │                           │
│         │        └──────────────┘                           │
└─────────│───────────────────────────────────────────────────┘
          │
          ▼
┌─────────────────────────────────────────────────────────────┐
│              Provider Adapters                               │
│  ┌─────────┐    ┌───────────┐    ┌──────────┐              │
│  │ OpenAI  │    │ Anthropic │    │ LiteLLM  │              │
│  └─────────┘    └───────────┘    └──────────┘              │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│                      LLM Provider                            │
└─────────────────────────────────────────────────────────────┘

Development

# Install with dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Type checking
mypy src/contextmd

# Linting
ruff check src/contextmd

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

contextmd-0.1.1.tar.gz (25.2 kB view details)

Uploaded Source

Built Distribution

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

contextmd-0.1.1-py3-none-any.whl (31.4 kB view details)

Uploaded Python 3

File details

Details for the file contextmd-0.1.1.tar.gz.

File metadata

  • Download URL: contextmd-0.1.1.tar.gz
  • Upload date:
  • Size: 25.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for contextmd-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5c494f8f5414337a168bf7695cd85c280cc4ca1a6c4ab38e0dba14b8df9e68be
MD5 7bb10fe5a70ca0e965503b04d603b34c
BLAKE2b-256 01995c2a92617c945878dea90450342bf0dca0921e09143a0f8367b4d356952f

See more details on using hashes here.

Provenance

The following attestation bundles were made for contextmd-0.1.1.tar.gz:

Publisher: publish.yml on Cenrax/contextmd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file contextmd-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: contextmd-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 31.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for contextmd-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2dc4a7916ee090034dbc5f4b2a124c767f8a63c0b457b584b1cc1abcc819f38e
MD5 24c4ef289acabd5786d18927a33ed7d6
BLAKE2b-256 5b5a4a8a8cddd6dc48f01182534bbb617f295d0b2d1a07c50e6ef8212063a944

See more details on using hashes here.

Provenance

The following attestation bundles were made for contextmd-0.1.1-py3-none-any.whl:

Publisher: publish.yml on Cenrax/contextmd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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