Skip to main content

lean-ctx (Python SDK)

Context compression for AI agents — a thin, dependency-free client for the local lean-ctx daemon.

pip install lean-ctx-sdk

Drop-in compress(messages, model)

Compress a chat-style messages array before sending it to any model. Only text payloads are rewritten through lean-ctx's deterministic funnel; images, tool-call blocks and ids pass through untouched, and the output is byte-stable so it stays friendly to provider prompt caching.

from lean_ctx import compress

messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": large_log_or_file_dump},
]

messages = compress(messages, model="claude-sonnet-4")
# → send `messages` to your provider as usual

Works with both OpenAI-style (content: "string") and Anthropic-style (content: [{type: "text", …}, {type: "tool_result", …}]) messages.

Token-savings stats

from lean_ctx import ProxyClient

result = ProxyClient().compress(messages, model="gpt-4o")
print(result.saved_tokens, result.saved_pct)   # e.g. 1840 63.1
messages = result.messages

Configuration

The endpoint and session token are auto-discovered from the running daemon. Every step is overridable:

Setting Env var Default
Proxy URL LEAN_CTX_PROXY_URL http://127.0.0.1:<port>
Proxy port LEAN_CTX_PROXY_PORT config.toml proxy_port, else UID-derived
Session token LEAN_CTX_PROXY_TOKEN <data_dir>/session_token

Or pass them explicitly (useful in CI / against a remote proxy):

compress(messages, base_url="http://127.0.0.1:4444", token="…")

If the daemon is not running, compress() raises LeanCtxConnectionError; an unauthenticated request raises LeanCtxAuthError. Both subclass LeanCtxError.

Framework integrations

LiteLLM

Compress requests transparently with a CustomLogger (pip install lean-ctx-sdk[litellm]):

import litellm
from lean_ctx import LeanCtxLiteLLMHandler

litellm.callbacks = [LeanCtxLiteLLMHandler(model="gpt-4o")]
# every completion now sends compressed messages

For non-proxy code, compress_request_data(data) rewrites the messages of any OpenAI-style request dict in place.

LangChain

from langchain_core.messages import HumanMessage, SystemMessage
from lean_ctx import compress_messages

messages = compress_messages(
    [
        SystemMessage(content="You are a helpful assistant."),
        HumanMessage(content=large_log_or_file_dump),
    ],
    model="gpt-4o",
)

In both adapters a compaction failure never breaks the call — the original messages are kept.

CLI helpers

LeanCtxClient wraps the lean-ctx binary for read / search / shell / gain / benchmark. The LeanCtxRetriever (LangChain) and LeanCtxNodeParser (LlamaIndex) retrieval adapters are available via the langchain / llamaindex extras.

Learn more

License

MIT

Release files for lean-ctx-sdk 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lean-ctx-sdk 0.3.0
File Size Uploaded
lean_ctx_sdk-0.3.0.tar.gz 14.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lean-ctx-sdk 0.3.0
File Interpreter ABI Platform
lean_ctx_sdk-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 27.1 kB

Release files / lean_ctx_sdk-0.3.0.tar.gz

Download URL lean_ctx_sdk-0.3.0.tar.gz
Size 14.8 kB
Tags Source
SHA-256 checksum
How to use checksums
bde30dc49394b0e0ebb2d3e94ef3c57a4be36212e652abae32f0672e3c7025c4
BLAKE2b-256 checksum
How to use checksums
bdf16d25ed3e80dc9bfe480eb2c1eea750431597a94eaa7662582be9e1dce4e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.6

Release files / lean_ctx_sdk-0.3.0-py3-none-any.whl

Download URL lean_ctx_sdk-0.3.0-py3-none-any.whl
Size 12.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0e5489a8dc8270f1cbd45fcffdaa16e08007a8cadd5d7d989eb86caa847219fa
BLAKE2b-256 checksum
How to use checksums
70e5cbd2d8bbc20e8f8a8cb46f992b5f9b2949d461841d5de4afbe52b8ba7f67
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.6

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page