skillfed

openai-messages-token-helper

A helper library for estimating tokens used by messages sent through OpenAI Chat Completions API.

openai-messages-token-helper v0.1.13 86.8K downloads/30d#13,840 on PyPI34
Permissive license AGING released

What it is and what it does

This package wraps OpenAI's tokenization logic to help you stay within token budgets when building chat conversations. It provides functions to count tokens in individual messages, images (for GPT-4 vision), and tool definitions, then uses those counts to automatically truncate old messages from a conversation history so the final message list fits within a model's token limit.

You supply a system prompt, optional few-shot examples, past messages, and a new user message, along with a target token budget and model name. The library calculates the token cost of each component using tiktoken for text and Pillow for image dimensions, then drops old messages from the history until everything fits. It also supports Azure OpenAI model names and can fall back to default limits if a model is unrecognized.

Use it for:

  • Automatically truncate chat history to fit within a model's context window before sending to the OpenAI API.
  • Calculate token costs for images in GPT-4 vision requests to budget vision-enabled conversations.
  • Build few-shot prompts with known token budgets, ensuring examples fit alongside user messages.
  • Estimate API costs by counting tokens before making requests to OpenAI.
  • Handle tool definitions and tool-choice parameters in token calculations for function-calling workflows.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Estimates token usage for OpenAI Chat Completions API messages and builds message lists that fit within model token limits, accounting for text, images, tools, and few-shot examples.

Yes, if you are building applications that use OpenAI's Chat Completions API and need to manage token budgets programmatically. The library is straightforward, has low install friction, carries no known vulnerabilities, and solves a real operational problem—preventing token-limit errors by pre-calculating and truncating conversations. Maintenance is aging but the repository is active; use it with the understanding that updates may be infrequent.

Install

openai-messages-token-helper on PyPI

pip

pip install openai-messages-token-helper

uv

uv add openai-messages-token-helper

poetry

poetry add openai-messages-token-helper

Installing openai-messages-token-helper

Before you install

Low install friction; depends on openai, tiktoken, and pillow. Maintenance status is aging—last commit 2025-12-29, 228 days since release—but the repository is active and not archived. Supports Python 3.9, 3.10, and 3.11.

License in practice

Licensed under MIT (permissive), so you can use, modify, and distribute freely with minimal restrictions.

Quickstart

from openai_messages_token_helper import build_messages

messages = build_messages(
    model="gpt-4",
    system_prompt="You are a helpful assistant.",
    new_user_content="Hello",
    past_messages=[]
)

Requires openai and tiktoken to be installed; model name must be recognized or fallback_to_default must be True.

Verify before relying

  • Whether the package handles all current OpenAI model variants or only a fixed set.
  • Performance characteristics when processing large message histories or many images in a single call.
  • Whether token counts match OpenAI's actual billing exactly or are estimates.

Package facts

License not declared (permissive)
Python support supports the current Python release (>=3.9)
Install friction low — pure-Python wheel
Runtime dependencies 3 — openai, tiktoken, pillow
Maintenance aging — 228 days since the last release
Last repo commit
First released
Downloads 86,761/month — #13,840 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: openai_messages_token_helper-0.1.13-py3-none-any.whl

Framework :: PytestLicense :: OSI Approved :: MIT LicenseProgramming Language :: PythonProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.9

Tags

openai token countinggpt message token estimationchat completion token limitsopenai api token calculatormessage history truncationgpt-4 vision token costtoken budget management
openai-integrationtoken-accountingllm-utilities

More Artificial Intelligence packages

litellm

LiteLLM provides a unified Python interface to…

permissive · top 100 on PyPI

huggingface-hub

Client library and CLI tool for downloading,…

permissive · top 100 on PyPI

langchain

LangChain provides a framework for building…

permissive · top 1,000 on PyPI

hf-xet

hf-xet provides chunk-based deduplication and…

permissive · top 1,000 on PyPI

tokenizers

Tokenizers converts raw text into token…

permissive · top 1,000 on PyPI

transformers

Transformers provides a unified framework for…

permissive · top 1,000 on PyPI

tokencost

Counts tokens and estimates USD costs for LLM…

permissive · top 15,000 on PyPI

tiktoken

tiktoken is a fast BPE tokenizer that converts…

permissive · top 1,000 on PyPI

openai-harmony

Renders and parses the Harmony response format…

unclear · top 5,000 on PyPI

agent-framework-openai

Provides OpenAI and Azure OpenAI integration…

permissive · top 5,000 on PyPI

openai

Provides a Python client library for the OpenAI…

permissive · top 1,000 on PyPI

opentelemetry-instrumentation-openai-v2

Automatically traces and logs OpenAI API calls…

permissive · top 5,000 on PyPI

llama-index-llms-openai

Integrates OpenAI's language models into…

permissive · top 1,000 on PyPI

g4f

Aggregates multiple LLM and media-generation…

unclear · top 15,000 on PyPI

summarization-pydantic-ai

Manages conversation history for Pydantic AI…

permissive · top 15,000 on PyPI

langchain-nvidia-ai-endpoints

Integrates NVIDIA AI Foundation Models and chat…

permissive · top 5,000 on PyPI