AI
10/27/2026
7 min read

LLM Tokens Explained: What They Are and Why They Decide Your Bill

LLM Tokens Explained: What They Are and Why They Decide Your Bill

Your first LLM feature works. Then the invoice arrives, or a long request fails with a limit error, and nobody on the team can say how big the request was. The answer is in one unit that most developers never look at: the token.

Tokens are how a language model reads your text, how providers meter your usage, and how limits are set. This guide explains what an LLM token is, how to count tokens in Node.js, and how to design your backend so tokens stop being a surprise.

What Is an LLM Token?

A token is a small chunk of text that a model reads and writes as a single unit. OpenAI's help center describes tokens as pieces of words. A token can be a whole word, part of a word, a word with its leading space, or a single punctuation mark.

The model never sees your letters. Before a request reaches the model, a program called a tokenizer splits your text into tokens and turns each one into a number. The model works on those numbers and returns more numbers, which the tokenizer turns back into text.

We ran a short test with the open-source js-tiktoken library (version 1.0.21) and the o200k_base encoding on October 27, 2026. The sentence "Backend engineers build reliable systems." has 41 characters and split into 6 tokens: "Backend", " engineers", " build", " reliable", " systems", and ".". The word "unbelievably" split into 3: "un", "bel", and "ievably". Short, common words are cheap. Long or rare words cost more pieces.

Rules of Thumb for Counting Tokens

OpenAI's help center gives two rules for English text: 1 token is about 4 characters, and 1 token is about three-quarters of a word. By that math, 1,000 tokens is about 750 words, and a 3,000-word article is roughly 4,000 tokens.

Use these numbers for a quick guess only. The real count depends on the model and on what is in the text. Code, JSON, URLs, and numbers usually break into more tokens than plain prose, so a payload full of IDs and timestamps can be bigger than it looks.

Why Tokens Are Not the Same Everywhere

Every model family ships its own tokenizer, so the same text can produce different counts on different models. Anthropic's token counting documentation notes that Claude 4.7 and later models use a newer tokenizer, and that the same input text produces approximately 30% more tokens than on earlier models. Its advice is to recount your prompts against the model you plan to use instead of reusing old numbers.

Language matters too. OpenAI's help center says the higher token-to-character ratio can make the API more expensive to use for languages other than English. We saw the effect in our own test. The Swahili sentence "Habari yako, ninataka kutuma pesa kupitia M-Pesa" has 48 characters and split into 13 tokens, about 3.7 characters per token. The English sentence above averaged about 6.8 characters per token. The two sentences are not a fair benchmark, but the gap is a reminder to test your real traffic. If your users write in Swahili, Yoruba, or Pidgin, count tokens on their messages, not on English samples.

What Counts Against Your Limit and Your Bill

Everything you send is input: the system prompt, the conversation history, retrieved documents, tool definitions, and the new user message. Everything the model writes back is output. Both are counted, and both are billed.

Providers normally price output tokens higher than input tokens, so a long answer costs more per token than a long question. Read the live pricing page of the provider you use when you plan a budget. Prices change, so we do not quote any here.

Tokens also set the size of the model's working memory. That limit is the context window, and your input plus the output must fit inside it. Tokens are the unit that window is measured in.

Dollar-priced APIs make this a budgeting decision for teams in Lagos, Nairobi, and Accra, where the card that pays the bill is charged in dollars. The good news is that token cost is something you control with code. A chat feature that sends the whole history on every turn pays for the same old messages again and again. Trimming that history is often the single biggest saving.

How to Count Tokens in Node.js

You can count tokens locally and for free with the open-source js-tiktoken package. It is pure JavaScript, so it runs without any native build step. Install it with npm install js-tiktoken, then create the file token-budget.js:

// token-budget.js
import { getEncoding } from "js-tiktoken";

const enc = getEncoding("o200k_base");

export const countTokens = (text) => enc.encode(text).length;

export function estimateCost({ inputText, expectedOutputTokens, pricePerMillion }) {
  const inputTokens = countTokens(inputText);
  const cost =
    (inputTokens / 1_000_000) * pricePerMillion.input +
    (expectedOutputTokens / 1_000_000) * pricePerMillion.output;
  return { inputTokens, expectedOutputTokens, cost };
}

The function countTokens turns text into tokens and returns how many there are. The function estimateCost multiplies the input and expected output counts by prices per million tokens. Pass the prices in from your own config, copied from your provider's pricing page that day, so the code never carries stale numbers.

Keep one limit in mind. A local counter tells you how an OpenAI-style tokenizer splits text. It will not match every model exactly. For Claude models, use Anthropic's token counting endpoint instead. Anthropic documents it as free to use, with its own rate limits, and says the returned count is an estimate that can differ by a small amount from the tokens actually used. Billing reflects only your content.

Five Backend Habits That Control Token Spend

Measure Every Request

Provider responses report how many input and output tokens a request used. Log those numbers with the feature name and the user's plan. A weekly query that sorts features by tokens per request shows you where the money goes, and it often surprises people.

Trim the Conversation History

Keep the last few turns and a short summary of older ones, instead of the full transcript. Our guide to the context window shows the trimming pattern step by step.

Retrieve Only What You Need

When you ground answers in your own documents, send the 3 best passages, not the 30 closest ones. Fewer, better passages cost less and often reduce wrong answers too. Our post on LLM hallucination explains why grounding matters.

Cap the Output

Set a maximum output length on every call, and ask for short answers when short answers are enough. Because output tokens usually cost more, a cap protects both your bill and your response time.

Set Budgets Before Launch

Give each user and each feature a daily token limit in your own code, and reject or queue requests that go over. A single runaway loop or an abusive user should hit your limit, not your credit card. Our guide on the 6 layers every AI backend needs shows where this kind of guard belongs.

Self-Hosted Models and Token Cost

If you run an open-weight model on your own hardware, there is no per-token invoice. Tokens still matter, because they set how much memory a request needs and how long it takes, and because power and GPU time have a cost of their own. The habits above still apply. They just save time and hardware instead of dollars.

Debugging Token Surprises

When a request fails with a limit error or a bill jumps, count first and guess later. Log the token count of each part of the prompt separately: system prompt, history, retrieved text, and user message. The part that grew is almost always the cause. Our guide on how to debug AI backend systems covers tracing a bad request back to its source.

If you want to learn these ideas in a structured order, the AI Engineering course takes you from AI foundations and prompt engineering through RAG pipelines and agents to cost optimization and caching.

Sources

  • What are tokens and how to count them, OpenAI Help Center, read October 27, 2026. The page shows no publication date.
  • Token counting, Claude Platform documentation, read October 27, 2026.
  • Token counts for the example sentences: our own run of js-tiktoken 1.0.21 with the o200k_base encoding, October 27, 2026.

Summary

An LLM token is the small piece of text a model reads and writes, and it is the unit behind your bill, your limits, and your context window. English text averages about 4 characters per token, but the real number depends on the model, the language, and whether the text is prose, code, or JSON.

Count tokens before you call the API, log the usage the provider reports, trim history, retrieve less, cap the output, and set budgets per user and per feature. Start today by adding one log line that records input and output tokens for your most used AI feature, then read it a week from now.

Frequently Asked Questions

What Is a Token in an LLM?

A token is a piece of text that a language model reads and writes as one unit. It can be a whole word, part of a word, a space plus a word, or a punctuation mark. OpenAI's help center puts it simply: tokens can be thought of as pieces of words.

How Many Words Is 1,000 Tokens?

For English, OpenAI's help center gives two rules of thumb: 1 token is about 4 characters, and 1 token is about three-quarters of a word. By that rule 1,000 tokens is roughly 750 words. Treat it as an estimate, and count the real number with a tokenizer before you plan a budget.

Do Input and Output Tokens Cost the Same?

No. Providers usually list one price for input tokens and a higher price for output tokens, and they bill both. Read the pricing page of the provider you use on the day you plan your budget, because the numbers change.

Is a Token the Same as a Word?

No. Common English words are often one token, but longer or rarer words split into several pieces, and punctuation and spaces count too. Text in other languages and in code often needs more tokens per word than plain English.

Why Does the Same Text Give Different Token Counts on Different Models?

Each model family has its own tokenizer, the rulebook that splits text into tokens. Anthropic notes that newer Claude models use a newer tokenizer, and that the same text can produce roughly 30% more tokens than on earlier models. Count tokens against the exact model you plan to call.

How Do You Count Tokens Before Calling an API?

Use the provider's own counter when one exists. Anthropic offers a free token counting endpoint that returns the input token count for a request. For OpenAI-style tokenizers you can run an open-source library such as js-tiktoken in Node.js and count on your own machine for free.

How Do You Reduce LLM Token Costs?

Send less text: trim old conversation turns, retrieve only the passages you need, shorten system prompts, and set a maximum output length. Then measure token use per request so you can see which feature spends the most.

Tags

Enjoyed this article?

Subscribe to our newsletter for more backend engineering insights and tutorials.