Loading...
const words = text.split(' ').length;
if (words > 4000) throw new Error('Input too long');const tokens = await countTokens(text); // tokenizer of the model in use
if (tokens > MAX_INPUT_TOKENS) throw new Error('Input too long');The limit is checked in tokens with the model's own tokenizer, since word counts underestimate tokens, especially outside English.
In English a token is often about three quarters of a word on average, but it varies by tokenizer, language and content like code.
Tokenizers are often trained mostly on English text, so words in other languages and scripts get split into more, smaller pieces.
Often not. Many APIs charge more for output tokens than input tokens, and cached input may cost less.