Skip to main content
TokenCost logoTokenCost

Free Token Counting API

Count LLM tokens for GPT, Claude, Gemini, and Llama models with a single HTTP request. No API key, no signup, no SDK. The same tokenizers that power our token counter, exposed as a public endpoint.

Endpoint

GET  https://tokencost.app/api/count
POST https://tokencost.app/api/count

Use GET with query parameters for quick checks, or POST a JSON body for large payloads. Both return the same JSON response and both send permissive CORS headers, so you can call the API straight from a browser, a serverless function, or a CI script.

GET example

curl "https://tokencost.app/api/count?text=Hello%20world&model=claude-sonnet-5"

Passing a model id resolves the correct tokenizer for that model and includes a cost estimate at its current per-token rates.

POST example

curl -X POST https://tokencost.app/api/count \
  -H "Content-Type: application/json" \
  -d '{"text": "Hello world", "tokenizer": "o200k_base"}'

POST accepts a JSON body with the same fields as the GET query parameters: text, model, and tokenizer. Use POST when your text is too long to fit comfortably in a URL.

Parameters

ParameterTypeRequiredDescription
textstringYesThe text to tokenize. Maximum 200,000 characters.
modelstringNoA model id from our pricing catalog, for example gpt-5.5, claude-sonnet-5, or llama-3.3-70b. Resolves the right tokenizer and adds a cost estimate to the response.
tokenizerstringNoCount with a specific tokenizer directly: cl100k_base, o200k_base, or llama3. Ignored when model is provided. Defaults to o200k_base.

Example response

{
  "tokens": 2,
  "characters": 11,
  "words": 2,
  "tokenizer": "cl100k_base",
  "exact": false,
  "model": "claude-sonnet-5",
  "cost": {
    "inputUSD": 0.000006,
    "outputUSD": 0.00003
  }
}

The cost object reports what this many tokens would cost as input and as output at the model's current per-million-token rates, in US dollars. It only appears when you pass a model. New to tokens? Start with what are tokens.

Exact vs estimated counts

The exact field tells you whether the count is genuinely precise for the model you asked about, following the same honesty rule as the rest of the site:

  • OpenAI models use their official tiktoken encodings, and Llama 3 models use the official Llama 3 tokenizer, so those counts are exact.
  • Anthropic, Google, and most other providers do not publish their tokenizers. For those models we count with the closest tiktoken encoding, which is a good estimate but not exact, so the response reports exact: false.
  • When you request a tokenizer directly, the count is exact for that tokenizer by definition.

Limits and fair use

  • No API key required. Free for any use, including commercial.
  • Rate limit: 60 requests per minute per IP. Exceeding it returns HTTP 429.
  • Maximum text size: 200,000 characters. Larger payloads return HTTP 413.
  • Nothing you send is stored. Responses are served with Cache-Control: no-store.
  • If you use the API in a public project, a link back to tokencost.app is appreciated but not required.