{"id":21643,"library":"openai-messages-token-helper","title":"OpenAI Messages Token Helper","description":"A helper library for estimating token counts for OpenAI Chat Completions messages. Currently at version 0.1.13, it supports GPT-3.5, GPT-4, and GPT-4-turbo models. Active development with monthly releases.","status":"active","version":"0.1.13","language":"python","source_language":"en","source_url":"https://github.com/microsoft/openai-messages-token-helper","tags":["openai","token-estimation","azure-openai"],"install":[{"cmd":"pip install openai-messages-token-helper","lang":"bash","label":"PyPI"}],"dependencies":[],"imports":[{"note":"Module name uses underscores, not hyphens.","wrong":"from openai_messages_token_helpers import ...","symbol":"messages_to_tokens","correct":"from openai_messages_token_helper import messages_to_tokens"},{"note":"Function is plural: build_messages.","wrong":"from openai_messages_token_helper import build_message","symbol":"build_messages","correct":"from openai_messages_token_helper import build_messages"}],"quickstart":{"code":"import os\nfrom openai import AzureOpenAI\nfrom openai_messages_token_helper import messages_to_tokens\n\nclient = AzureOpenAI(\n    api_key=os.environ.get('AZURE_OPENAI_KEY', ''),\n    api_version=\"2024-02-01\",\n    azure_endpoint=os.environ.get('AZURE_OPENAI_ENDPOINT', '')\n)\nsystem_message = \"You are a helpful assistant.\"\nuser_message = \"Hello!\"\ntokens = messages_to_tokens([\n    {\"role\": \"system\", \"content\": system_message},\n    {\"role\": \"user\", \"content\": user_message}\n], model=\"gpt-4\")\nprint(f\"Token count: {tokens}\")","lang":"python","description":"Quick estimate tokens for a list of messages."},"warnings":[{"fix":"Use for estimation only; always handle token limit errors in your code.","message":"The token count is an approximation and may differ from actual API usage. Do not rely on exact counts for billing or critical limits.","severity":"gotcha","affected_versions":"all"},{"fix":"Always specify the correct model string. Check OpenAI documentation for supported model names.","message":"Model parameter must exactly match the model identifier used in the API (e.g., 'gpt-4', 'gpt-3.5-turbo'). Using an unrecognized model may fall back to a default tokenizer and give inaccurate results.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"pip install openai-messages-token-helper  # and import as from openai_messages_token_helper import ...","cause":"Package installed as 'openai-messages-token-helper' but import requires underscores.","error":"ModuleNotFoundError: No module named 'openai_messages_token_helper'"},{"fix":"messages_to_tokens(messages, model=\"gpt-4\")","cause":"The 'model' argument is required but was omitted.","error":"TypeError: messages_to_tokens() missing 1 required positional argument: 'model'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}