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.1.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.1-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for contexthub-0.0.1.tar.gz
Algorithm Hash digest
SHA256 1e15dab9671fe96f3180e9b9f51a74e45310f3f13453e759c81afe471ee3b142
MD5 90230445476c618d067193d055ec71cd
BLAKE2b-256 99e11a7067460c7154418504ccfb319559dbd9667fca9f1c1cb8da6c9707238f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: contexthub-0.0.1-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.13.11

File hashes

Hashes for contexthub-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e778bd4baf13f597bde175362e5ea8dd1e148e5dbe09c27a48bf656af871512f
MD5 5bae4e68945402e3b43d6caf030e2565
BLAKE2b-256 ea5aa15aa39c99141ea3d9f8da186aeac50c58987a4ad49c0aa08fbbb57281fd

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