Skip to main content

AI Token Counter

Accurate token counting for multiple LLM providers.

Features

  • Multi-Provider Support: OpenAI (GPT-4, GPT-3.5, GPT-4o, o1), Anthropic (Claude 3, 3.5, 3.7), Google (Gemini), Meta (Llama 2, 3, 3.1, 3.2, 3.3), Mistral, DeepSeek, Cohere
  • Cost Estimation: Built-in pricing for accurate cost calculations
  • Message Support: Count tokens in chat messages with proper overhead
  • Simple API: Easy-to-use functions for quick counting

Installation

pip install ai-token-counter

Quick Start

from ai_token_counter import count_tokens, estimate_cost

# Count tokens in text
count = count_tokens("Hello, world!", model="gpt-4o")
print(f"Tokens: {count}")

# Estimate cost
cost = estimate_cost(input_tokens=1000, output_tokens=500, model="gpt-4o")
print(f"Cost: ${cost.total_cost_usd:.6f}")

# Count chat messages
messages = [
    {"role": "user", "content": "Hello!"},
    {"role": "assistant", "content": "Hi there!"}
]
from ai_token_counter import count_message_tokens
result = count_message_tokens(messages, model="claude-3-5-sonnet")
print(f"Message tokens: {result.count}")

Supported Models

OpenAI

  • gpt-4o, gpt-4o-mini, gpt-4-turbo, gpt-4, gpt-3.5-turbo
  • o1-preview, o1-mini

Anthropic

  • claude-3-7-sonnet, claude-3-5-sonnet, claude-3-5-haiku
  • claude-3-opus, claude-3-sonnet, claude-3-haiku

Google

  • gemini-2.0-flash-exp, gemini-1.5-pro, gemini-1.5-flash
  • gemini-1.0-pro

Meta

  • llama-3.3-70b, llama-3.1-405b, llama-3.1-70b
  • llama-3-70b, llama-3-8b, llama-2-70b

Mistral

  • mistral-large, mistral-medium, mistral-small
  • mixtral-8x7b, mixtral-8x22b, codestral

DeepSeek

  • deepseek-chat, deepseek-coder, deepseek-reasoner

Cohere

  • command-r-plus, command-r, command

API Reference

TokenCounter Class

from ai_token_counter import TokenCounter

counter = TokenCounter(model="gpt-4o")

# Count text
result = counter.count("Your text here")
print(result.count)  # Token count
print(result.provider)  # Provider
print(result.estimated)  # Whether estimated

# Count messages
messages = [{"role": "user", "content": "Hello"}]
result = counter.count(messages=messages)

# Estimate cost
cost = counter.estimate_cost(input_tokens=1000, output_tokens=500)
print(cost.total_cost_usd)

# Get pricing info
pricing = counter.get_pricing_info("gpt-4o")

Convenience Functions

from ai_token_counter import (
    count_tokens,
    count_string_tokens,
    count_message_tokens,
    estimate_cost,
    count_conversation_tokens
)

# Quick count
count = count_tokens("text", model="gpt-4o")

# Full result
result = count_string_tokens("text", model="claude-3-opus")

# Messages
result = count_message_tokens(messages, model="gemini-1.5-pro")

# Cost
cost = estimate_cost(1000, 500, model="llama-3-70b")

# Conversation breakdown
breakdown = count_conversation_tokens(messages)
for msg in breakdown["messages"]:
    print(f"Message {msg['index']} ({msg['role']}): {msg['tokens']} tokens")
print(f"Total: {breakdown['total_tokens']} tokens")

License

MIT License - see LICENSE file for details.

Metadata

Release files for ai-token-counter 1.0.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 ai-token-counter 1.0.0
File Size Uploaded Provenance Actions
ai_token_counter-1.0.0.tar.gz 12.9 kB Without provenance Details for ai_token_counter-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ai-token-counter 1.0.0
File Interpreter ABI Platform Actions
ai_token_counter-1.0.0-py3-none-any.whl Python 3 none any Details for ai_token_counter-1.0.0-py3-none-any.whl

Total release size: 23.2 kB

Release files / ai_token_counter-1.0.0.tar.gz

Download URL ai_token_counter-1.0.0.tar.gz
Size 12.9 kB
Tags Source
SHA-256 checksum
How to use checksums
610925471e6349ba0c39f8c430adfc6ff412d0938a6b31ff780b3cab063f8910
BLAKE2b-256 checksum
How to use checksums
d7ef9d11ed558401f12f2bee43736633bed325009de39ed0fe278d07830ce4b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release files / ai_token_counter-1.0.0-py3-none-any.whl

Download URL ai_token_counter-1.0.0-py3-none-any.whl
Size 10.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9a75aabadb88875c7acddaa0a06900e497253f733ac7e23cb124e06cd2946199
BLAKE2b-256 checksum
How to use checksums
a32030c050166e1d80ad8eb80bdcd51d9e610cb5c6bc364a5ff5b8f89f912351
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Security for ai-token-counter 1.0.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.

Release history Release notifications | RSS feed

This release

1.0.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