Skip to main content

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.

Metadata

Release files for contexthub 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for contexthub 0.1.0
File Size Uploaded Provenance Actions
contexthub-0.1.0.tar.gz 11.6 kB Without provenance Details for contexthub-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for contexthub 0.1.0
File Interpreter ABI Platform Actions
contexthub-0.1.0-py3-none-any.whl Python 3 none any Details for contexthub-0.1.0-py3-none-any.whl

Total release size: 22.2 kB

Release files / contexthub-0.1.0.tar.gz

Download URL contexthub-0.1.0.tar.gz
Size 11.6 kB
Tags Source
SHA-256 checksum
How to use checksums
50012cb26178ad024a539c04a5769ecf2290f6aa71f719ac92d2b9b5bee82446
BLAKE2b-256 checksum
How to use checksums
9a795f7b357c327e6e49dad92dcf49f197be0fad8525413b44078602c0f66415
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.11

Release files / contexthub-0.1.0-py3-none-any.whl

Download URL contexthub-0.1.0-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
64e1099a2025061a1f4f1f87f733873b92568129f0e15b7a499602d415219962
BLAKE2b-256 checksum
How to use checksums
2be99977b04ec17569c7536bfc54b2810f0b14b554ebde6e2663b2951037de07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.11

Security for contexthub 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page