PTENES
MODULE 1.3

🪟 Context, tokens, and window

Learn how the agent's working memory works, what tokens are, why the context window has a limit, and how not to waste it.

6
Topics
40
Minutes
Basic
Level
Theory
Type
1

📝 What context is

O context is everything the model knows at the moment: your message, the previous conversation, the files the agent has read, and the tool results. It’s the session’s active “working memory” — and it has a limit.

CONTEXT WINDOW — 200,000 tokens System ~2K tok CLAUDE.md ~5K tok Conversation history grows over time Tool results (files read) accumulates with every Read/Bash free When approaching the limit → automatic compaction or quality degradation fixed persistent volatile biggest culprit

📝 What goes into the context

  • •System prompt: Claude Code internal instructions (~2K tokens, fixed)
  • •CLAUDE.md: your custom project instructions (you control them)
  • •History: the entire conversation since the start of the session
  • •Tool results: the contents of each file read, the output of each Bash command
System prompt
Claude Code base instructions
CLAUDE.md
Your persistent context
History
Accumulated conversation
Tool results
Contents of files read
2

🔢 What Tokens Are

Tokens are the smallest units of text that language models process. They aren’t exactly words or letters—they’re text fragments that the tokenizer splits based on frequency in the language.

📊 Token reference

  • 1 English word ≈ 1.3 tokens
  • 1 word in pt-BR ≈ 1.5-2 tokens
  • 100 lines of code ≈ 800-1,200 tokens
  • 1 page of text (A4) ≈ 500-700 tokens
  • 1KB file ≈ 250-400 tokens
  • 10KB file ≈ 2,500-4,000 tokens
  • 100KB file ≈ 25,000-40,000 tokens
  • Typical CLAUDE.md ≈ 500-2,000 tokens

✓ Efficient token use

  • ✓Code in English/symbols
  • ✓Concise, direct text
  • ✓Code without long comments
  • ✓JSON without extra spaces

✗ Uses too many tokens

  • ✗Copying entire files into the message
  • ✗Text in languages with long words
  • ✗Repeating the same content several times
  • ✗Verbose agent responses
Tokenizer
Algorithm that splits the text
Input
Tokens you send
Output
Tokens the model generates
Cost
Cost per 1M tokens
3

🪟 Context window

A context window is the maximum number of tokens the model can process at once. It's like a computer's RAM — fast, but finite. Claude 3 Sonnet has 200K tokens.

🪟 200,000 tokens in perspective

It seems like a lot. It is—but it fills up quickly during long sessions with lots of code reading.

  • •≈ 500 pages of dense text
  • •≈ 20,000 lines of code
  • •≈ 40-60 medium-sized code files

With a long session, you can use this in:

  • ⚠️1 500KB file read = 125K tokens
  • ⚠️10 50KB files = the same thing
  • ⚠️A 2-hour session with lots of debugging = full

💡 Tip: When the context is full

Signs the context window is nearly full: the agent starts to "forget" instructions from the beginning, does things you already said not to do, or the answers become more generic. Use /compact or /clear.

200K tokens
Claude 3 Sonnet window
Truncation
Remove old parts when things get full
Degradation
Quality degradation near the limit
RAM analogy
Working memory, not a hard drive
4

📈 Why the context fills up

The window doesn't fill up randomly. There are common culprits that consume tokens disproportionately—and identifying them lets you act before losing critical context.

1°

Large files read in full

A 500KB file uses ~125K tokens — more than half the context window. The agent reads more than it needs when there are no specific instructions.

2°

Long conversation history

Every message stays in the context. A 2-hour session with many exchanges can easily accumulate 30-50K tokens just in the history.

3°

Many tool calls with long output

Each Bash(npm install) with long output goes straight into the context. Logs with 10K lines are common.

4°

Verbose agent responses

Does the agent explain every step in detail? That also fills up the context. Configure this in CLAUDE.md: "keep your responses concise".

Large file
Biggest individual culprit
Long history
Accumulates over the session
Tool output
Long-running command logs
Verbose
Unnecessarily long responses
5

🗜️ Context Compaction

Claude Code has mechanisms for handling full contexts: the command /compact (manual) and automatic compaction that happens when the window is almost full.

✓ /compact — when to use it

  • ✓After completing a feature
  • ✓When the history gets too long
  • ✓Before starting a new subtask
  • ✓When you notice a decline in quality

✗ Be careful with /compact

  • ✗Details of previous errors may be lost
  • ✗Don't use in the middle of a critical task
  • ✗The summary may lose important nuances
  • ✗Some contexts are better kept intact

⌨️ Context management commands

/compact — summarizes the conversation and frees up space
/clear — completely deletes the history
/tokens — shows how many tokens are in use
# CLAUDE.md as persistent context:
echo "# Project Context" > CLAUDE.md
echo "Stack: Node.js + TypeScript + Prisma" >> CLAUDE.md
/compact
Summarize and free up context
/clear
Clear history
Auto-compact
Triggered when reaching ~95%
CLAUDE.md
Doesn't disappear after compaction
6

💡 Context best practices

Managing context well is one of the skills that most distinguishes advanced users from beginners. A few simple rules have a huge impact on session quality and cost.

📋 The 5 golden rules of context

  • 1.Short, focused sessions: one session per feature/bug, not per day
  • 2.CLAUDE.md for fixed context: stack, coding rules, project conventions
  • 3.Don't paste code into messages: say "edit file X" instead of copying the content
  • 4./compact proactively: use before starting a new task, not when the window is already full
  • 5.Ask for concision: include in the CLAUDE.md "be direct and concise in your responses"

💡 The perfect CLAUDE.md for saving context

A good CLAUDE.md has 3 sections: (1) Project context — technologies, structure, architecture; (2) Code rules — style, conventions, standards; (3) Session instructions — "be concise," "ask before creating files."

This eliminates the need to repeat the context in every session, and the agent starts being productive immediately.

Focused session
One task per session
CLAUDE.md
Replaces context repetition
Path ref.
"Edit src/x.ts" don't paste code
Compact early
Before filling up, not afterward

✅ Module 1.3 Summary

✓
Context = working memory — everything the model knows in the session
✓
Tokens are fragments of text — ~1.5 tok/word in pt-BR
✓
200K-token window — fills up quickly with large files
✓
Culprits: large files and long history — monitor and compact early
✓
CLAUDE.md is the solution — persistent context without repetition

Next Module:

1.4 — Terminal and CLI for beginners: go from zero to using the terminal in 40 minutes, including the commands Claude Code uses most.