Context Kernel for LLM APIs. Standardize what happens before and after every model call, while keeping execution in your own runtime.
-
Before call:
adapter.plan(context, spec)builds provider-ready payloads and diagnostics -
After call:
adapter.ingest(context, response, spec=...)normalizes output back intoContext - Boundary: lmctx never sends HTTP requests, executes tools, or orchestrates loops
-
Append-only, snapshot-friendly context model (
Context) with immutable-by-default updates -
Unified part model (
Part) for text, images, files, tool calls/results, thinking, compaction -
Loss-resistant round-trips for opaque provider payloads through
provider_rawand blob references -
Pluggable blob storage (
InMemoryBlobStore,FileBlobStore, or customBlobStore) -
Provider adapters + auto routing via
AutoAdapteron(provider, endpoint, api_version) -
Explainable planning through
RequestPlan(included,excluded,warnings,errors) - Minimal dependencies (core package has no runtime deps; provider SDKs are optional extras)
pip install lmctx
# provider extras (optional)
pip install 'lmctx[openai]'
pip install 'lmctx[anthropic]'
pip install 'lmctx[google]'
pip install 'lmctx[bedrock]'
pip install 'lmctx[all]'from openai import OpenAI
from lmctx import AutoAdapter, Context, RunSpec
from lmctx.spec import Instructions
# 1) Build conversation state
ctx = Context().user("What is the capital of France?")
# 2) Describe runtime call settings
spec = RunSpec(
provider="openai",
endpoint="responses.create",
model="gpt-4o-mini",
instructions=Instructions(system="You are concise and accurate."),
)
# 3) Build request payload with lmctx
router = AutoAdapter()
plan = router.plan(ctx, spec)
# 4) Execute with provider SDK in your own code
client = OpenAI()
response = client.responses.create(**plan.request)
# 5) Normalize response back into Context
ctx = router.ingest(ctx, response, spec=spec)
assistant = ctx.last(role="assistant")
if assistant:
print(assistant.parts[0].text)| Type | Role |
|---|---|
Context |
Append-only conversation log (messages, cursor, usage_log, blob_store) |
Part / Message
|
Canonical content model shared across adapters |
RunSpec |
Call configuration (provider, endpoint, model, tools, schema, extras) |
RequestPlan |
Planned payload + diagnostics for observability and debugging |
BlobReference / BlobStore
|
Out-of-line binary/opaque payload storage with integrity verification |
| Adapter |
RunSpec selector |
Typical SDK call |
|---|---|---|
OpenAIResponsesAdapter |
openai / responses.create
|
client.responses.create(**plan.request) |
OpenAIResponsesCompactAdapter |
openai / responses.compact
|
client.responses.compact(**plan.request) |
OpenAIChatCompletionsAdapter |
openai / chat.completions
|
client.chat.completions.create(**plan.request) |
OpenAIImagesAdapter |
openai / images.generate
|
client.images.generate(**plan.request) |
AnthropicMessagesAdapter |
anthropic / messages.create
|
client.messages.create(**plan.request) |
GoogleGenAIAdapter |
google / models.generate_content
|
client.models.generate_content(**plan.request) |
BedrockConverseAdapter |
bedrock / converse
|
client.converse(**plan.request) |
-
docs/README.md: doc map and recommended reading paths -
docs/architecture.md: boundaries, lifecycle, extension points -
docs/data-model.md: concrete type contracts and invariants -
docs/api-reference.md: public API quick reference -
docs/adapters.md: adapter matrix and provider caveats -
docs/examples.md: runnable examples and prerequisites -
docs/logs.md: log files and regeneration workflow
Scripts are in examples/:
- Core (no API keys):
quickstart.py,multimodal.py,blob_stores.py,tool_calling.py - OpenAI:
api_openai_responses.py,api_openai_compact.py,api_openai_chat.py,api_openai_images.py - Anthropic:
api_anthropic.py,api_anthropic_compact.py - Google:
api_google_genai.py,api_google_image_generation.py - Bedrock:
api_bedrock.py
Run one:
uv run python examples/quickstart.pyExample outputs can be stored locally under examples/logs/ (git-ignored by default).
See docs/logs.md for mapping and regeneration commands.
See CONTRIBUTING.md for full guidelines.
uv sync --all-extras --dev
make check- Python
>=3.10,<3.15
Apache License 2.0. See LICENSE.