Getting Started
Install, compress, and focus the context on the question your agent is answering.
Install
Install the Python package with pip. Optional extras enable exact token counts.
pip install llmslim
# Optional exact token counter
pip install 'llmslim[fast-tokens]'Compress text
The GENERAL role preserves the legacy compress(text) behavior.
from llmslim import compress
result = compress(long_context, target_ratio=0.5)
print(result.compressed_text)
print(result.token_counter_used)Compress around a question
Pass the question your agent is answering and LLMSlim 0.8 ranks chat rounds, documents, paragraphs or log lines against it, keeps the best ones whole and in their original order, and fits them into the budget. Logs, tracebacks and code keep their line structure.
from llmslim import compress
result = compress(chat_history, target_ratio=0.25, query="Where should the replacement card be shipped?")
print(result.selection) # "focus"
print(result.compressed_text)Sharper retrieval with the reranker
Relevance is lexical by default: no model, deterministic, tens of milliseconds on a 3k-token context. For retrieval and long chat memory, where the answer often shares few words with the question, add the optional 22M-parameter reranker. It also accepts any callable scorer(query, texts), such as a hosted reranking API.
# pip install "llmslim[semantic]"
from llmslim import compress_documents
results = compress_documents(documents, query=question, target_ratio=0.25, relevance="semantic")
context = "\n\n".join(r.compressed_text for r in results)Questions
Does LLMSlim require an LLM provider?
No. Extractive compression runs locally. Rewrite and hybrid strategies require a caller-supplied provider.