Typorithm · easy

Tokens & Context Window

tokens-contextllmtokenscontext-windowcount
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Tokens & Context Window

0%

Context & Tokens

Models process tokens, not characters — about four characters per token. The context window caps input and output tokens together, so very long prompts cost more and can bury the details that matter. Count tokens before sending to stay within limits and budget.

Claude reads text as tokens, each roughly four characters. A short English word is often one token; code and non-English text tokenize into more. The model never sees raw characters — token counts are what drive cost and limits.

The context window is a hard ceiling on input plus output tokens combined. A prompt that nearly fills the window leaves little room for the response, and very long prompts can bury the key details among less relevant text.

Call client.messages.countTokens with the same model and messages you plan to send. It returns input_tokens so you can budget cost and confirm the request fits before you ever pay for the full call.

01

Tokens, not characters: ~4 characters per token.

02

The context window caps input + output together.

03

countTokens estimates a request before you send it.

// text is split into tokens (~4 chars each)
const count = await client.messages.countTokens({
  model: "claude-opus-4-8",
  messages,
});
// the context window caps input + output tokens

Read the implementation

Connect each part of the code to the concept before you type.

1. Tokens, not characters

Models read text as tokens — roughly four characters each — not as raw characters.

// text is split into tokens (~4 chars each)

2. Count before sending

countTokens measures a request's token usage before you send it, so you can budget and stay within limits.

const count = await client.messages.countTokens({

3. Model id

Token counts are model-specific, so countTokens takes the same model id you will run inference with.

  model: "claude-opus-4-8",

4. The messages

You count the exact messages you plan to send — the count reflects that real payload.

  messages,

5. Context window

The context window bounds input plus output together, so long prompts cost more and can crowd out key details.

// the context window caps input + output tokens

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Tokens & Context Window: Lesson & Practice · Typorithm | ApplyDjinn