leanctx
by jia-gao
leanctx is a Python library that shortens the text you send to AI models, so each request uses fewer tokens and costs less. It works as a replacement for the OpenAI, Anthropic or Gemini client and leaves code, JSON and tool calls untouched while compressing prose.
- Stars
- 321
- Licence
- MIT
- Runs on
- Your computer
- Setup
- Moderate
- Language
- Python
- Age
- 194 days
What you can do with it
- Swap in the leanctx client for the OpenAI, Anthropic or Gemini client to compress long prompts automatically
- Shorten retrieved documents, logs and chat history in RAG apps and agents before they reach the model
- Check how many tokens each request saved using the usage figures returned with every response
Make money with it
A freelancer could offer an LLM cost-reduction audit for small companies, using leanctx to cut their API spend and charging a fee or a share of the savings.
An idea, not financial advice. Check the licence before charging for anything built on it.
Ways a business could use it
- Lower the monthly API bill of a customer-facing chatbot or document-processing tool by compressing long inputs
- Keep a long-running support or coding agent affordable as its conversation history grows
Can you use it commercially?
Free to use, change and sell, including in paid products. Keep the copyright notice.
Not legal advice. Read the project's licence file before building a product on it.
Getting it running
Install with pip (for example pip install 'leanctx[openai,lingua]'), then change your import to use the leanctx client; the first run downloads about 1.2 GB of model weights.
Follow the official instructions in the project README.
Things to check
- Compression costs some accuracy (1.8 percentage points overall in the benchmark, and more on some task types)
- You still need a paid API key from your model provider
- Runs a roughly 1.2 GB model locally; a GPU is advised for low latency
- Savings claims vary between the headline (40-60%) and the README body (10-40%), so test on your own traffic
Who's talking about it
Checked 29 Oct 2026. Summary drafted by AI from the project's README.