Tokens

Tokens are the basic units that AI models use to process and generate text. A token roughly equals 4 characters or about 0.75 words in English. Understanding tokens helps you estimate costs, manage context windows, and craft efficient prompts for AI-assisted development.

Example

The sentence 'Write a React component' is about 6 tokens. A 500-line code file might be 2,000-3,000 tokens. Claude's 200K context window means it can process about 150,000 words at once.

Tokens are the currency of AI interactions. Everything you send to AI and everything it generates is measured in tokens, which affects both cost and capability.

Token Basics

  • ~4 characters per token in English
  • ~0.75 words per token on average
  • Code often tokenizes differently than prose
  • Common words = fewer tokens; rare words = more

Why Tokens Matter

Cost: Most AI APIs charge per token:

  • Input tokens (your prompt)
  • Output tokens (AI's response)
  • Cheaper models = less capable but more affordable

Context limits:

  • Each model has a token limit
  • Input + output must fit within the window
  • Larger context = more code AI can consider

Estimating Tokens

Quick mental math:

  • 1,000 words ≈ 1,300 tokens
  • 100 lines of code ≈ 500-1,000 tokens
  • A typical prompt ≈ 50-200 tokens
  • A detailed response ≈ 200-1,000 tokens

Optimizing Token Usage

For cost:

  • Be concise in prompts
  • Only include relevant context
  • Use cheaper models for simple tasks

For context:

  • Focus on relevant code
  • Summarize long discussions
  • Remove unnecessary comments

Token Limits by Model

ModelContext Window
GPT-4o128K tokens
Claude 3.5200K tokens
Gemini 1.51M+ tokens

These limits continue expanding, making larger codebases accessible to AI.