Token Counting API
Free REST API for counting tokens across all major AI models. No authentication required. CORS enabled.
GET / POST
/api/count-tokensCount tokens and estimate costs for any supported AI model. Use POST for larger payloads.
Parameters
Available Models
gpt-5.4gpt-5.4-minigpt-5.4-nanogpt-5.4-progpt-5.2gpt-5.2-progpt-5.1gpt-5gpt-5-minigpt-5-nanogpt-5-progpt-4-1gpt-4-1-minigpt-4-1-nanogpt-4ogpt-4o-minio1o1-proo3o3-proo4-minio3-minio1-minigpt-3-5-turboclaude-opus-4-7claude-opus-4-6claude-opus-4-5claude-opus-4-1claude-opus-4claude-sonnet-4-6claude-sonnet-4-5claude-sonnet-4claude-sonnet-3-7claude-haiku-4-5claude-haiku-3-5claude-opus-3claude-3-haikugemini-3-1-progemini-3-1-flash-litegemini-3-flashgemini-2-5-progemini-2-5-flashgemini-2-5-flash-litegemini-2-0-flashgemini-1-5-progemini-1-5-flashdeepseek-v3deepseek-r1llama-4-scoutllama-4-maverickllama-3-3-70bmistral-large-latestpixtral-large-latestmistral-small-latestcodestral-latestministral-8b-latestministral-3b-latestmistral-nemopixtral-12b-2409sonar-prosonar-largesonar-smallsonar-hugegrok-4-3grok-4-20grok-4-1-fastqwen-3-7-maxqwen-3-5-plusqwen-2-5-72bExample Request
# 1. GET Request (For small payloads)
curl "https://tokencalculator.vercel.app/api/count-tokens?text=Hello%20world&model=gpt-4o"
# 2. POST Request (Recommended for code / large docs)
curl -X POST "https://tokencalculator.vercel.app/api/count-tokens" \
-H "Content-Type: application/json" \
-d '{"text": "def compute_loss(): pass", "model": "gpt-4o"}'
# JavaScript / Node.js
fetch('https://tokencalculator.vercel.app/api/count-tokens', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: 'Hello world', model: 'gpt-4o' })
})
.then(res => res.json())
.then(data => console.log(data));
# Python
import requests
resp = requests.post(
'https://tokencalculator.vercel.app/api/count-tokens',
json={'text': 'Hello world', 'model': 'gpt-4o'}
)
print(resp.json())Example Response
{
"model": {
"id": "gpt-4o",
"name": "GPT-4o",
"provider": "OpenAI",
"encoding": "o200k_base"
},
"counts": {
"tokens": 14,
"words": 10,
"characters": 56
},
"cost": {
"inputCost": "$0.000035",
"outputCost": "$0.000140",
"inputPricePerMillion": 2.5,
"outputPricePerMillion": 10
},
"contextWindow": 128000,
"timestamp": "2026-03-31T00:00:00.000Z"
}Rate Limits & Notes
- Payload Limit (POST): Maximum 100,000 characters per JSON request
- Payload Limit (GET): Maximum 10,000 characters via query strings
- CORS & Access: Configured allow-origin for all domains requests. No API Key required.
- Accuracy: Server-side API count is a rapid approximation based on the standard `~4 chars/token` metric. It parses split-words to supplement length metrics.
- 100% Exact Matching: For byte-perfect encoding exactly matching OpenAI and Anthropic standards, utilize the WASM web tokenizer instead of the API.
- Error Handling: Standard REST HTTP Codes (400 Bad Request on missing \`text\` or oversized payload). Returns JSON with an \`error\` description.