Ai Engineering Core / Transformers, Tokens & Context Windows

Understanding Tokenization

๐Ÿš€ Modern Approach โ€” Tokenization

๐ŸŽจ Spaces Are Part of the Token Too!

Here's something surprising โ€” tokens don't just capture the word itself, they often include the space before it as well! ๐Ÿคฏ

" important" โ€” notice the space at the front ๐Ÿ‘ˆ

This tells the model two things at once:

  • ๐Ÿ“ A new word is starting here

  • ๐Ÿ’ฌ That word is "important"

๐Ÿงฉ Where a Token Appears Matters

The same word can be tokenized differently depending on where it sits:

Type

Example

๐ŸŸข Start of a word

" important" (with space)

๐Ÿ”ต Fragment inside a word

"important" (no space)

๐Ÿ” Take "unimportant" for example:
โ†’ un + important (fragment)

๐Ÿ’ก Why This is Clever

By tracking spaces and positions, the model can understand word structure more naturally โ€” recognizing roots, prefixes, and how words connect to each other. ๐Ÿง 


๐Ÿ” Rare Words Get Broken Into Pieces

Uncommon words don't get their own token โ€” instead, they're split into smaller, familiar fragments the model already knows. ๐Ÿงฉ

๐Ÿ”ฌ "exquisitely" โ†’ ex + quis + ite + ly

๐Ÿง  Why This is Smart

Rather than learning every rare word from scratch, the model reuses its knowledge of:

  • ๐Ÿ”“ Prefixes

  • ๐Ÿ”š Suffixes

  • ๐Ÿ”— Common fragments

This leads to faster learning โšก, sharper pattern recognition ๐ŸŽฏ, and a leaner vocabulary ๐Ÿ“ฆ.

๐Ÿงฑ Real Word Examples

Word

Token Split

๐Ÿคฒ handcrafted

hand + crafted

๐Ÿง™ witchcraft

witch + craft

๐Ÿ† masters

master + s

๐Ÿ’ก Worth Noting

Fragment tokens like crafted typically don't carry a leading space โ€” a subtle signal that they're the middle or end of a word, not the start of a new one.


๐Ÿ”ข Numbers Are Also Tokenized

Numbers aren't special โ€” they get chunked just like words!

๐Ÿ”ฌ 3.141592653589793 โ†’ 3. + 141 + 592 + 653 + 589 + 793

๐Ÿ’ก Fun fact: GPT vocabularies include tokens for every number from 000 to 999 โ€” meaning any 3-digit number is just one single token. ๐ŸŽฏ

๐Ÿค– Why Old GPT Models Struggled at Math

Problem Type

Difficulty

๐ŸŸข 3-digit math

Easier

๐Ÿ”ด 4-digit math

Harder

Larger numbers split across multiple tokens, making relationships harder to learn. Modern models handle this much better. โœ…


๐Ÿ“ Token-to-Word Ratio

A handy rule of thumb:

๐Ÿ”ค 1 token โ‰ˆ 4 characters or 0.75 words

Tokens

Approx Words

1,000

~750 words

1,000,000

~750,000 words

๐Ÿ“š The Complete Works of Shakespeare (~900,000 words) is roughly 1.2 million tokens โ€” a popular benchmark for understanding token scale.


๐Ÿ’ฐ Tokens Have a Real Cost

AI APIs charge based on input and output tokens. Understanding tokens helps you:

  • ๐Ÿ’ต Estimate API costs

  • ๐Ÿ“ Manage context window usage

  • ๐Ÿšง Stay within model limits


โš ๏ธ Code Uses More Tokens

Programming and technical content is token-heavy due to:

  • ๐Ÿ”ก Uncommon variable names

  • ๐Ÿ”ฃ Dense symbols and syntax

  • ๐Ÿ“ฆ Compressed expressions

const userAuthenticationHandler = ... can split into many fragments ๐Ÿ˜ฌ


๐Ÿงช Try It Yourself!

Play around with tokenizer tools to:

  • โœ‚๏ธ See how text splits

  • ๐Ÿ”„ Compare different models

  • ๐Ÿงฎ Estimate token counts

This builds real intuition for prompt engineering ๐Ÿ› ๏ธ, cost optimization ๐Ÿ’ฐ, and context management ๐Ÿง .