When you send a prompt to an LLM, the model does not read it as ordinary words. It processes the input as a sequence of smaller units called tokens.
This matters because most LLM APIs calculate usage based on tokens, not the number of words or characters you type.
For example, a short English sentence and a Hindi sentence with the same meaning may produce different token counts. A long number, product ID, or currency value can also be split into multiple tokens.
Understanding tokenization helps developers estimate API costs, manage context windows, and build more efficient AI applications.
Let's see how it works.
1. What Exactly Is a Token?
A token is a unit of text that a language model processes.
A token can represent:
- A complete word.
- A part of a word.
- A number or a group of digits.
- Punctuation or whitespace.
- A combination of common characters.
For example, the word tokenization might be represented as multiple subword tokens rather than one complete token.
This means:
One word does not always equal one token.
Similarly, 100 words do not necessarily produce 100 tokens. The count depends on the text and the tokenizer used by the model.
2. How BPE Tokenization Works
Many modern language models use a technique called Byte Pair Encoding (BPE) or a variation of it.
The basic idea is to build a vocabulary of frequently occurring text sequences.
Here's how to understand it:
Step 1: Start with smaller units
Text is represented using bytes and divided into suitable pieces before the BPE process.
Step 2: Identify frequent sequences
The tokenizer learns which adjacent sequences appear frequently in its training corpus.
Step 3: Merge common sequences
Frequently occurring sequences can become larger vocabulary units.
For example, a common suffix such as ing may be represented as a single token. The word encoding could be split into pieces such as encod and ing, depending on the tokenizer.
Step 4: Convert text into token IDs
When your application sends text to a model, the tokenizer maps the resulting pieces to numerical token IDs.
The model processes those IDs rather than directly processing the original text.
The exact splitting depends on the tokenizer vocabulary and encoding.
3. Why the Same Sentence Can Have Different Token Counts
Consider these two sentences:
English: "I am learning machine learning."
Hindi: "मैं मशीन लर्निंग सीख रहा हूँ।"
Both express a similar idea, but they may produce different token counts.
Why?
Tokenizers learn vocabulary units from text patterns. English words and common subwords may have more efficient representations in some tokenizers because those patterns are well represented in their vocabulary.
Hindi uses Devanagari characters and different word patterns. Some Hindi words may therefore be split into smaller pieces rather than represented by one token.
This can increase token usage for Hindi text compared with an English sentence expressing the same meaning.
However, this is not a universal rule. The result depends on the tokenizer, model, sentence, and language. Modern tokenizers can also represent multilingual text more efficiently than older ones.
For Indian-language applications, never estimate API cost using English token counts alone. Test your actual Hindi content.
4. Why Numbers Can Consume More Tokens
Numbers are another important source of unexpected token usage.
Consider these examples:
2500
2,500
2500.00
₹2,500
123456789012345
A tokenizer may split these strings differently.
A common number or digit sequence may be represented efficiently, while a long random number, decimal, formatted currency value, or unique ID may be divided into several tokens.
For example, an application that sends thousands of order IDs, transaction references, or product codes to an LLM can consume more tokens than expected.
The important point is:
A number is not guaranteed to be one token, and a tokenizer does not necessarily assign one token to every digit.
You should measure the actual token count for your data format.
5. Count Tokens Live with OpenAI tiktoken
OpenAI provides an open-source Python library called tiktoken for working with its model tokenizers.
Install it:
pip install tiktoken
Now let's count tokens for English, Hindi, and different number formats.
import tiktoken
# Load the encoding associated with GPT-4o
encoding = tiktoken.encoding_for_model("gpt-4o")
examples = {
"English": "I am learning machine learning.",
"Hindi": "मैं मशीन लर्निंग सीख रहा हूँ।",
"Number": "2500",
"Formatted number": "2,500.00",
"Long ID": "12345678901234567890",
}
for label, text in examples.items():
token_ids = encoding.encode(text)
pieces = [
encoding.decode_single_token_bytes(token_id).decode(
"utf-8",
errors="backslashreplace"
)
for token_id in token_ids
]
print(f"\n{label}: {text}")
print(f"Token count: {len(token_ids)}")
print(f"Token IDs: {token_ids}")
print(f"Token pieces: {pieces}")
This example does three things:
encoding_for_model() loads the tokenizer associated with the selected model.
encoding.encode() converts the input text into token IDs.
decode_single_token_bytes() helps inspect the individual token pieces. Some pieces may represent incomplete UTF-8 sequences, so their printed representation can contain escaped bytes.
The output lets you compare how the same tokenizer handles different text formats.
You can also change the input examples to test your own Hindi prompts, user messages, JSON payloads, or database records.
Important: Use the encoding associated with your actual model. A tokenizer from another model can return a different count.
6. How Token Counts Affect LLM API Cost
Most text-model APIs distinguish between two main types of usage:
Input tokens: Tokens sent to the model, including the prompt and relevant conversation context.
Output tokens: Tokens generated by the model in its response.
The basic cost formula is:
[ \text{Total Cost} = \frac{\text{Input Tokens}}{1,000,000} \times \text{Input Price} + \frac{\text{Output Tokens}}{1,000,000} \times \text{Output Price} ]
Let's use hypothetical pricing to understand the calculation.
Suppose an API charges:
| Token type | Example price |
|---|---|
| Input | $1 per million tokens |
| Output | $3 per million tokens |
Your request uses:
- Input tokens: 10,000
- Output tokens: 2,000
Input cost:
[ \frac{10,000}{1,000,000}\times 1 = $0.01 ]
Output cost:
[ \frac{2,000}{1,000,000}\times 3 = $0.006 ]
Total cost:
[ $0.01+$0.006=\boxed{$0.016} ]
This is only an illustrative calculation. Actual prices depend on the model and provider. Some providers also have separate rates for cached input, reasoning, or other token categories.
7. What Actually Consumes Input Tokens?
Developers sometimes count only the user's latest message when estimating API costs. That can lead to incorrect estimates.
An LLM request may include:
- System and developer instructions.
- The current user message.
- Earlier conversation history.
- Retrieved documents in a RAG application.
- Tool definitions and function parameters.
- Structured data and JSON payloads.
All of this can contribute to input usage.
For example, imagine a RAG application retrieves 10 document chunks and sends them to an LLM.
Even if the user's question is only 20 tokens long, the retrieved context may contain thousands of additional tokens.
That is why controlling retrieved context is important for both cost and latency.
8. Practical Ways to Reduce Token Usage
You do not need to remove useful context to reduce token costs. Focus on avoiding unnecessary input and output.
Limit conversation history: Send relevant messages instead of repeatedly including the entire conversation.
Optimize RAG retrieval: Retrieve fewer, more relevant chunks instead of sending large amounts of unnecessary context.
Keep instructions focused: Remove repeated system instructions and redundant prompt text.
Control output length: Use appropriate output-token limits and ask for concise responses when possible.
Test multilingual inputs: Measure Hindi and English token counts using the target model's tokenizer before estimating production costs.
Measure actual API usage: Use the token-usage information returned by the provider instead of relying only on local estimates.
9. Is tiktoken Enough to Calculate the Exact API Bill?
Not always.
tiktoken is useful for estimating text token counts for supported OpenAI models. However, counting a plain text string does not necessarily account for all tokens in a complete API request.
Chat message formatting, tool definitions, multimodal inputs, and model-specific processing can affect actual usage.
For production billing, use the API response's usage information as the source of truth.
Also remember that other providers may use different tokenizers. An OpenAI tokenizer should not be used to estimate token counts for an unrelated model.
Conclusion
Tokenization is more than a technical preprocessing step. It directly affects how much information an LLM can process and how much an API request costs.
BPE helps convert text into reusable vocabulary units, but the resulting token count depends on the tokenizer and the input itself.
For developers building multilingual AI applications, especially Hindi-language products, measuring real token usage is essential.
Count tokens, control context, and calculate costs before scaling your LLM application.



