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.
    • 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 compression that doesn't send data to third-party compression APIs.
  • 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.