Skip to main content

token-overflow

Prevent LLM context window overflow. Count tokens, enforce budgets, and smart-truncate prompts before they silently fail.

The Problem

LLM APIs silently truncate or error when you exceed context windows. You don't know until you get garbage output or a 400 error. token-overflow catches this before the API call.

Install

pip install token-overflow

Quick Start

from token_overflow import TokenGuard, fit

# Simple: fit text into a budget
short = fit("very long document...", max_tokens=1000)

# Guard a full prompt
guard = TokenGuard(model="gpt-4")  # auto-detects 8192 limit

messages = [
    {"role": "system", "content": system_prompt},
    {"role": "user", "content": huge_document},
]

# Check before calling API
if guard.would_overflow(messages):
    messages = guard.truncate(messages, strategy="tail")

# Or one-liner: safe messages guaranteed to fit
safe = guard.safe(messages)

Token Counting

from token_overflow import count_tokens, count_messages

# Count tokens in text
n = count_tokens("Hello world", model="gpt-4")  # 2

# Count tokens in chat messages
n = count_messages([
    {"role": "system", "content": "You are helpful."},
    {"role": "user", "content": "Hi"},
], model="gpt-4")

Truncation Strategies

guard = TokenGuard(model="gpt-4")

# Keep the tail (most recent), trim from head
guard.truncate(messages, strategy="tail")

# Keep the head (system prompt), trim from tail
guard.truncate(messages, strategy="head")

# Keep head and tail, trim middle
guard.truncate(messages, strategy="middle")

# Custom reserve: keep 500 tokens for response
guard.truncate(messages, strategy="tail", reserve=500)

Model Limits

from token_overflow import get_limit

get_limit("gpt-4")           # 8192
get_limit("gpt-4-turbo")     # 128000
get_limit("gpt-3.5-turbo")   # 16385
get_limit("claude-3-opus")   # 200000
get_limit("claude-3-sonnet") # 200000

Features

  • Zero config: Knows context limits for 30+ models
  • Multiple strategies: head, tail, middle truncation
  • Message-aware: Understands chat message format, preserves system prompts
  • Reserve tokens: Leave room for the response
  • Fast counting: Uses tiktoken when available, falls back to approximation
  • No dependencies required: tiktoken optional for exact counts

License

MIT

Metadata

Release files for token-overflow 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 token-overflow 0.1.0
File Size Uploaded Provenance
token_overflow-0.1.0.tar.gz 5.7 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 12.1 kB

Release files / token_overflow-0.1.0.tar.gz

Download URL token_overflow-0.1.0.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
13179351749aeb535d3a9ce9a2bbf27d157af5876b2a69d21fe73f4fbfef1f7b
BLAKE2b-256 checksum
How to use checksums
7e31e594fe039421c2702258b0dd940bb67e35d2eab07517f30113afd9290b5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.8

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

Download URL token_overflow-0.1.0-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
24b6ada14460c2a0d1aee1d5b567005b485e8ea6b46fe5c56eaa1ad062ca1d1c
BLAKE2b-256 checksum
How to use checksums
ec61d274b56aa813f1055e80a89dd7c880930fb138cd54cb212bf81fc4096c00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.8

Security for token-overflow 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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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