Catalog/util-count-tokens

Utilities

Count tokens in text API

Count the number of LLM tokens in a string using a real BPE tokenizer (GPT-4 family encoding). For agents budgeting context windows and estimating model costs.

Price$0.01per request
MethodPOST
Route/v1/util/count-tokens
StatusLive
MIME typeapplication/json
Rate limit120/minute
CacheNo cache
utiltokenstokenizerllmcontextcount
API URLhttps://x402.hexl.dev/v1/util/count-tokens
Integration docs
Example request
{
  "text": "The quick brown fox."
}
Example response
{
  "tokens": 5,
  "characters": 20,
  "encoding": "cl100k_base (GPT-4 family)",
  "note": "Token count uses the GPT-4 family encoding."
}
Input schema
{
  "type": "object",
  "required": [
    "text"
  ],
  "properties": {
    "text": {
      "type": "string",
      "minLength": 1,
      "maxLength": 1000000
    }
  }
}
Output schema
{
  "type": "object",
  "required": [
    "tokens",
    "characters",
    "encoding"
  ],
  "properties": {
    "tokens": {
      "type": "number"
    },
    "characters": {
      "type": "number"
    },
    "encoding": {
      "type": "string"
    },
    "note": {
      "type": "string"
    }
  }
}