Codecademy Logo

Token Usage Optimization

Related learning

  • Learn how AI tokens work, where costs add up, and how to reduce usage with smarter prompts, token awareness, and practical tracking.
    • With Certificate
    • Beginner Friendly.
      1 hour

Tokens

A token is the smallest unit of text a language model reads and processes, not quite a word and not quite a character, but something in between.

Before a model does anything with text, it breaks it into these units: a common word often maps to a single token, while longer or rarer words split into several pieces, and even spaces and punctuation count.

In English, a token averages about three to four characters, or roughly three-quarters of a word, so 1,000 tokens come out to around 750 words (this varies by model and language). Tokens matter because they are the unit everything is measured in: cost, context limits, and usage are all counted in tokens.

[Token] [ization] [ isn] ['t] [ free] [.]
1 2 3 4 5 6 → 6 tokens

Token Usage

Token usage falls into two categories.

Input tokens are everything sent to the model: the prompt typed, attached files, etc.

Output tokens are the words the model generates in response.

Much of the input is invisible, since system instructions and earlier turns ride along behind every new message even when they aren’t retyped. The distinction between the two is the foundation for everything else, because input and output are counted, and often priced, separately.

INPUT (everything sent to the model)
├─ the prompt you type
├─ system instructions / persona
├─ attached files & documents
└─ full conversation history
OUTPUT (what the model generates back)
└─ the response

Token Management

In a multi-turn conversation, every new message includes the full conversation history as context. Each earlier turn, both the questions asked and the responses given, is resent in full on top of whatever is newly typed, since the model processes the whole thread fresh on every request.

Token consumption therefore accumulates with each exchange: even a short follow-up drags the entire prior thread along, so the cost of a single turn keeps climbing as the session grows.

Turn 1: prompt → 1,200 tokens sent
Turn 2: [turn 1] + prompt → 2,000 tokens sent
Turn 3: [turn 1 + turn 2] + prompt → 2,900 tokens sent

Effort Settings

On reasoning models, effort settings control how many tokens a model spends on internal reasoning before it produces a response.

This reasoning is what the model does privately to plan or think through a problem, separate from the visible answer that follows, so a higher effort setting means more hidden reasoning tokens, not a longer reply.

Those reasoning tokens are billed like output (on the expensive side), so an uncapped thinking pass can quietly inflate the cost of a single answer. The practical move is to match effort to the task: low for simple lookups and formatting, higher only for genuinely hard problems.

Low effort → prompt → [reason] → ████ response
High effort → prompt → [reason · · · · · · · · ·] → ████ response

Context Compression

Context compression is the mechanism by which a platform summarizes or discards older conversation history as a session nears its context limit. Generating that summary costs tokens up front, but it replaces a long history with a compact stand-in, so every later message resends far fewer tokens and the session grows cheaper from there.

The trade-off is fidelity: afterward, earlier messages survive only as an approximation, no longer verbatim, so a precise instruction or constraint set early can quietly degrade.

Before: [Turn 1] [Turn 2] [Turn 3] [Turn 4] ← context nearly full
After: [summary of turns 1–4] [Turn 5] ← room freed

Planning

There are two habits that can cut down on follow-up corrections:

  1. Asking a model to outline its approach before executing surfaces the plan in cheap discussion, where a wrong direction can be fixed before it becomes a whole wrong answer.
  2. Stating an explicit success condition up front (cite a source for each claim, stop when the tests pass) gives the model a concrete target to hit and check itself against.

Both work because a vague prompt invites a vague answer and another round to fix it, and every correction is wasted output on the expensive side, resent with the full thread.

Multimodal Input and Output

Images and PDFs are the heaviest inputs a model can take. Each one is converted into tokens before its content can be read, a step plain text skips entirely.

The magnitudes are large: one image can run a few hundred to a couple thousand tokens, and a multi-page PDF (each page parsed as an image) can reach ten thousand or more.

So when the content is really just text, pasting it costs a fraction of uploading the file. Reserve images and PDFs for when the visual itself is the point—a chart or a UI bug—not a default container for words.

Same paragraph:
pasted as text → ~120 tokens
same as an image → ~1,200 tokens
a page of a PDF → ~1,500+ tokens (parsed as an image)

Output Token Cost

Across providers, output tokens cost meaningfully more than input, commonly three to five times more per token and sometimes higher. That makes the length of the response, not the prompt, the bigger driver of most bills. The practical consequence is where to spend effort: trimming a verbose prompt saves on the cheap side, while shaping the output (a shorter reply, no padding, only what’s actually needed) saves on the expensive one.

Same 1,000 tokens total (rates ~ $3 in / $15 out per 1M):
900 in + 100 out → cheaper (most tokens on the cheap side)
100 in + 900 out → ~3x more (most tokens on the pricey side)

Caching

Caching is the mechanism by which a platform stores a snapshot of content it has already processed (a long system prompt, an uploaded document, the earlier turns) and reuses it on the next request instead of re-reading from scratch.

When supported, reuse is often automatic and billed at a steep discount, often a tenth of the normal input rate. But the snapshot only lasts a short, provider-specific window: miss it, and the full context is reprocessed from scratch at standard rates.

Request 1: process prefix (system + earlier turns) → full price
Request 2: reuse cached prefix (within window) → ~1/10 price ✓ warm
Request 3: prefix expired → reprocess from scratch → full price ✗ cold

Token Optimization In Sessions

Token optimization is itself an effort, so it pays to focus on the interactions where the return is highest rather than optimizing every exchange. In general, the biggest opportunities are long-running sessions with large contexts, heavily multimodal interactions, repetitive workflows with a stable structure, and scenarios that rely on expensive frontier models.

For a quick lookup or a one-off question, the savings are minimal, and the effort spent optimizing often costs more attention than it returns.

Token-Conscious Prompts

Verbose, filler-heavy prompts spend tokens without improving the result. Politeness padding, repetition, and overexplained context increase token counts without adding signal, so the same request stated plainly often costs far less while producing the same output.

Structure also enables useful trimming. Instead of narrated transitions, we can use an explicit label (a ## Context header or <context> tags in Claude) to communicate the same information in fewer tokens while removing ambiguity about where each section begins and ends. The gains from structure are usually smaller than the gains from cutting filler, but both help move the prompt toward a higher signal-to-token ratio.

Verbose: "I'd really appreciate it if you could please carefully write some unit tests for the function below, thanks!" → ~30 tokens
Concise: "Write unit tests for the function below." → ~9 tokens

Learn more on Codecademy

  • Learn how AI tokens work, where costs add up, and how to reduce usage with smarter prompts, token awareness, and practical tracking.
    • With Certificate
    • Beginner Friendly.
      1 hour