LESSON 16 Β· TOKENS & CONTEXT

Token Limits

A model can only process a finite amount of tokenized information in one request. Learn how input, output, and context capacity interactβ€”and how to design applications that stay within the limit.

14 min readβ€’Beginnerβ€’Generative AI

By the end of this lesson, you will be able to:

  • Explain what a token limit is and why it matters.
  • Distinguish input tokens, output tokens, and the total context used by a request.
  • Estimate whether a prompt plus expected response can fit within a model's capacity.
  • Use Python to count tokens with a model-appropriate tokenizer.
  • Apply practical strategies when content is too large for one request.
1

What Is a Token Limit?

A token limit is the maximum amount of tokenized context a model can handle for a request. The exact limit depends on the model and API, so it should never be assumed from another model.

Simple idea:Think of the context capacity as a desk. Your instructions, conversation history, retrieved documents, and generated answer all need space on that desk.
Context capacityModel-specific maximum
Input tokensOutput tokens
Prompt + history + tools + retrieved contextGenerated response

Important: A token limit is not the same thing as a character limit or word limit. Tokenization determines how much text consumes the available capacity.

2

Input Tokens and Output Tokens

For a chat application, the model may receive much more than the latest user message. Previous messages, system instructions, retrieved documents, tool results, and other context can all consume input capacity.

INPUT

What you send

Instructions, conversation history, documents, examples, and tool results become part of the model's input context.

OUTPUT

What the model generates

The response also uses tokens. Reserving enough output space is important when the answer may be long.

Used contextinput tokens + output tokensmust fit the model's applicable capacity
3

A Practical Budget Example

Suppose a hypothetical model has a context capacity of 8,000 tokens. Your application sends 5,600 input tokens and asks for up to 1,500 output tokens.

Capacity8,000maximum tokens
Input5,600already used
Output budget1,500requested maximum
Total planned7,100fits with 900 tokens left

This is a planning example, not a statement about any specific production model. In real applications, use the current model's documentation and tokenizer.

4

What Can Consume Your Context?

Token limits become easy to hit when an application keeps adding information to every request.

System instructionsRules, role, formatting requirements, safety instructions
Conversation historyEarlier user and assistant messages
Retrieved contextDocuments, database results, or RAG chunks
Tool resultsAPI responses, search results, structured data
Current requestThe latest user question and attached context
Generated answerThe response consumes output capacity too
5

How Do You Know How Many Tokens You Have?

Do not estimate exact token counts from characters alone. Use the tokenizer associated with the model whenever the exact count matters.

</> Python
from transformers import AutoTokenizer

# Use the tokenizer that matches the model family you are evaluating.
tokenizer = AutoTokenizer.from_pretrained("gpt2")

text = "Explain why token limits matter for an LLM application."
tokens = tokenizer.encode(text, add_special_tokens=False)

print("Token count:", len(tokens))
print("Token IDs:", tokens)

Why this matters: the number is tokenizer-specific. The GPT-2 tokenizer above is a teaching tool for counting tokens; do not use its count as an exact count for a different model.

6

What Happens When You Exceed a Limit?

An application cannot simply keep adding tokens forever. Depending on the API and request, an oversized request may be rejected, truncated, or require you to reduce the context before sending it.

Too much context

Full chat history + every document + large tool output + long answer budget.

capacity exceeded
β†’

Managed context

Keep relevant history, retrieve only useful chunks, summarize older turns, and reserve output space.

fits the budget
7

Strategy 1: Trim or Summarize History

Long-running chat applications can accumulate thousands of tokens. Instead of sending every previous message, keep recent turns and summarize older information.

Old conversation20,000 tokens
↓
Conversation summary1,200 tokens
+
Recent turns1,000 tokens

The goal is not to delete useful information blindly. Preserve facts the application actually needs and remove redundant history.

8

Strategy 2: Retrieve Only Relevant Context

RAG systems should not paste an entire knowledge base into the prompt. Retrieval narrows a large collection to a smaller set of relevant chunks.

10,000 documentslarge knowledge base
β†’
Top relevant chunkssmall context
β†’
LLManswers using selected context
Engineering rule:More context is not automatically better. Relevant context is usually more useful than a large amount of unrelated text.
9

Strategy 3: Control the Output Budget

If your request uses most of the available capacity for input, there may not be enough room for a long response. Set a sensible output limit for the task.

TaskTypical output goal
Classificationshort label or JSON
Short summaryfew paragraphs
Code generationdepends on requested code
Long reportmay require multiple steps

When a response genuinely needs to be long, consider generating it in stages rather than forcing one request to do everything.

10

Real Application: Context Budgeting

Imagine a company assistant with a model capacity of 16,000 tokens. Your request contains 2,000 tokens of instructions, 4,000 tokens of recent conversation, and 7,000 tokens of retrieved documents. You reserve 2,000 tokens for the answer.

Instructions2,000
History4,000
Retrieved7,000
Output2,000
15,000 planned tokensThat leaves about 1,000 tokens of headroom in this hypothetical example.

Now imagine retrieval grows to 10,000 tokens. The same request would plan for 18,000 tokens and no longer fit. A good application notices this before sending the request and reduces, summarizes, or reranks the context.

PRACTICE

Build a Token Budget Checker

Write a small Python function that accepts a context capacity, input token count, and output budget. Return whether the planned request fits and how much headroom remains.

βœ“ capacityβœ“ input tokensβœ“ output budgetβœ“ remaining headroom
QUICK QUIZ

Test yourself

Which strategy is most appropriate when a RAG application retrieves far more text than the model needs?

30-Second Recap

  • Token limits describe how much tokenized context a model can handle for a request.
  • Input includes more than the latest user message: history, instructions, retrieved text, and tool results can all consume tokens.
  • Output tokens also need room, so budget for the response.
  • Exact token counts depend on the tokenizer; use the model-appropriate tokenizer when precision matters.
  • When context is too large, trim history, summarize, retrieve fewer relevant chunks, rerank, or split the task into multiple steps.