
Tokens and Context: The Currency of AI Thought
Master the fundamental building blocks of LLM inference. Understand how tokens, context windows, and embeddings shape the responses you receive from models like Claude 3.5 and Amazon Titan.
Tokens and Context: The Currency of AI Thought
In the previous module, we explored the "what" and "why" of prompt engineering. Now, we dive into the "how." To a human, a prompt is a sequence of words. To a Large Language Model (LLM), a prompt is a sequence of Tokens, processed within a fixed Context Window.
If the prompt is the program, then tokens are the machine code, and context is the RAM. Understanding these two concepts is critical because they directly impact the cost, latency, and accuracy of your AI applications. In this lesson, we will peel back the layer of natural language and look at the mathematical reality of tokens and context.
1. What is a Token? (The Atomic Unit of AI)
A common misconception is that LLMs read words. They do not. They read Tokens.
The Tokenization Process
When you send text to a model, the first thing it does is run it through a Tokenizer. This breaks the text into the smallest units the model can recognize.
- Short words are often a single token (e.g., "Apple").
- Long or complex words are split into multiple tokens (e.g., "Tokenization" might be
Token+iz+ation). - Whitespace and Punctuation are also tokens.
The Rule of Thumb
In English, a good estimate is: 1,000 tokens ≈ 750 words.
graph LR
A[Raw Text: 'Prompt Engineering'] --> B[Tokenizer]
B --> C[Token IDs: 4235, 8812, 12]
C --> D[Model Brain]
Why Tokenization Matters for Prompt Engineering
- Cost: services like AWS Bedrock charge you per 1,000 tokens. If you are verbose, you are wasting money.
- Model Bias: Tokenization can sometimes be biased. For example, rare languages or technical code (like dense Regex) use more tokens per character, making them "more expensive" and harder for the model to process accurately.
- Caps: Every model has a
max_tokenslimit for its output. If your prompt utilizes too much of the context, the model won't have "room" to finish its answer.
2. The Context Window: The AI's Working Memory
The Context Window is the maximum number of tokens a model can "see" or "remember" at any one time.
Limits and Evolution
- Early Models (GPT-3): 2,000 - 4,000 tokens.
- Modern Models (Claude 3.5): 200,000 tokens.
- Gemini 1.5 Pro: 1,000,000+ tokens.
The Problem with Large Contexts
Just because a model can see 200,000 tokens doesn't mean it's equally good at all of them.
- Latency: The more tokens you send, the longer the model takes to "read" before it starts generating.
- Memory Decay: Information in the "middle" of a long prompt is often ignored (the "Lost in the Middle" phenomenon).
3. Embeddings: Turning Text into Space
Once tokens are identified, they are converted into Embeddings. This is where the magic happens. An embedding is a vector (a list of numbers) that represents a token's meaning in a high-dimensional space.
Visualizing Meaning
Imagine a 3D space:
- X-axis: King vs. Queen (Gender).
- Y-axis: Man vs. Woman (Gender).
- Z-axis: Person vs. Concept (Concreteness).
In this space, the "distance" between King and Queen is almost identical to the distance between Man and Woman. The model understands relationship via Spatial Geometry.
graph TD
A[Token: 'King'] --> B[Embedding Vector]
B --> C[Location in High-Dimensional Space]
C --> D[Find Nearest Neighbors: 'Monarch', 'Throne', 'Queen']
4. Prompt Engineering at the Token Level (Python Examples)
In a professional stack using FastAPI and LangChain, we often need to "calculate" tokens before sending them to the model to avoid hitting limits or exceeding budgets.
Python Example: Token Counting with tiktoken
import tiktoken
from fastapi import FastAPI
app = FastAPI()
def count_tokens(text: str, model="gpt-4"):
# Load the specific tokenizer for the model
encoding = tiktoken.encoding_for_model(model)
num_tokens = len(encoding.encode(text))
return num_tokens
@app.post("/estimate-cost")
async def estimate(prompt: str):
tokens = count_tokens(prompt)
cost = (tokens / 1000) * 0.03 # Estimated cost per 1k tokens
return {
"token_count": tokens,
"estimated_cost_usd": f"${cost:.4f}"
}
By counting tokens programmatically, you can implement Auto-Summarization. If a user's prompt is too long, you can use a smaller model to summarize it before the final "Big" model processes it. This is a core pattern in Token Efficiency.
5. Deployment: Context Management in Production
When you move your AI application to Docker and Kubernetes, you need to manage your "Context Strategy."
Strategy 1: Sliding Window
In a long chat conversation, you only send the latest 10 messages to the model. This keeps the token count low and the response time fast.
Strategy 2: Prompt Caching (AWS Bedrock)
If you have a 100,000-token PDF that every user is asking questions about, you shouldn't send those 100k tokens every time. Using Prompt Caching, you "upload" the PDF once, and then every subsequent query only sends the small user prompt. You save money and reduce latency.
6. The "Invisible" Tokens: Control Characters
Did you know that prompts have "invisible" tokens?
<|endoftext|>: Tells the model the prompt is over.- \n: Newline tokens often define the boundary between a "System" message and a "User" message.
Understanding these helps you debug why a model might be "leaking" part of its response or cutting off early.
7. SEO and Information Architecture
Just as a model uses tokens to navigate meaning, a search engine uses keywords and metadata to navigate relevance. When writing prompts that generate SEO content, you must ensure the model understands the Token Density of your keywords. A prompt that asks for "Natural integration of the keyword 'Cloud Architecture'" is far more effective than one that just says "Use the keyword 5 times."
Summary of Module 2, Lesson 1
- Tokens are the machine language of AI: 1k tokens ≈ 750 words.
- Context Windows are limited: Managing them is the key to cost and latency.
- Embeddings map meaning spatially: The model "sees" distance between concepts.
- Always count tokens in code: Don't guess; use libraries like
tiktokento be precise.
In the next lesson, we will look at Instructions vs Information—how to ensure the model knows what is an "Order" and what is just "Data."
Practice Exercise: Token Analysis
- Count Tokens: Use an online tokenizer (like OpenAI's Tokenizer tool) to see how many tokens are in your favorite poem.
- Test Complexity: Compare a sentence in English with the same sentence in another language. Which one uses more tokens? Why?
- Python Implementation: (Optional) Write a FastAPI endpoint that takes a long string, splits it into 1,000-token chunks, and returns them as a list. This is the foundation of Chunking in RAG systems.
Mastering the currency of AI is the first step toward building profitable, high-scale AI products.