# Tokens and Context Windows

> Models read and write tokens, not characters or words; tokens drive cost, latency, limits and many strange failure modes. Free notes, key concepts, resources...

Canonical: https://www.internhack.in/roadmaps/tracks/ai-engineer/tokens-and-context
Last updated: 2026-11-28

Models read and write tokens, not characters or words; tokens drive cost, latency, limits and many strange failure modes.

Beginner · about 4 hours · difficulty 2/5

A language model never sees your text directly. A **tokenizer** splits text into tokens (common words, word pieces, punctuation, bytes) and maps each to an integer ID. Roughly, one token is about four characters of English, but code, non-English languages and numbers often use many more tokens per character.

### Why engineers care

- **Pricing** is per token, usually with output tokens costing several times more than input tokens.
- **Latency** grows with output length: tokens are generated one at a time.
- **Context window**: the maximum number of tokens (input plus output) the model can handle in one request. Modern models support very large windows, but everything you send costs money and time.
- **Quirks**: character counting, spelling, reversing strings and arithmetic can be unreliable because the model sees chunks, not letters.

### The context window is working memory

The model has no memory between API calls. Everything it knows about the conversation, your instructions and retrieved documents must be inside the context window of the current request. Chat apps work by resending the conversation history each turn. Long contexts also degrade: models can miss details buried in the middle of a very long prompt, so **put the important material where it is easy to find** and do not stuff the window just because you can.

### Good enough to move on

You can count tokens for a prompt with a provider's tokenizer or token counting endpoint, estimate the cost of a request, and explain why a chat app must truncate or summarize history.

## Key concepts

- **Tokenization (BPE and friends)**
- **Input vs output tokens**
- **Context window limits**
- **Counting tokens in code**
- **Lost-in-the-middle effects**
- **Conversation history truncation**

## Mini project

Build a small script that takes any text file, reports its token count with a real tokenizer, estimates the cost to send it to two different model tiers, and warns if it would exceed a chosen context budget.

## Check yourself

Why might a model fail to count the letters in a word correctly even though it can write fluent essays?

## Free resources

- [OpenAI Tokenizer playground](https://platform.openai.com/tokenizer) (OpenAI)
- [Let's build the GPT Tokenizer (Andrej Karpathy)](https://www.youtube.com/watch?v=zduSFxRajkE) (YouTube)
- [Hugging Face LLM Course](https://huggingface.co/learn) (Hugging Face)

Previous: [SQL and Postgres Basics](https://www.internhack.in/roadmaps/tracks/ai-engineer/sql-basics) · Next: [Transformers and Attention, Intuitively](https://www.internhack.in/roadmaps/tracks/ai-engineer/transformers-attention)

## More in How LLMs Work (An Engineer's View)

- [Tokens and Context Windows](https://www.internhack.in/roadmaps/tracks/ai-engineer/tokens-and-context)
- [Transformers and Attention, Intuitively](https://www.internhack.in/roadmaps/tracks/ai-engineer/transformers-attention)
- [How LLMs Are Trained and Aligned](https://www.internhack.in/roadmaps/tracks/ai-engineer/how-llms-are-trained)
- [Sampling, Temperature and Determinism](https://www.internhack.in/roadmaps/tracks/ai-engineer/sampling-and-decoding)
- [Reasoning Models and Thinking Budgets](https://www.internhack.in/roadmaps/tracks/ai-engineer/reasoning-models)
- [The Model Landscape and Choosing a Model](https://www.internhack.in/roadmaps/tracks/ai-engineer/choosing-models)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [AI Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/ai-engineer)
