Skip to Content

Context windows and prompts

A model sees only what's in its context window, rebuilt by your app on every call. Watch a prompt get assembled, counted and trimmed, and see a key fact fall out.

9 minFree

Context windows and prompts
  1. Build prompt
  2. Count tokens
  3. Trim
  4. Generate
  5. Save
FIG. AI-CONTEXT-WINDOWS / 06 PARTSTravellerchat userYour appbuilds the promptInstructionssystem instructionChat historyevery past turnToken budgetcount_tokensGeminisees only the prompt

Press Play to watch it run, or step through with ← →. Then switch to Practice and run it yourself.

00/09

What the model sees

Nothing sent yet. Press Play.

Background: the idea in plain words1 min read

A model has no memory between calls. Each request starts from nothing. Yet a chat app seems to remember what you said five minutes ago. How?

What the model can use is the context window: all the tokens (small pieces of text) you send in one request. Google's docs compare it to short-term memory. The app stores the conversation and sends it again with every new message.

In a chat app, the prompt (the full text sent to the model) that the model really sees is usually several parts joined together:

  • System instruction: your fixed rules ("You are a trip planner. Keep answers short.").
  • Conversation history: earlier messages, sent again every time.
  • Extra context: documents, search results or tool results you add.
  • The new message from the user.

A tiny example. A traveller tells a trip-planning bot "I'm vegetarian". Many messages later, the traveller asks for dinner ideas. The bot can suggest vegetarian food only if that early message is still inside the prompt.

Many Gemini models have context windows of 1 million tokens or more. Google's docs give examples of what that holds: about 50,000 lines of code, 8 average-length English novels, or transcripts of over 200 podcast episodes. That's a lot, but it isn't free: you pay for every token on every call, and bigger prompts take longer. So an app often sets its own smaller limit and has to choose what to leave out when a chat gets long.

Press Play to watch a short chat. Then switch Try it to Long chat: oldest turns dropped, and finally to keep a summary of key facts.

What to notice

  1. The model only knows what's in the window. No hidden memory, no other chats. If a fact isn't in the prompt (and wasn't in its training data), the model can't use it.
  2. "Memory" is your app re-sending history. The bot seems to remember because your code stores each turn and sends it again. See Training an LLM: chatting never changes the model itself.
  3. What falls out is your choice. Dropping the oldest turns is simple but loses important facts. Summarising old turns, or storing key facts separately, keeps them in far fewer tokens.
  4. Output counts too. The reply is generated into the same request. On Gemini 4 models, the 1M-token window is shared by input, thinking and output tokens. Earlier Gemini 3 and 2.5 models have separate input and output limits.
  5. A big window isn't perfect recall. Google's docs report that models find a single fact in a long context very well, but accuracy drops when you need many separate facts from it at once.

Reusing a long context If many requests share the same large context, such as one long manual, look at context caching. Google's docs call it the main optimisation for long-context work: you pay less for cached input tokens than for sending them fresh each time.

Go deeper

Words to know

1 min read
WordMeaning in simple words
Context windowAll the tokens a model can take in one request. It is the only thing the model can see
PromptThe full text your app sends to the model for one request
System instructionYour fixed rules for the model, sent with every request
Conversation historyThe earlier messages of the chat, which your app stores and sends again
TurnOne step of a chat: a user message and the reply to it
Token budgetA limit your own app sets on prompt size, to control cost and speed

Check your understanding

1. A user said “I'm vegetarian” at the start of a long chat. Your app keeps only the last 20 turns. What happens?
2. Your model supports 1 million tokens. Why might you still keep prompts much smaller?
3. Which is the best way to keep important facts from a very long conversation?
Sources: the official documentation3 links

This lesson is based on the official documentation. Check it for the latest details.

Something wrong or unclear in this lesson? Tell us, it gets fixed.