Lesson 1 of 30
Every LLM API you'll ever call bills you, limits you, and thinks in tokens, not characters or words — and the gap between those three counts trips up almost everyone the first time.
11 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 3
You want to check whether a prompt will fit inside a model's limits before sending it. Which approach is actually reliable?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
A token is the actual unit an LLM reads, writes, and gets billed in. It's not a character and it's not a word — it's a sub-word chunk produced by a tokenizer, a separate piece of software that breaks text apart before any of it reaches the model.
Common short words are often a single token. Longer or rarer words frequently split into two or three pieces — a word like "tokenization" might come apart as token + ization. Punctuation, whitespace, and even parts of numbers can each claim their own token. None of this maps cleanly onto "one word" or "one character," which is exactly why it surprises people.
Two things ride directly on token count:
So when you're estimating how much a prompt will cost or whether a document will fit, "count the characters" or "count the words" is the wrong instrument. You need a token count, and the only reliable way to get one is to actually run the text through the same tokenizer the model uses.
// Rough sketch of what a token-aware cost estimate needs
const systemPrompt = "You are a helpful assistant for a Next.js codebase.";
const userMessage = "Summarize the changes in lib/ai/chat/handler.ts";
// Wrong: treating characters as a proxy for tokens
const naiveEstimate = (systemPrompt + userMessage).length; // character count — not billable units
// Right: run the actual text through a tokenizer to get a real count
// const tokenCount = tokenizer.encode(systemPrompt + userMessage).length;
A plain sentence: "Tokenization isn't 1:1 with words." 6 words, 40 characters.
If you built a "will this fit?" check using .length on a string, it would be wrong in both directions — sometimes overestimating (short common words), sometimes badly underestimating (a paragraph of rare technical terms, code identifiers, or non-English text, all of which tend to tokenize less efficiently than plain English prose). A codebase like this app's own lib/ai/chat/handler.ts, which sits in front of every AI call, is exactly the place this matters: before a request goes out, you want a real token count for the assembled prompt, not a guess based on string length.
"How would you estimate the cost of a prompt before sending it?" is a real question that separates candidates who reach for a real tokenizer from ones who reason about character or word counts — and it opens naturally into context-window and cost-control follow-ups.