Skip to main content

A tiny, dependency-free tool belt for LLMs: token counting, cost estimation, retries, prompt templates, and text chunking.

Project description

llmbelt 🧰

A tiny, zero-dependency tool belt for working with LLMs. The small utilities you end up re-writing on every project — token counting, cost estimation, retries, prompt templates, and text chunking — in one clean import.

CI PyPI Python License: MIT

  • 🪶 Zero required dependencies — pure standard library.
  • 🔌 Provider-agnostic — works with Anthropic, OpenAI, Gemini, or anything else.
  • 🧪 Fully tested across Python 3.9–3.12.

Install

pip install llmbelt

# Optional: exact token counts for OpenAI-family models
pip install "llmbelt[tiktoken]"

Usage

Count tokens

from llmbelt import count_tokens, truncate_to_tokens

count_tokens("Hello, world!")              # exact if tiktoken installed, else estimated
count_tokens("Hello", model="gpt-4o")      # use a model-specific encoding

# Trim text to fit a budget (great before sending context to an API)
truncate_to_tokens(long_document, max_tokens=4000)

Estimate cost

from llmbelt import estimate_cost, Price

estimate_cost(input_tokens=1_500, output_tokens=800, model="gpt-4o-mini")   # -> USD

# Bring your own prices (the built-in table is approximate — always verify):
my_prices = {"my-model": Price(input_per_1m=2.0, output_per_1m=6.0)}
estimate_cost(1000, 500, "my-model", pricing=my_prices)

Retry with backoff

from llmbelt import retry

@retry(attempts=5, exceptions=(ConnectionError, TimeoutError))
def call_api():
    ...   # retried with exponential backoff + jitter on failure

Prompt templates

from llmbelt import PromptTemplate

t = PromptTemplate("Translate {text} into {language}.")
t.render(text="hello", language="French")   # "Translate hello into French."
t.render(text="hello")                      # KeyError: Missing template variables: ['language']

Chunk text for RAG

from llmbelt import chunk_text

chunks = chunk_text(document, chunk_size=1000, overlap=100)
# overlapping chunks so answers aren't split across a boundary

API reference

Function Description
count_tokens(text, model=None) Exact (tiktoken) or estimated token count
estimate_tokens(text) Dependency-free heuristic count
truncate_to_tokens(text, max_tokens, model=None) Trim text to a token budget
estimate_cost(input_tokens, output_tokens, model, pricing=None) USD cost estimate
retry(attempts, base_delay, backoff, jitter, exceptions, ...) Backoff retry decorator
PromptTemplate(template) Templating with missing-variable validation
chunk_text(text, chunk_size, overlap) Overlapping text chunks

Development

git clone https://github.com/YoungAlpaccino/llmbelt
cd llmbelt
pip install -e ".[dev]"
pytest          # run tests
ruff check .    # lint

Publishing to PyPI (maintainer notes)

python -m build
twine upload dist/*

Before first publish: confirm the name llmbelt is free on PyPI. If taken, rename in pyproject.toml, the src/ folder, and imports (a single find-and-replace).


License

MIT — see LICENSE. Use it anywhere, including commercially.

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

llmbelt-0.1.0.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

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

llmbelt-0.1.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file llmbelt-0.1.0.tar.gz.

File metadata

  • Download URL: llmbelt-0.1.0.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for llmbelt-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2a751767b18d4c4cc48d13c8c2f5694aa6adf2f51c5180113aa0ae1705090d65
MD5 3666d038ceada39b26ae744d8a4829ba
BLAKE2b-256 983525ce4d76f3301884ab33ce2dfdd0c4e67c4d5ffd45b2a846627245c1c5d7

See more details on using hashes here.

File details

Details for the file llmbelt-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: llmbelt-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for llmbelt-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a6e792571ca833d5dcb0ebb1bec233b6f2109241cb4d89d754031746bf1a3d0d
MD5 322161c2a32c0325b6b38d97832777fe
BLAKE2b-256 fe44a9b0d742f39a38c390742fdf4df2d3e530509334b61670ba387ed2ab0da8

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