Skip to main content

A lightweight context management library for LLM applications

Project description

ContextHub

A lightweight Python library for managing LLM context windows. It helps structure context items, estimate token usage, prioritize what stays when budget is limited, and split long text into smaller chunks.

Features

  • Context window management with token budgets
  • Priority-aware context selection (CRITICAL, HIGH, MEDIUM, LOW)
  • Chunking utilities — by tokens, sentences, paragraphs, or custom separator
  • Zero-dependency default install with optional tiktoken integration
  • OpenAI-compatible message format export

Installation

pip install contexthub

Optional tokenizer support for accurate token counting:

pip install "contexthub[tiktoken]"

Quick Start

Build a prioritized context window

from contexthub import ContextWindow, Priority

window = ContextWindow(max_tokens=50)

window.add("You are a precise assistant.", priority=Priority.CRITICAL, role="system")
window.add("Summarize this document in 3 bullet points.", priority=Priority.HIGH, role="user")
window.add("Previous conversation detail that can be dropped first.", priority=Priority.LOW)

selected_items = window.build()
messages = window.to_messages()

print([item.priority.name for item in selected_items])
print(messages)

Chunk long text

from contexthub import chunk_by_tokens, chunk_by_sentences, chunk_by_paragraphs, chunk_by_separator

text = "This is sentence one. This is sentence two. This is sentence three."

print(chunk_by_sentences(text, max_sentences=2))
print(chunk_by_tokens(text, max_tokens=10, overlap=2))
print(chunk_by_paragraphs("Paragraph one.\n\nParagraph two.\n\nParagraph three."))
print(chunk_by_separator("a|b|c", separator="|"))

Utility helpers

from contexthub import count_tokens, trim_to_fit

text = "This is a long context block for a tight budget"
print(count_tokens(text))
print(trim_to_fit(text, max_tokens=6))

API Summary

ContextWindow

  • ContextWindow(max_tokens=4096, tokenizer=None)
    • add(content, priority=Priority.MEDIUM, role="user", metadata=None) — Add a context item
    • build() — Build context fitting within token budget, prioritized
    • to_messages() — Export as OpenAI-compatible message list
    • clear() — Clear all items
    • total_tokens — Current total token count
    • remaining_tokens — Remaining token budget
    • items — All items in the window

Types

  • ContextItem — Dataclass with content, priority, role, metadata, token_count
  • Priority — Enum: CRITICAL, HIGH, MEDIUM, LOW

Chunking

  • chunk_by_tokens(text, max_tokens, overlap=0, tokenizer=None)
  • chunk_by_sentences(text, max_sentences=5)
  • chunk_by_paragraphs(text)
  • chunk_by_separator(text, separator="\n")

Priority Helpers

  • select_by_priority(items, max_tokens, tokenizer=None)
  • trim_to_fit(text, max_tokens, tokenizer=None, suffix="...")

Tokenizer

  • count_tokens(text, model=None)

Development

pip install -e ".[dev]"
python -m pytest tests/ -v
python -m build

License

Apache License 2.0. See LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

contexthub-0.0.2.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

contexthub-0.0.2-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file contexthub-0.0.2.tar.gz.

File metadata

  • Download URL: contexthub-0.0.2.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for contexthub-0.0.2.tar.gz
Algorithm Hash digest
SHA256 692a0b69362b689c9af3b9203348bcd32d8a6978611a4dd61c8258b8bac427bf
MD5 abb2004ef4c40ac8f3d7a0d7cc9041d2
BLAKE2b-256 0c501802518f3c1634cc7bcfb9ae8262cb3221a497f2f8d039af39811b8873be

See more details on using hashes here.

File details

Details for the file contexthub-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: contexthub-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for contexthub-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3bc99e2205378a05a2e9eb68b8afaa9adc141602d390396d727524da0191a027
MD5 89bf9ca3375e46703631376492af0b03
BLAKE2b-256 44a00a6740d7df129a77aa82c0e1765aefa75457efb68c4bf525004c97cda7fd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page