leanctx
A Python library that acts as a drop-in replacement for LLM provider clients such as OpenAI, Anthropic or Gemini, compressing prompts to cut token costs while treating code and structured data as untouchable.
leanctx is a drop-in prompt compression library for production LLM applications, used by swapping the import for a provider's own client, such as OpenAI's, for leanctx's version so requests are compressed automatically without other code changes.
It classifies each part of a prompt by how much distortion it can tolerate: code, stack traces and structured tool-call data are kept verbatim; documentation, retrieved passages, logs and prior turns are compressed on-device with LLMLingua-2; and low-confidence prose can optionally be compressed with a further LLM call. On the LongBench v2 benchmark, layered on top of an existing compressor, it removes an additional 18.7% of tokens on average, rising to 36.7% on prose-heavy traffic, at a cost of 1.8 percentage points of accuracy.
It is open source, MIT licensed, and runs locally, so prompts and user data stay on the caller's own infrastructure by default.