jia-gao/leanctx

Drop-in prompt compression for production LLM apps. Cut your token bill 40-60% without changing your code. Python SDK, LLMLingua-2, MIT.

What it solves

leanctx は、既存のアプリケーションコードを変更することなく、LLM の入力トークンコストを 40-60% 削減するように設計された、ドロップイン形式のプロンプト圧縮ライブラリです。特に、プロバイダーレベルのプロンプトキャッシュでは最適化できない、チャット履歴、新しく取得された RAG ドキュメント、ツール出力などの動的なクエリごとのコンテンツを対象としています。

How it works

このライブラリは、既存の LLM SDKs (OpenAI, Anthropic, Gemini) のラッパーとして機能します。リクエストをインターセプトし、設定可能な圧縮パイプラインを通過させます:

  1. Classification: It identifies content types (e.g., code, error logs, prose).
  2. Routing: Based on configuration, it routes content to different compressors:
    • Verbatim: Preserves critical data (code, stack traces, tool calls) byte-identically.
    1. Lingua: Uses a local LLMLingua-2 model to compress prose at zero marginal cost.
    • SelfLLM: Uses a cheap LLM (like GPT-4o-mini) to summarize content.
  3. Execution: The compressed prompt is then sent to the actual provider API.

Who it’s for

本番環境の LLM アプリケーションを構築している開発者、特に以下のような理由で高いトークン料金が発生している開発者向けです:

  • 大量の取得ドキュメントを含む RAG アプリケーション。
  • 長時間稼働する対話型エージェント (例: LangChain や CrewAI を使用)。
  • ドキュメント処理パイプライン。
  • 広範なツール呼び出し履歴を持つコーディングエージェント。

Highlights

  • Drop-in Integration: Replaces standard SDK imports with leanctx wrappers for minimal code changes.
  • Local Compression: Supports LLMLingua-2 for local, private 圧縮が可能です。データをサードパーティの圧縮 API に送信することはありません。
  • Content-Aware: Programmatically ensures that code and tool-call payloads remain untouched while compressing logs and prose.
  • Observability: Integrated OpenTelemetry support for tracking token savings, costs, and latency.
  • Flexible Deployment: Available as a Python library or an HTTP sidecar for non-Python stacks.