Skip to content
ACS Infra

Tokenize a text

Turn a text into the token ids of a Base model, count them before you send a request, and turn ids back into text. The API answers with the tokenizer that it counts a Hold with. It asks no model, so the two routes cost no tokens and never wait for a start.

POST /v1/tokenize and POST /v1/detokenize take the fields of vLLM's routes of the same names. They work on llama-8b, llama-405b, and trinity-truebase.

Count the tokens of a text

Send POST /v1/tokenize with a model and a prompt. The answer has the token ids in tokens, their number in count, and the model's context length in max_model_len.

curl "$ACS_API_BASE/tokenize" \
  -H "Authorization: Bearer $ACS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "llama-8b", "prompt": "The sky is"}'

The curl command prints the answer:

Answer
{
  "count": 4,
  "max_model_len": 131072,
  "tokens": [128000, 791, 13180, 374],
  "token_strs": null
}

The Python program prints the count and the ids:

Python output
4 [128000, 791, 13180, 374]

count is the number of prompt tokens that POST /v1/completions holds for the same prompt and add_special_tokens. The first id, 128000, is the BOS token <|begin_of_text|>, which Inference adds in front of a text. Set add_special_tokens: false to leave it out, as in a Completion. A prompt and its max_tokens must fit max_model_len (Prompt too long).

Read each token's text

Set return_token_strs: true to get the text of each token in token_strs. Each text is the token as a Completion shows it in tokens and in the keys of top_logprobs, so you can find a token there. This request asks for the tokens of an answer letter in brackets:

curl "$ACS_API_BASE/tokenize" \
  -H "Authorization: Bearer $ACS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "llama-8b", "prompt": " (A)", "add_special_tokens": false, "return_token_strs": true}'
Answer
{
  "count": 3,
  "max_model_len": 131072,
  "tokens": [320, 32, 8],
  "token_strs": [" (", "A", ")"]
}

The text is three tokens, and the letter is the token "A", id 32. After " (", the letter is a token of its own. To read the log probability of a token that you choose, follow Get the log probability of a chosen token.

token_strs differs from vLLM's own route in two ways:

  • vLLM's token_strs has the pieces of the vocabulary, such as Ġ( for " (". This API gives the text instead.
  • A token that holds only part of a character's UTF-8 bytes reads � (U+FFFD), and so do the other parts. A Completion's tokens give the whole character to the last part. Send all the ids to POST /v1/detokenize to read the character.

The answer has no character offsets, and vLLM's route has none either. When each character of a text stays in one token, the texts in token_strs join to the text. A token then starts at the length of the texts before it.

Turn token ids into text

Send POST /v1/detokenize with a model and the ids in tokens. The answer's prompt is their text. It keeps the special tokens, such as BOS, and every space, also before a point or a comma.

curl "$ACS_API_BASE/detokenize" \
  -H "Authorization: Bearer $ACS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "llama-8b", "tokens": [128000, 791, 13180, 374]}'
Answer
{ "prompt": "<|begin_of_text|>The sky is" }

The ids of POST /v1/tokenize give its prompt back, with the BOS token's text in front when add_special_tokens added it. An empty tokens list gives the empty text.

Requests that the API refuses

The API checks each request before it tokenizes, and answers 400 invalid_request_error:

Requestcode and param
A model that the API does not servemodel_not_found, model, with the message of POST /v1/completions
A model that the API has no tokenizer forinvalid_request, model. The message names the models that it tokenizes.
A prompt of more than 65,536 bytes as UTF-8 (Request size)invalid_request, prompt
More than 65,536 ids (Request size)invalid_request, tokens
An id below 0, or at or past the size of the tokenizer's vocabularyinvalid_request, tokens

The vocabulary of llama-8b and llama-405b is 128,256 ids, 0 to 128,255. The tokenizer of trinity-truebase has 200,000, although the model scores 200,192.

To count a longer text:

  1. Split the text into parts of at most 65,536 bytes, each cut before a space.
  2. Tokenize the first part as it is, and each later part with add_special_tokens: false, so that only the first part has the BOS token.
  3. Add up the counts.

This request asks for an id past the vocabulary of llama-8b:

curl "$ACS_API_BASE/detokenize" \
  -H "Authorization: Bearer $ACS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "llama-8b", "tokens": [791, 128256]}'
Error
{
  "error": {
    "message": "tokens[1] is 128256, past the vocabulary of llama-8b's tokenizer: its token ids are 0 to 128255.",
    "type": "invalid_request_error",
    "param": "tokens",
    "code": "invalid_request"
  },
  "request_id": "req_9f2c4e1a7b3d4c5e8f6a0b1c2d3e4f5a"
}

Each request counts toward your API key's 600 other requests a minute (Traffic).

Next steps

Read token probabilities reads the log probabilities of the tokens that you counted. Tokens and Holds says how a request holds and spends tokens.

On this page