Context window management utilities for LLM-based applications
Project description
harnessutils
Python library for managing LLM context windows in long-running conversations. Enables indefinite conversation length while staying within token limits.
Installation
uv add harness-utils
Features
- Three-tier context management - Truncation, pruning, and LLM-powered summarization
- Turn processing - Stream event handling with hooks and doom loop detection
- Pluggable storage - Filesystem and in-memory backends
- Zero dependencies - No external runtime requirements
- Type-safe - Full Python 3.12+ type hints
Quick Start
from harnessutils import ConversationManager, Message, TextPart, generate_id
manager = ConversationManager()
conv = manager.create_conversation()
# Add message
msg = Message(id=generate_id("msg"), role="user")
msg.add_part(TextPart(text="Help me debug"))
manager.add_message(conv.id, msg)
# Prune old outputs
manager.prune_before_turn(conv.id)
# Get messages for LLM
model_messages = manager.to_model_format(conv.id)
Context Management
Three tiers handle context overflow:
1. Truncation - Limits tool output size (instant, free)
output = manager.truncate_tool_output(large_output, "tool_name")
2. Pruning - Removes old tool outputs (fast, ~50ms)
result = manager.prune_before_turn(conv.id)
# Keeps recent 40K tokens, removes older outputs
3. Summarization - LLM compression when needed (slow, ~3-5s)
if manager.needs_compaction(conv.id, usage):
manager.compact(conv.id, llm_client, parent_msg_id)
Turn Processing
Process streaming LLM responses with hooks:
from harnessutils import TurnProcessor, TurnHooks
hooks = TurnHooks(
on_tool_call=execute_tool,
on_doom_loop=handle_loop,
)
processor = TurnProcessor(message, hooks)
for event in llm_stream:
processor.process_stream_event(event)
Includes:
- Tool state machine
- Doom loop detection (3 identical calls)
- Snapshot tracking
Configuration
from harnessutils import HarnessConfig
config = HarnessConfig()
config.truncation.max_lines = 2000
config.pruning.prune_protect = 40_000 # Keep recent 40K tokens
config.model_limits.default_context_limit = 200_000
Storage
from harnessutils import FilesystemStorage, MemoryStorage
# Filesystem (production)
storage = FilesystemStorage(config.storage)
# In-memory (testing)
storage = MemoryStorage()
# Custom (implement StorageBackend protocol)
# See examples/custom_storage_example.py
storage = YourCustomStorage()
Examples
basic_usage.py- Simple conversationollama_example.py- Ollama integrationollama_with_summarization.py- Full three-tier demoturn_processing_example.py- Stream processingcustom_storage_example.py- Custom storage adapter (SQLite)
Development
uv sync # Install deps
uv run pytest # Run unit tests
uv run mypy src/ # Type check
uv run python -m evals.runner # Run evals (quality, budget, performance)
Evals test real-world behavior beyond unit tests:
- Information preservation after compaction
- Token budget compliance
- Performance benchmarks (latency, throughput)
See evals/README.md for details.
License
MIT License - see LICENSE 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 harness_utils-0.1.5.tar.gz.
File metadata
- Download URL: harness_utils-0.1.5.tar.gz
- Upload date:
- Size: 219.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
93074c32eeb751dcedcf8ecbab863295777b5784e06ed47cec5259d8b4dc9923
|
|
| MD5 |
b85e2e12cc21525df3f023dab542db52
|
|
| BLAKE2b-256 |
6ca294f0d3e03e3672074e4590da7db4d83aab417f792ca07370b24eefb52d67
|
File details
Details for the file harness_utils-0.1.5-py3-none-any.whl.
File metadata
- Download URL: harness_utils-0.1.5-py3-none-any.whl
- Upload date:
- Size: 28.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43c4e15a485e3e02859e32c89509ca3fa0b0a7f2bdf4ee617de4495906704b15
|
|
| MD5 |
b9bbc75468c82ff705e305992d188463
|
|
| BLAKE2b-256 |
0a4de591d3547dbea22fc30bf63e8324590d78204219571ae6af5e424129a6f9
|