Explorer
AI Foundation

The Secret Language of LLMs: Tokens, Tokenizers, and Context Windows

Large Language Models generate text by predicting what should come next.

For example:

JAVASCRIPT
The capital of India is ...

A model may continue with:

JAVASCRIPT
New Delhi

Then it repeats the process, generating one token after another until the response is complete.

From the outside, it can feel as if the model is simply reading words and writing more words.

But an LLM is built using a neural network, and that neural network does not work with written words in the same way humans do.

Inside the model, computation happens using numerical representations.

So before a sentence can be processed by the model, human-readable text must first be converted into a form that the neural network can work with mathematically.

That sounds simple, but it raises several questions.

If you type:

JAVASCRIPT
He is amazing.

does the model receive three words and a full stop?

To answer this questions, we first need to understand how text is broken into smaller pieces.


1. Text Does Not Go Directly Into the Model

Imagine this sentence:

JAVASCRIPT
He is amazing.

Before the model processes the text, it is represented as smaller units.

For teaching purposes, imagine the split looks like this:

JAVASCRIPT
He | is | amazing | .

These smaller units are called tokens.

A token is a unit of text produced by the model's tokenization scheme.

For intuition, think of it as:

a piece of text represented as one unit for model processing.

A token can represent:

  • a whole word,

  • part of a word,

  • punctuation,

  • a symbol,

  • a character,

  • or text together with surrounding whitespace.

So this common assumption is wrong:

JAVASCRIPT
One word = one token

For example:

JAVASCRIPT
programming

might be one token in one system.

Another system might split it into something like:

JAVASCRIPT
program | ming

The exact split depends on the tokenizer and its vocabulary.

The key idea is:

Word ≠ Token


2. What Is a Tokenizer?

Now that we know what tokens are, we can name the component that creates them.

A tokenizer is the component that turns text into tokens and maps those tokens to numerical IDs called token IDs.

Conceptually:

JAVASCRIPT
Text
 ↓
Tokenizer
 ↓
Tokens
 ↓
Token IDs

Suppose we start with:

JAVASCRIPT
I love programming

A tokenizer might conceptually split it into:

JAVASCRIPT
I | love | programming

Then each token is matched with an ID:

JAVASCRIPT
I           → 51
love        → 872
programming → 4381

So the model may receive something like:

JAVASCRIPT
[51, 872, 4381]

The values above are only illustrative. Real token IDs depend on the tokenizer being used.

Encoding and Decoding

Turning text into token IDs is commonly called encoding.

JAVASCRIPT
Readable Text
    ↓
Encoding
    ↓
Token IDs

Converting token IDs back into text is called decoding.

JAVASCRIPT
Token IDs
    ↓
Decoding
    ↓
Readable Text

So during generation, the broad flow is:

JAVASCRIPT
Text
 ↓
Tokens
 ↓
Token IDs
 ↓
Language Model
 ↓
Generated Token IDs
 ↓
Readable Text

This gives us the first useful mental model for how human-readable text reaches a language model.


3. Token IDs Do Not Contain Meaning

Suppose a tokenizer assigns Token ID:

JAVASCRIPT
Java   → 932
React  → 145
Apple  → 71
Dog    → 503

You might notice:

JAVASCRIPT
932 > 145

Does that mean Java has more meaning than React?

No.

A token ID is an identifier within a particular tokenizer vocabulary.

Think of employee IDs:

JAVASCRIPT
Rahul → 142
Anita → 781

The fact that:

JAVASCRIPT
781 > 142

does not make Anita more important than Rahul.

The number identifies the employee.

It does not describe the employee.

Token IDs work in the same way.

JAVASCRIPT
Java → 932

roughly means:

Use vocabulary entry 932.

It does not mean that the numerical value 932 itself contains the meaning of Java.

So remember:

Token ID ≠ Meaning

This distinction becomes extremely important later when we ask how a model represents relationships between concepts.


4. Why Not Use One Token for Every Word?

A simple idea would be to give every word its own token.

For example:

JAVASCRIPT
boy
girl
computer
programming
developer

Then:

JAVASCRIPT
This is a boy

could become:

JAVASCRIPT
This | is | a | boy

That looks convenient.

But consider:

JAVASCRIPT
develop
developer
developers
developing
development
developmental

Should every variation require a completely separate entry?

Now add:

  • names,

  • company names,

  • technical terms,

  • spelling variations,

  • URLs,

  • code identifiers,

  • new words,

  • and many human languages.

The vocabulary would become extremely large.

There is another problem too:

What happens when the system sees a word that is not already stored as a complete entry?

A pure word-level approach therefore creates serious vocabulary and coverage problems.


5. Why Not Use Individual Characters?

We could go to the other extreme.

Instead of:

JAVASCRIPT
programming

we could use:

JAVASCRIPT
p | r | o | g | r | a | m | m | i | n | g

Now unfamiliar text is easier to represent because it can be built from smaller units.

But sequences become much longer.

Longer sequences mean:

  • more positions for the model to process,

  • more context space used,

  • more computation,

  • and potentially higher API usage.

So we have two extremes:

JAVASCRIPT
Whole words
→ shorter sequences
→ very large vocabulary

and:

JAVASCRIPT
Characters
→ smaller vocabulary
→ very long sequences

A practical system needs something between them.


6. The Middle Ground: Subword Tokens

This leads to subword tokenization.

A subword is simply a reusable piece of text that can be smaller than a full word but larger than a single character.

Imagine:

JAVASCRIPT
untrustable

being split conceptually into:

JAVASCRIPT
un | trust | able

Now pieces can be reused:

JAVASCRIPT
unable
→ un | able
JAVASCRIPT
trustable
→ trust | able
JAVASCRIPT
untrusted
→ un | trust | ed

The exact splits above are only examples.

A real tokenizer may divide those words differently.

The important idea is the trade-off:

JAVASCRIPT
Characters ←──── Subwords ────→ Whole Words
                    ↑
             practical middle

Subword tokenization offers a practical trade-off: it can keep the vocabulary manageable without turning every word into a long sequence of tiny units.

Token Boundaries Are Not Meaning Boundaries

There is one important detail.

If you see:

JAVASCRIPT
un | trust | able

you may naturally interpret:

JAVASCRIPT
un    = not
trust = trust
able  = capable of

But a tokenizer does not need to split words according to grammar or human meaning.

Its job is to create units that work well for the tokenization scheme; those boundaries do not have to match human linguistic analysis.

So:

Token boundary ≠ Semantic boundary

A token can be useful to the model even if its boundary looks strange to Us.


7. How Does a Tokenizer Build Its Vocabulary?

A tokenizer works with a vocabulary: a mapping between token pieces and their token IDs.

Each vocabulary entry has a token ID.

For example:

Token

Token ID

Java

23232

is

43

good

718

.

13

So:

JAVASCRIPT
Java is good.

might become:

JAVASCRIPT
Java | is | good | .

and then:

JAVASCRIPT
[23232, 43, 718, 13]

But how are useful text pieces chosen?

There are several tokenization methods, including BPE, WordPiece, and Unigram.

One well-known approach is Byte Pair Encoding, usually shortened to BPE.

The name sounds complicated, but its core intuition is simple.

Imagine the text contains:

JAVASCRIPT
low
lower
lowest

For a simplified character-level illustration, start with small pieces:

JAVASCRIPT
l | o | w
l | o | w | e | r
l | o | w | e | s | t

If:

JAVASCRIPT
l + o

appears together often, those pieces can be merged:

JAVASCRIPT
lo

Later:

JAVASCRIPT
lo + w

might also become:

JAVASCRIPT
low

The simplified idea is:

JAVASCRIPT
Start with small pieces
        ↓
Find recurring neighboring pieces
        ↓
Merge useful patterns
        ↓
Repeat
        ↓
Build reusable vocabulary pieces

You do not need the full BPE algorithm to understand the important point:

A tokenizer can learn reusable text pieces from patterns in data instead of storing every possible word manually.

Real implementations include additional preprocessing details, and byte-level BPE begins from bytes rather than ordinary characters. But the merge intuition above is enough to understand the main trade-off.

BPE is not the only tokenization method; WordPiece and Unigram are other widely used subword approaches.


8. The Same Text Can Tokenize Differently

Token IDs only make sense relative to the tokenizer vocabulary that defines them.

If one tokenizer says:

JAVASCRIPT
Java → 23232

another tokenizer may:

  • assign Java another ID,

  • split it into multiple tokens,

  • or use a different vocabulary entirely.

Therefore:

JAVASCRIPT
Same Text
+
Different Tokenizer
=
Potentially Different Tokens

Consider:

JAVASCRIPT
Artificial intelligence is amazing.

chatgpt tokenizer might produce:

JAVASCRIPT
Artificial | intelligence | is | amazing | .

Another gemini tokenizer might produce:

JAVASCRIPT
Art | ificial | intelligence | is | amazing | .

That means the same sentence can have different token counts.

This matters because token count can affect:

  • API cost,

  • context usage,

  • how much room remains for output,

  • and processing requirements.

So:

JAVASCRIPT
100 words ≠ necessarily 100 tokens

and:

JAVASCRIPT
Different tokenizer ≠ same token count

Does a Larger Vocabulary Mean a Better Model?

No.

A larger vocabulary can represent some common text with fewer tokens, but it also has trade-offs.

Designers balance things such as:

  • vocabulary size,

  • sequence length,

  • language coverage,

  • memory,

  • and compute.

So:

Larger vocabulary ≠ Better model

Tokenization is one part of the system, not a measure of intelligence.


9. Languages, Emojis, and Code

Tokenization is especially important outside simple English text.

Different Languages

Consider:

JAVASCRIPT
I am learning English.

and:

JAVASCRIPT
मैं अंग्रेज़ी सीख रहा हूँ।

They can communicate similar ideas but still use different numbers of tokens.

Why?

Because a tokenizer's vocabulary and encoding rules may represent patterns from different languages differently.

So:

Same meaning does not mean same token count.

Emojis

Consider:

JAVASCRIPT
😂

or:

JAVASCRIPT
👨‍👩‍👧‍👦

What looks like one visual symbol to us is not guaranteed to be one token.

Unicode representation and the tokenizer's vocabulary can cause a visible emoji to be represented by one or several tokens.

So:

One visible symbol ≠ necessarily one token


10. What Is a Context Window?

So far, we have looked at how normal text becomes tokens and token IDs.

But when you chat with an AI assistant, the model usually receives more than just your current message.

For example, the application may give the model:

JAVASCRIPT
Instructions
+
Previous Messages
+
Your Current Message
+
Rules
+
Retrieved Information / Tool Results

Your current message is only one part of the complete input.

The exact information included depends on the application.

The important idea is:

The model can receive instructions, previous conversation, your current message, retrieved information, and tool results together.

All of this becomes part of the information available to the model for the current request.

But the model cannot consider an unlimited amount of information at once.

There is a limit to how many tokens can be included in a single request.

That limit is called the context window.

You can think of the context window as:

the maximum amount of tokenized information the model can work with at one time.

Conceptually:

JAVASCRIPT
Context Window

Instructions
+
Previous Messages
+
Current Message
+
Retrieved Information
+
Tool Results
+
Generated Response

As more information is included, more of the available context capacity is used.

The response generated by the model also uses tokens.

The exact limits and accounting differ between models, but the important rule is:

The context window has finite capacity.

This means a conversation cannot keep growing forever while every previous message remains fully available to the model.

And that leads to another important distinction:

What you can see in the chat history is not necessarily the same as what is currently included in the model's context.


12. Context Window Is Not Permanent Memory

Suppose a chat contains hundreds of messages.

You may still be able to scroll back and see the first message.

That does not mean every old message is sent to the model again with every new request.

An application may:

  • send some previous messages,

  • remove irrelevant ones,

  • summarize older conversation,

  • retrieve relevant information when needed,

  • use a separate memory system,

  • or combine these approaches.

Therefore:

JAVASCRIPT
Visible Chat History ≠ Exact Active Context

and:

JAVASCRIPT
Context Window ≠ Permanent Memory

A useful analogy is a desk.

You may own thousands of books, but only some can be open on your desk at one moment.

The context window is closer to that desk than to the entire library.


13. More Context Is Not Automatically Better

A large context window tells us how much tokenized information a model can accept within its limits.

It does not guarantee that every piece of that information will be used equally well.

So:

JAVASCRIPT
Context Capacity
≠
Perfect Context Utilization

This is an important engineering distinction.

Imagine you are debugging one error.

You could send:

JAVASCRIPT
600 source files
+
all logs
+
the full database schema
+
months of conversations
+
all documentation

That gives the model more information.

But the useful evidence may be buried inside noise.

A better request might contain:

JAVASCRIPT
Relevant Component
+
API Response
+
Stack Trace
+
Expected Behavior

So the goal is not:

send as much information as possible.

The better goal is:

send the information that is actually useful for the current task.

This broader design problem is often described as context engineering.

Context engineering means deciding what information the model should receive, how that information should be represented, and when it should be supplied.


14. Why Retrieval Helps

Suppose your system contains:

JAVASCRIPT
100,000 documents

Putting all of them into the model context would usually be wasteful or impossible.

Instead, the application can first find the most relevant parts:

JAVASCRIPT
Question
   ↓
Search Documents
   ↓
Retrieve Relevant Sections
   ↓
Send Those Sections to the Model

Finding relevant information from an external source is called retrieval.

When a system retrieves relevant information and supplies it to a model as context for generation, the pattern is commonly called Retrieval-Augmented Generation, or RAG.

The important idea is simple:

Find useful information first. Give the model only what it needs.

This helps control:

  • context usage,

  • noise,

  • cost,

  • and relevance.


15. Longer Prompt Does Not Mean Better Prompt

More tokens do not automatically create a better answer.

Compare:

Too Vague

JAVASCRIPT
Explain closures.

Short, but unclear.

Long but Noisy

JAVASCRIPT
I would like you to explain the JavaScript concept called
closures and explain closures in JavaScript and describe
the closure concept...

Longer, but repetitive.

Clear and Specific

JAVASCRIPT
Explain JavaScript closures to a beginner who understands
functions and scope.

Use one practical example and explain why an inner function
can still access an outer variable after the outer function returns.

This is longer than the first prompt, but almost every sentence adds useful information.

A good prompt gives the model:

  • the task,

  • relevant context,

  • the expected level,

  • useful constraints,

  • and the desired output.

So:

Better prompt ≠ Longer prompt

The goal is useful context, not maximum token count.


16. Why Tokens Matter in Real Systems

Tokens are not just an internal language-model detail.

They affect practical system design.

Token count can influence:

  • API cost,

  • context limits,

  • available output space,

  • document chunking,

  • retrieval design,

  • multilingual usage,

  • code processing,

  • and the amount of computation required.

Do not estimate exact token counts by counting words.

For example:

JAVASCRIPT
10,000 words

does not mean:

JAVASCRIPT
10,000 tokens

Token count depends on:

  • the tokenizer,

  • language,

  • punctuation,

  • spaces,

  • code,

  • names,

  • symbols,

  • and formatting.

Rough estimates can be useful, but when exact limits or costs matter, use the tokenizer or token-counting mechanism for the model you are actually using.


17. Common Misconceptions

Misconception

Better Mental Model

One token = one word

A word may become one or several tokens

Every model uses the same tokenizer

Different models can use different tokenization systems

Token IDs contain meaning

IDs identify vocabulary entries

Token boundaries follow grammar

Token boundaries do not have to match human linguistic boundaries

One emoji = one token

A visible symbol may require multiple tokens

Larger vocabulary = better model

Vocabulary design involves trade-offs

Large context = perfect memory

Capacity does not guarantee perfect use

Visible history = active context

Applications may summarize, retrieve, or omit information

More tokens = better answer

Relevant context matters more than raw length


18. Putting Everything Together

Now we can follow one sentence through the whole process.

You type:

JAVASCRIPT
He is amazing.

The application prepares the model input.

Then:

JAVASCRIPT
Human-Readable Text
        ↓
     Tokenizer
        ↓
       Tokens
        ↓
     Token IDs

Conceptually:

JAVASCRIPT
He | is | amazing | .

might map to:

JAVASCRIPT
[1544, 382, 8467, 13]

Those IDs become part of the model's active context.

The context may also include:

  • instructions,

  • earlier messages,

  • retrieved information,

  • and tool results.

The language model processes its numerical representations and produces a prediction for the next token.

That new token becomes part of the sequence, and generation continues token by token.

The generated token IDs are decoded into text that can be shown to the user.

So the complete mental model is:

JAVASCRIPT
Application Input
       ↓
Structured Text
       ↓
Tokenizer
       ↓
Tokens
       ↓
Token IDs
       ↓
Finite Context Window
       ↓
Language Model
       ↓
Generated Token IDs
       ↓
Decoding
       ↓
Readable Response

Final Takeaway

We started with a simple question:

How does the text we write become something a language model can process?

Now we can follow the path:

JAVASCRIPT
Human Text
    ↓
Tokenizer
    ↓
Tokens
    ↓
Token IDs
    ↓
Language Model

Along the way, we learned that:

  • a token is not always a complete word,

  • different tokenizers can split the same text differently,

  • token IDs are only identifiers,

  • subword tokenization helps balance vocabulary size and sequence length,

  • all the information given to the model must fit within a limited context window,

  • and more context does not automatically mean better context.

But we have only solved one part of the problem:

how text reaches the model.

There is still something important missing.

Suppose:

JAVASCRIPT
Java   → 23232
Python → 9821
Banana → 417

These numbers only identify vocabulary entries.

They do not explain why:

JAVASCRIPT
Java ↔ Python

should be treated as more closely related than:

JAVASCRIPT
Java ↔ Banana

So the next question becomes:

How does the model turn these simple IDs into numerical representations that can capture useful relationships between concepts?

That is what we will explore in the next chapter.

Finished this lesson?

Mark this chapter complete to update your learning streak and unlock the next lesson.