1. ai
  2. /foundations
  3. /tokens-and-context

Tokens and Context Windows

Every file, rule, and message you send consumes tokens from a fixed context window. Budgeting context is the highest-leverage skill after writing clear prompts.

Last reviewed: June 2026

Context window sizes vary by model. Check Anthropic model docs and OpenAI model docs for current limits.

Prerequisites

Skim How LLMs Work for token prediction basics. This page focuses on practical budgeting in the IDE.

What Counts Toward Context

SourceTypical cost
System prompt + rules (AGENTS.md, .cursor/rules)Fixed per session — keep lean
Conversation historyGrows every turn — summarize or reset
@Files / @Codebase attachmentsProportional to file size
Tool outputs (MCP, terminal)Can explode quickly
Model responseCounts toward same window

Input + output share one limit on most models.

Rough Size Estimates

ContentApprox tokens
1 line of code5–15
100-line file800–1,500
Full package.json + lock excerpt2,000+
Long chat (20 turns)10,000+

Use provider tokenizers for accuracy: OpenAI tokenizer tool.

Priority Order (When Window Is Full)

When you must cut context, drop in this order:

  1. Old chat turns unrelated to current task
  2. Large generated outputs you already applied
  3. Broad @Codebase search results — replace with specific @Files
  4. Duplicate rules already in project defaults
  5. Never drop: security constraints, stack version, file boundary instructions

See Context Engineering for IDE tactics.

Attachment Strategies

StrategyWhen
@File specific pathsYou know the module
@Codebase searchUnknown location; narrow query
MCP doc searchInternal KB too large to attach
Paste error onlyDebug loop — not whole log file
New chatTopic changed; history is noise
Bad vs good context attachment

Bad: @Codebase + entire package-lock.json + 30-turn history for a one-line typo.

Good: New chat, @File src/components/Button.tsx, rules file loaded once, paste exact TS error.

Context Window by Use Case

TaskBudget guidance
Inline completionCurrent file + nearby imports only
Single-file bugfixTarget file + test file + error output
Cross-module refactorPlan mode first; attach 3–5 files max per agent step
Large migrationChunk by directory; multiple sessions

Symptoms of Context Pressure

SymptomLikely causeFix
Model "forgets" rules mid-chatWindow fullNew chat; shorten rules
Wrong file editedIrrelevant files attachedRemove @ mentions
Slower responsesHuge attachmentsSplit task
Drops early instructionsLong promptPut constraints at top and bottom

Tokens cost money in API products and often in IDE plans. See Cost, Latency, and Tokens.

For shipped features:

  • Cache stable system prompts
  • Summarize long histories server-side
  • Use smaller models for classification; larger for generation

For Teams

PolicyRecommendation
Rules file sizeKeep AGENTS.md under ~500 lines; split by package in monorepos
.cursorignoreExclude node_modules, build output, secrets, large fixtures
Contractor accessNarrow repo scope — less context leakage risk
LoggingDo not log full prompts with PII in production apps

Document exclusions in Team AI Policy.