S0 · #8, Skill Lesson, 18 min, Published

Tokens and Context Windows Explained

Track Your Progress

Watched it and tried the exercises? Mark it done: it counts toward your credentials.

About This Lesson

Every AI model reads and writes in tokens, not words. A sentence is cut into pieces, the pieces become numbers, and the answer comes back the same way. This lesson explains tokens and context windows from zero, the generative AI principles a Laboratoire SVR job ad for an AI Solutions Engineer asks for, because tokens decide what a model costs, how much it can read at once and how fast it answers.

It starts with one sentence cut into tokens on OpenAI's Tokenizer. The same ibuprofen label paragraph takes 39 tokens in English and 68 in French, and a rare drug name, trastuzumab deruxtecan, takes 7. Then the price of a token, in and out, and the context window as a desk that holds the system prompt, the conversation, your message and the answer, until the oldest messages have to go.

A second part is about choosing a model: what the number in a name like Mistral 7B means, closed frontier models against open-weight models on cost, control, privacy and hardware, and the providers behind them. The mini project, Measure and Choose, has you count a label in two languages, estimate a pharmacy chatbot's daily cost, check a 300-page manual against a 128,000-token window and choose a model for three jobs.

What You'll Learn

  • What a token is, and why a word can be several of them
  • Why French costs more tokens than English
  • How to estimate what tokens cost
  • What a context window is, and why a long chat forgets
  • Closed frontier models versus open-weight models
  • How to answer an interview question on tokens, cost and context length
Free Workbook

Practise This Lesson on Paper

  • The whole lesson, step by step
  • Exercises and homework, with answers
  • Interview questions, with model answers
  • A reading list to go deeper

PDF, 30 pages, Version 1.0

Get the Free WorkbookFree account, no payment.

Sources and References

Everything this lesson states was checked against these sources before it was published.

1 Job Posting5 Documents and Papers3 Books

Job Postings in the Lesson

  • Knowledge of generative AI principles and a strong interest in conversational assistants, augmented document search engines and AI agents.

    AI Solutions Engineer, Laboratoire SVR, Paris. LinkedIn, 2026, archived posting; quoted line translated from French.

Official Documents and Papers

  • What are tokens and how to count them?

    OpenAI Help Center, 2026. What a token is, rules of thumb for counting, and the Tokenizer page used in the lesson. Token counts in the lesson: o200k_base, 20 November 2026; French translations ours.

  • API pricing

    OpenAI developer documentation, 2026. Standard tier, read November 2026: input and output tokens are priced separately, output higher.

  • Models overview

    Anthropic documentation, 2026. Current Claude models with their context windows and prices.

  • Long context

    Google, Gemini API documentation, 2026. How very large context windows work, and what to put in them.

  • Ibuprofen 200 mg Drug Facts labels

    DailyMed, US National Library of Medicine, 2026. The "Uses" paragraph counted in two languages in the mini project. The ibuprofen example is practice in counting, not medical advice.

The Books Behind This Lesson

This lesson was researched from these books. Read them to go deeper:

Corrections

None so far. If you spot an error, tell us in the comments under the video or through the contact form.

Job postings belong to their companies; ai2hired is not affiliated with them. How sources are chosen and checked.

Transcript

Read the Full Transcript

The Job Line: Generative AI Principles

You'll find this line in job descriptions for several roles, on LinkedIn and on job boards. Knowledge of generative AI principles and a strong interest in conversational assistants, augmented document search engines and AI agents, and every one of them runs on tokens. By the end of this video, you'll know the pieces every model reads, how much text it can hold at once, and how to pick the right model for a task.

Tokens and Context Windows Explained

This is tokens and context windows explained, so let's get into it.

The Big Picture: Inside the Engine

You've met this car in our earlier lessons, where the application is the whole car and the model is the engine that makes it go. Today we lift the hood and look inside that engine, which is a large language model. On the left, your text goes in. Along the top runs the path that turns it into an answer, one small piece at a time. The whole machine stands on the ground it was built from, which a later season digs into. Two parts matter to us today, starting with the pieces your text is cut into.

The second is the glass tray along the bottom, the context window, which holds every piece the model can see at once. Today we'll measure a real text and choose a model for it, and every part you don't recognize yet will get its own lesson later in the course.

Two Surprises, One Cause

Here are two things that surprise almost everyone who works with AI assistants for a while. In a very long chat, the assistant can seem to forget what you told it at the start. And when a company builds an app on a model, the bill grows with every message, even when the questions stay short. Both have the same cause, and once you see it, you can predict both before they happen.

What a Model Reads

Before I remind you, try to answer this from our lesson on what AI really is. What does a large language model actually do with your text? It guesses what comes next, one step at a time, then adds that guess and guesses again. Back then I said that, strictly speaking, it reads and writes in small pieces, and today we open those pieces up. Those small pieces are called tokens. A token is a piece of text, often a whole word, sometimes part of a word or a punctuation mark, and it's the unit a model reads, counts and charges for.

So what does this do for you? Once you think in tokens, you can count what a text will cost, and you can tell before you paste it whether it will fit. You'll meet tokens on every price page and in every model's description, and right now we'll climb to them one small step at a time.

One Sentence, Nine Tokens

Let's start with one plain sentence, take one tablet twice a day with food. A model can't read letters the way you do, so the first thing that happens is that this sentence is cut into pieces. Here it's nine tokens, one for each word and one for the full stop at the end. The program that does the cutting is called a tokenizer, and every model comes with its own.

Pieces Become Numbers

Here's the second step, and this one surprises people. The model doesn't work with these pieces as words either, because each token is swapped for a number, its place in a long list. That list is the tokenizer's vocabulary, the full list of pieces a tokenizer knows, and the tokenizer behind OpenAI's current models knows about two hundred thousand of them. The model works only with these numbers. When it answers, it writes numbers too, and the tokenizer turns them back into words for you, which is why we call it a round trip.

How a tokenizer decides where to cut is a story of its own, and we'll open it up in the tokenization lesson in Season 7.

Same Meaning, Different Counts

Here's the same instruction in French, and before the count appears, guess whether it needs more tokens or fewer. It needs thirteen, four more than the English, even though it says exactly the same thing. Most tokenizers learned mostly from English text, so English words are often one piece each, while other languages get cut into more of them. That's why the rule of thumb you'll read everywhere, that a token is about three quarters of an English word, really only holds for English.

Words the tokenizer rarely saw get cut even finer. A short word like dog is a single token, but trastuzumab deruxtecan, the name of a cancer medicine, comes out as seven. The same goes for long numbers, product codes and computer code. A different model with a different tokenizer can also give a different count for the very same text, so you always count with the tokenizer of the model you'll use.

Tokens Cost Money

Now, why does all this counting matter? Companies that build apps on a model reach it through an API, a doorway that lets one program ask another for something. They pay for every token that goes through that doorway. On one current OpenAI model, for example, a million tokens going in costs about two dollars, and a million tokens coming out costs about ten dollars. Writing an answer takes more work than reading a question, so output usually costs more.

Tokens cost time as well, because the model writes its answer one token after another, so a longer answer takes longer to arrive. That gives us a rule we'll come back to many times in this course, and it sounds simple. Every token has a cost, in money and in time, so a professional counts before sending.

The Context Window

Now for the second idea, and it's the one behind the chat that forgets. A model can only look at a limited number of tokens at once, and that limit is called its context window. Picture a desk that holds only so many pages. Everything the model works with for one answer has to fit on that desk at the same time. Here's what goes on it. First comes the system prompt, the hidden instructions an app gives the model before your message.

Then comes the whole conversation so far, then your new message, and finally the answer itself, because it takes up space on the desk too.

When the Desk Is Full

So what happens when a long chat fills the desk? Depending on the app, the oldest messages are dropped or squeezed into a short summary, or the request is simply refused. That's why the assistant seemed to forget your first message, which was no longer on the desk. Here's where the desk stops being a perfect picture, though. The model doesn't choose which pages stay on the desk, because the app around it makes that choice.

Context windows have grown enormously in a few years. Early models held about eight thousand tokens, while today's large models hold hundreds of thousands, and some about a million, which is roughly eight average novels on one desk. A bigger desk still costs more to fill, because every token on it is paid for.

Hands-On: Counting Real Tokens

Let's count real tokens on a real tokenizer. OpenAI publishes a free page where you can paste any text and watch it being cut, and you don't need an account to use it. I'll paste the sentence from a minute ago, take one tablet twice a day with food. Each colored block is one token, and the counter shows nine, just as we saw. Next comes the French version of the same sentence, and the counter climbs to thirteen. And finally the medicine name, which the page splits into seven colored pieces, right next to the single block for dog.

This is also why longer prompts aren't free. Think of the prompt card you built in our prompting lesson, with its task, context, role, format and examples. Every one of those slots is made of tokens, and they get counted every single time you send it. A card of about three hundred tokens sent a hundred times becomes thirty thousand tokens, which is still cheap, but now you can see where the cost comes from.

Choosing a Model

So far we've looked inside one engine. Now let's step back to the car, because when you build an app, choosing the engine is one of your first real decisions.

Size in the Name

Many model names carry a number with a B, like Mistral seven B or Llama eight B. That number counts the model's parameters, in billions. A parameter is one of the numbers a model adjusted during training, and together they hold everything it learned. So seven B means seven billion of those numbers. You'll find the size on the model card, the page where a model's makers describe what it is, how it was trained and its limits.

Within one family, more parameters usually means a more capable model, but a newer small model can beat an older large one, and most closed providers don't publish their sizes at all.

Closed or Open-Weight

Now the biggest choice of all, which is whether to use a closed model or an open one. The most capable models of the moment are called frontier models, and most of them are closed. You use them through the provider's app or API, you pay per token, and you never see or change the model itself. An open-weight model, which people often call open-source, is one whose parameters you can download and run yourself, on your own machine or in your own cloud.

Let's compare them on four things. On cost, closed means a bill per token, while open means no token bill but a machine to pay for. On control, an open model can be adapted to your task, and a closed one stays as it is. On privacy, with a closed model your text goes to the provider, while an open model can keep everything inside your walls. And on hardware, a closed model needs nothing from you, while a large open model needs a powerful computer with GPUs, the chips first built for video games that AI runs on.

It also needs enough VRAM, the memory on that chip, which decides how big a model it can hold.

The Providers

Here are the providers you'll see most in job ads. OpenAI makes the GPT models, Anthropic makes Claude, and Google makes Gemini, and these are best known for their closed frontier models. Meta releases Llama as open-weight models, and the French company Mistral offers both kinds. In fact several providers now release open models next to their closed ones. One warning before you download anything, because open doesn't mean free for any use.

Each open model comes with its own license, some allow almost anything while others add conditions, so you read the license before you build on it. And remember the idea of using the simplest tool that solves the problem. For many tasks a small open model is the right engine, cheaper and private, and the biggest frontier model would be wasted. You'll run an open model on your own computer in the lesson on open models, in Season 8.

Common Mistakes

The first mistake is believing that one token is one word. As you saw with the French sentence and the medicine name, a word can be several tokens, so a professional counts with the model's own tokenizer instead of guessing. The second is thinking that a bigger number in the name is always better. A bigger model costs more and answers more slowly, so the fix is to pick the smallest model that does your task well, and to test it on your own examples.

The third is saying that pasting your documents trains the model. Training happened long before, in pretraining, the first, huge stage of learning, when the model learns from text by guessing the next piece. What you paste only lands on the desk again, in the context window, for that one conversation, and the model itself stays exactly as it was. And the fourth is assuming that open means free for any use, when the real answer always sits in the license you read before building.

Mini Project: Measure and Choose

Now it's your turn, with a mini project in four steps. We'll use the same label you checked on DailyMed in our lesson on AI assistants, the Drug Facts label of an ibuprofen tablet. We'll measure its uses paragraph, and then choose a model for three jobs. First, paste the uses paragraph into the tokenizer, written as one sentence in English, and watch the counter while the text goes in. When the whole paragraph is in, the counter stops at thirty-nine tokens.

Then paste our own French translation of the same paragraph, and watch the counter again while the text goes in. This time the count settles at sixty-eight, which is about three quarters more for the same meaning. The second step is to estimate the cost, and one paragraph costs only a tiny fraction of a cent, so let's think like a company instead. Imagine a pharmacy chatbot that sends the whole label, about fifteen hundred tokens, with each of ten thousand questions a day.

That's fifteen million tokens, or about thirty dollars a day, before a single answer is written. The third step is to check the window, and the label easily fits on any modern desk. A three-hundred-page manual of about two hundred thousand tokens is another story, because it wouldn't fit on a desk of a hundred and twenty-eight thousand. For that one, you'd need a bigger window or a smaller piece of the text. Finally, choose a model for three jobs, with one reason each.

When a hospital needs a model for its private patient notes, an open-weight model running on its own machines keeps the data inside its walls. For a public FAQ bot that must answer well from day one, a closed frontier model is quick to start, and the text it sees is public anyway. And for a student's homework helper, a small model of either kind is cheap and good enough. With those three choices made, the whole project is done.

You've measured a real text and chosen a model the way a professional would, with a number and a reason.

Interview Angle

Here's a question that comes up in AI interviews. What is a token, and why does it matter for cost and context length? A strong answer goes like this. A token is the piece of text a model reads and writes, counted by that model's own tokenizer. Providers bill per token, with output usually costing more than input, and the context window limits how many tokens fit at once, prompt, history and answer together.

Recap

Let's put it all together. A model reads your text as tokens, pieces cut by its own tokenizer and turned into numbers, and the count changes with the language and the model. Every token has a cost in money and time, and the context window is the desk that holds every token the model sees at once, answer included. Closed frontier models are easy to start, while open-weight models give you control and privacy, if you have the hardware and read the license.

The exercises, further reading and interview questions for this lesson are in the free workbook on ai2hired. org, linked in the description. And the most useful habit from today is simple to keep, which is to count before you send.

Your New CV Line

Here's your new CV line. Estimate token usage and cost for an LLM use case, and choose between closed and open-weight models. It's yours to add, starting today.

Never Miss a Lesson

Subscribe on YouTube, or get an email the day each new lesson goes live.