Lightweight, framework-agnostic Python library for intelligent LLM context window management and memory heap packing.
Project description
context-clipper ✂️
context-clipper is a lightweight, framework-agnostic Python library designed to solve LLM context window overflow and "lost in the middle" retrieval degradation.
Unlike naive sliding windows (messages[-10:]) that blindly drop critical system prompts or early user context, context-clipper treats the conversation window as a prioritized memory heap to intelligently shed low-priority noise or summarize intermediate turns without pulling in heavy frameworks like LangChain, LlamaIndex, or Pydantic.
✨ Why context-clipper?
- 🪶 Zero Bloat: Built using 100% pure Python standard libraries (
dataclasses,heapq,functools). No heavy framework dependencies. - 🎯 Accurate Token Counting: Uses
tiktokenwhen available (including ~4 tokens/message framing overhead) with graceful fallback to character heuristics. - 🔌 Standard API Compatibility: Accepts and returns standard Python lists of dictionaries matching OpenAI and Anthropic message formats.
- 🧠 3-Phase Waterfall Engine:
- Total Protection: Guarantees all
sticky=Truemessages (e.g., system instructions, core rules) are preserved. - Priority-Based Shedding: Uses a min-heap to drop lowest-priority items first (e.g., failed web searches or debug logs with
priority=0). - Rolling Middle-Summarization: Condenses intermediate conversational turns into a single summary message via your own custom LLM callback while preserving active recent tail turns.
- Total Protection: Guarantees all
📦 Installation
Install directly via pip:
pip install context-clipper
To include tiktoken for tokenizer-accurate counting:
pip install context-clipper[tiktoken]
🚀 Quickstart
Basic Priority Shedding
from context_clipper import pack
messages = [
# System instructions are sticky and cannot be dropped
{"role": "system", "content": "You are a helpful coding assistant.", "sticky": True, "priority": 100},
{"role": "user", "content": "How do I reverse a list in Python?", "priority": 1},
{"role": "assistant", "content": "You can use list.reverse() or [::-1].", "priority": 1},
# Low-priority tool error / noise that should be dropped first when budget is tight
{"role": "tool", "content": "Error 500: Search timeout", "priority": 0, "tool_call_id": "call_1"},
{"role": "user", "content": "Thanks! Now explain generator expressions.", "priority": 2}
]
# Pack the conversation into a 150-token window
packed_messages = pack(messages, max_tokens=150)
Rolling Middle-Summarization with Custom Callback
Pass any summarizer callback (OpenAI, Anthropic, local Ollama, etc.) to condense intermediate turns while keeping sticky prompts and recent turns intact:
from context_clipper import Clipper
def my_llm_summarizer(text: str) -> str:
# Call your preferred LLM provider here
return "User asked about list reversal; assistant provided reverse() and slice methods."
clipper = Clipper(max_tokens=150, summarizer_cb=my_llm_summarizer)
packed_messages = clipper.pack(messages)
print(packed_messages[1])
# {
# "role": "system",
# "content": "[Summary of older conversation: User asked about list reversal; assistant provided reverse() and slice methods.]"
# }
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file context_clipper-0.1.0.tar.gz.
File metadata
- Download URL: context_clipper-0.1.0.tar.gz
- Upload date:
- Size: 12.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6e79f32d9c4710ab2b85c69f6647b6d3e800f61ef186a7c7e6aa87d634f5e620
|
|
| MD5 |
c5521bd0ad519cce5369bd304eb1431f
|
|
| BLAKE2b-256 |
458c17dd2b780be351a04abbda50489ba768ada9826fd06dc72807ce107b7999
|
File details
Details for the file context_clipper-0.1.0-py3-none-any.whl.
File metadata
- Download URL: context_clipper-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8134b993b3f5d1ca4644578308ccff328c496feace8d551e2983587358e56039
|
|
| MD5 |
f02d3420a2623967824b295063a893f0
|
|
| BLAKE2b-256 |
7df9d3954b4405f744a08d9eff2da052eb99ae497b483569571cd11c75863d49
|