A lightweight, vendor-agnostic context & memory engine for LLM applications
Project description
ContextEngine
ContextEngine is a lightweight, vendor-agnostic, pip-installable context & memory engine for LLM applications.
It adds state and memory to otherwise stateless LLM API calls by automatically storing, retrieving, and trimming past interactions.
This is infrastructure — not a chatbot framework.
✨ Why ContextEngine?
Most LLM APIs are stateless:
- Every API call forgets the past
- Developers manually manage history
- Context windows overflow unpredictably
ContextEngine solves this by providing:
- Automatic memory storage (input + output)
- Semantic-first retrieval (real memory)
- Hybrid context (memory + conversation flow)
- Token-safe context trimming
- Pluggable storage (In-memory, MongoDB, more later)
- Zero vendor lock-in
🚀 Features
- ✅ Auto-save interactions by default
- ✅ Semantic memory (embedding-based)
- ✅ Hybrid retrieval (semantic + recent)
- ✅ Session-based isolation
- ✅ Token-budget aware context trimming
- ✅ InMemoryStore (dev / testing)
- ✅ MongoMemoryStore (production persistence)
- ✅ Fully vendor-agnostic
- ✅ Deterministic & inspectable behavior
📦 Installation
Core (no database dependency)
pip install contextengine
With MongoDB support
pip install pymongo
(ContextEngine keeps database dependencies optional.)
🧠 Core Concepts
ContextUnit
The atomic unit of memory:
(role, content, session_id, metadata, timestamp)
Session
A user-defined identifier that isolates memory:
session_id="user_123"
MemoryStore
Pluggable backend for persistence:
- InMemoryStore
- MongoMemoryStore
- (FAISS / others later)
🔧 Quick Start (In-Memory)
from contextengine import (
ContextEngine,
ContextConfig,
InMemoryStore,
SentenceTransformerEncoder,
)
engine = ContextEngine(
store=InMemoryStore(),
encoder=SentenceTransformerEncoder(),
config=ContextConfig(session_id="demo")
)
engine.store_interaction(
input="My name is Albi",
output="Nice to meet you Albi"
)
context = engine.get_context(query="What is my name?")
print(context)
🗄️ Using MongoDB (Persistent Memory)
from contextengine import ContextEngine, ContextConfig, SentenceTransformerEncoder
from contextengine.memory.mongo import MongoMemoryStore
engine = ContextEngine(
store=MongoMemoryStore(
uri="mongodb://localhost:27017",
db_name="contextengine"
),
encoder=SentenceTransformerEncoder(),
config=ContextConfig(session_id="user_42")
)
engine.store_interaction(
input="I live in Kerala",
output="Kerala is in India"
)
context = engine.get_context(query="Where do I live?")
print(context)
✂️ Token-Safe Context Trimming
ContextEngine automatically enforces a token budget.
ContextConfig(
session_id="chat",
max_tokens=2048,
token_estimator="chars" # or "words"
)
- Oldest messages are removed first
- Order is preserved
- No vendor-specific tokenizers required
🧪 Testing
pytest
Tests run against:
- InMemoryStore
- MongoMemoryStore
Same behavior guaranteed.
🎯 Non-Goals
ContextEngine intentionally does NOT include:
- UI
- Agent frameworks
- Workflow orchestration
- Prompt templates
- LLM clients
It is a memory layer, not an app framework.
🧭 Roadmap
- FAISS / vector DB backend
- Custom token estimators
- Policy-based trimming
- Optional summaries
- Streaming support
📜 License
MIT License
👤 Author
Built independently as an open-source infrastructure project.
---
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file contextengine_ai-0.1.0.tar.gz.
File metadata
- Download URL: contextengine_ai-0.1.0.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
86819b46e5ef00021b89ad10e612e715ac8cdc9ca61a3a467e44d03f3ba07e27
|
|
| MD5 |
ea19847f749f1527d7a4aa1772d66a54
|
|
| BLAKE2b-256 |
1ae19f52d2e21642572d1364d73d81bde729b0a144e3c7edcda4ea93d168fe8b
|
File details
Details for the file contextengine_ai-0.1.0-py3-none-any.whl.
File metadata
- Download URL: contextengine_ai-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c92e41022bf4657276fb0a1e02b151482918bc9d10f3d5083b2fe446b1565cc4
|
|
| MD5 |
671d62365d0a80300fc3062691b5a8ad
|
|
| BLAKE2b-256 |
cd7e9666b990d7e785103763d012d69ddee3b4c07da1f08af14341f808879a54
|