Skip to main content

Mastering Token Optimization for AI Coding Agents

The definitive, practical guide to controlling token consumption and optimizing cost, speed, and quality across GitHub Copilot, Claude Code, and OpenAI Codex. Every technique here is drawn from how these agents actually build context, so you can spend fewer tokens and get better answers.

Why this matters

Tokens are the currency of AI. Every file you attach, every reply the model writes, and every step an agent takes costs tokens — which cost money, latency, and, most importantly, quality. A bloated context isn't just expensive; it makes the model dumber by burying the signal in noise.


1. First Principles — What You're Actually Paying For

🧒 What Is a Token? (Explained Like You're a Kid)

Imagine you have a big bag of LEGO bricks. To build a sentence, the AI snaps together little bricks — and each brick is a token.

Here's the fun part: a brick isn't always a whole word.

  • Easy, common words are one brick: cat, dog, the, run.
  • Big or unusual words get split into a few bricks: unbelievable might become un + believ + able (3 bricks).
  • Even spaces and punctuation can be their own tiny bricks: . , ?

So when the AI reads or writes, it's really just counting bricks. The more bricks you hand it, the more it has to carry — and carrying bricks costs money, time, and brain-space.

The ice-cream analogy

Think of tokens like scoops of ice cream you pay for. One scoop = one token. A short question is a small cone (cheap). Pasting your whole project is a giant sundae with 50 scoops (expensive!). You want just enough scoops to be happy — not a melting mountain you can't finish. 🍦

🔤 The Real Deal — How Tokens Actually Work

Now the grown-up version. Models don't read letters or whole words — they read subword chunks produced by a process called tokenization (commonly Byte-Pair Encoding, BPE). The tokenizer learns the most frequent character sequences and turns text into a list of numeric IDs the model understands.

That's why "one word = one token" is wrong:

  • Frequent words are a single token; rare words fragment into several.
  • Capitalization, a leading space, and punctuation can each change the split ( cat, cat, Cat, cat. may tokenize differently).
  • Numbers and code symbols often split in surprising ways.

📏 How to Measure Tokens

A reliable rule of thumb for English:

~4 characters ≈ 1 token, or roughly ¾ of a word (so ~100 words ≈ 130 tokens)

Quick real-world counts:

TextApprox. tokens
"Hello"1
"Hello, how are you?"~5
"I am a software engineer"~6
"Refactor this function to use async/await"~9
One page of prose (~500 words)~650–750
A 200-line source file~2,000–3,500

To measure exactly, use a real tokenizer:

  • OpenAI / Codex → the tiktoken library (or OpenAI's online tokenizer).
  • Claude → Anthropic's token-counting endpoint / SDK.
  • In-agent/status (Codex) and usage panels show live token counts for your session.
Code tokenizes heavier than prose

Source code is denser than English. Indentation, braces, punctuation, and long identifiers like getUserAuthenticationToken each cost bricks. A file that looks "short" can tokenize far heavier than the same character count of plain text — so never eyeball code by line count alone.

The Four Token Sinks

Every AI coding interaction spends tokens in four places:

SinkWhat It IsWho Controls It
Input / contextYour prompt + attached files + history + system promptYou (mostly)
OutputThe model's reply (code, explanations)You (via instructions)
Reasoning"Thinking" tokens on reasoning modelsYou (via effort settings)
Agent loopsRepeated tool calls, file reads, retriesYou (via task scoping)

The single biggest lever is input context. Agents re-send context on every turn, so a 40K-token context in a 20-turn session can cost you ~800K input tokens — even if you never add another word.

The Hidden Multiplier: Context Is Re-Sent Every Turn

Turn 1:  [system + instructions + 30K context] + your msg  → reply
Turn 2: [system + instructions + 30K context + turn 1] + your msg → reply
Turn 3: [system + instructions + 30K context + turns 1-2] + your msg → reply
...
The compounding trap

A long conversation with a large context doesn't cost tokens once — it costs them cumulatively, on every single turn. This is why "start a fresh chat" is the most underrated cost-saving move.


2. The Golden Rules (Read This If Nothing Else)

  1. Scope tightly. Give the agent the smallest context that makes the task solvable.
  2. Start fresh often. New task → new conversation. Don't drag stale history around.
  3. Point, don't dump. Reference specific files/symbols instead of pasting whole folders.
  4. Compact long sessions. Summarize and reset before the window fills.
  5. Constrain the output. Ask for a diff or the changed function, not the whole file.
  6. Right-size the model. Cheap/fast model for simple work; reasoning model only when needed.
  7. Invest in instructions files. Persistent guidance beats repeating yourself every prompt.
  8. Review before you iterate. A precise correction costs less than three vague retries.

3. Prompting for Token Efficiency

How you phrase a request changes both what you spend and what you get back.

✅ Do: Be Specific and Outcome-Focused

A precise prompt reduces exploration loops (input) and rambling replies (output).

❌ Vague — triggers exploration + a long, hedged answer
"Can you look at my auth and see if anything's wrong?"

✅ Precise — bounded context, bounded output
"In #file:auth.service.ts, the refreshToken() method doesn't handle a
401 from the refresh endpoint. Add that case and return a typed error.
Show only the changed method."

✅ Do: Constrain the Output Explicitly

Output tokens are generated one at a time and are often the slowest part. Tell the model to be economical:

  • "Show only the diff."
  • "Return just the changed function, no explanation."
  • "Answer in one paragraph."
  • "No preamble; give me the code."

One well-scoped prompt covering three related changes is cheaper than three separate prompts that each re-send context.

❌ Avoid: "Explain everything" Prompts

"Explain this whole codebase" forces a massive read and a massive write. Ask about one module, one flow, or one file at a time.

❌ Avoid: Pasting Large Blobs Into Chat

Pasting a 2,000-line file into the message body is pure input cost — and it stays in history for every following turn. Attach by reference instead (see §5) so the tool can read only what it needs.

❌ Avoid: Vague Follow-ups That Force Re-Reading

"Now do the same for the others" makes the agent re-derive what "the same" and "the others" mean. Name them.

The prompt shape that saves tokens

Context reference + specific target + desired action + output constraint.

"In #file:cart.ts, make applyDiscount() handle stacked coupons. Show only the updated method."


4. Managing the Conversation Window

The conversation itself is the most common source of silent token bloat.

Start New Conversations Aggressively

When you switch tasks, the old history is dead weight that gets re-sent every turn.

ToolReset Command
Claude Code/clear (wipe context) · /compact (summarize + shrink)
Codex/new or /clear · /compact to summarize
CopilotStart a new chat / /clear in chat; new agent session

Compact Instead of Carrying Everything

/compact (Claude Code, Codex) replaces a long transcript with a concise summary — preserving decisions and state while discarding the verbose middle.

# Long debugging session getting expensive?
/compact
# The model summarizes what matters and frees the window.

Watch Your Usage Live

You can't optimize what you can't see:

  • Codex: /status shows the active model, token usage, and remaining context. /statusline keeps counters visible.
  • Claude Code: the CLI surfaces context usage; /compact when it climbs.
  • Copilot: check Settings → Copilot on github.com for request/usage; the editor shows premium-request consumption.
Auto-compaction is a safety net, not a strategy

Agents may auto-summarize when the window is nearly full — but by then you've already paid for the bloat on every prior turn. Compact proactively.


5. Context Curation — The Highest-Leverage Skill

Giving the agent the right context (and nothing more) is where the biggest savings live.

Reference Files; Don't Paste Them

Every agent has a way to attach files by reference so the tool reads them on demand:

ToolHow to Reference
Copilot#file, #selection, #codebase, @workspace
Claude Code@path/to/file.ts mentions
Codex/mention path/to/file (and file mentions)

Prefer a Selection Over a Whole File

If the change is in one function, select that function and use #selection (Copilot) or point at the symbol. Sending 40 lines beats sending 1,400.

Be Careful with "Whole-Codebase" Modes

@workspace / #codebase (Copilot) and repo-wide search are powerful but can pull in a lot. Use them to locate things, then narrow to specific files for the actual work.

Step 1 (locate, broad):  @workspace where is rate limiting implemented?
Step 2 (work, narrow): In #file:rate-limiter.ts, raise the burst limit to 50 and add a test.

Exclude Noise

  • Keep generated files, node_modules, build output, lockfiles, and vendored code out of context.
  • Use content exclusions (Copilot Business/Enterprise) and .gitignore-aware tooling.
  • Add an ignore file for the agent where supported (e.g. Codex/Claude respect ignore patterns and sandbox roots).

Close Irrelevant Editor Tabs

Some editor integrations use open tabs as ambient context signals. A dozen unrelated open files can quietly inflate what gets considered.


6. Instruction & Memory Files — Pay Once, Save Forever

Repeating "we use TypeScript strict mode, Vitest, and signals" in every prompt is a token tax. Say it once in a persistent file.

ToolFile(s)
Copilot.github/copilot-instructions.md, *.instructions.md (scoped), AGENTS.md
Claude CodeCLAUDE.md (+ AGENTS.md), memory files
CodexAGENTS.md (+ fallback filenames)

Keep Instruction Files Lean

Here's the paradox: instruction files are also context and get loaded into the window. A 2,000-line CLAUDE.md is expensive on every turn.

Write instructions like a tight cheat-sheet, not an encyclopedia.
# Good: concise, high-signal
- TypeScript strict; no `any`.
- Tests: Vitest. Run `npm test` before proposing changes.
- State: signals only — do not add NgRx.
- Don't touch `/legacy/**`.
  • ✅ Short bullet facts, commands, and hard constraints.
  • ❌ Long prose, tutorials, or duplicated docs the model already knows.

7. Right-Sizing the Model & Reasoning Effort

The model you pick is a direct multiplier on cost and speed.

Match Model to Task

Task typeRecommended tier
Autocomplete, tiny edits, renamesFast / cheap model
Everyday coding, tests, refactorsBalanced mid-tier (e.g. Sonnet-class)
Architecture, deep debugging, tricky algorithmsReasoning / top tier (e.g. Opus-class) — sparingly
  • Copilot: use the model picker; premium models may consume premium requests.
  • Claude Code: /model to switch Haiku ↔ Sonnet ↔ Opus by task.
  • Codex: /model, profiles, or -c model=...; tune model_reasoning_effort.

Tame Reasoning Tokens

Reasoning models spend hidden "thinking" tokens. That depth is worth it for hard problems and wasteful for trivial ones.

# Codex: named modes so you don't overpay for easy work
[profiles.quick-fix]
model_reasoning_effort = "low"

[profiles.deep-work]
model_reasoning_effort = "xhigh"
Don't bring an Opus to a typo fight

Downshifting to a smaller model for routine edits is often a 5–10× cost reduction with zero quality loss on easy tasks.


8. Agentic Workflows — Where Tokens Multiply Fast

Agents are the most powerful and most token-hungry mode, because they loop: read → act → read output → correct → repeat. Each loop re-sends context.

Plan Before You Let It Loose

A quick planning pass prevents expensive wrong turns.

  • Codex: /plan to get a plan before edits.
  • Claude Code: plan mode / ask for a plan first.
  • Copilot: ask agent mode to outline the approach before executing.

Approving a good plan costs a few hundred tokens; recovering from a 15-step wrong direction costs tens of thousands.

Scope the Task Narrowly

"Refactor the entire service layer" is an open-ended token furnace. "Extract the retry logic in http.ts into a withRetry() helper and update its two callers" is bounded and cheap.

Cap Autonomy Where It Helps

More auto-approval means more unsupervised loops. For unfamiliar or risky work, keep approvals on so you can stop a runaway sequence early — a token and safety win.

Use Async/Cloud Agents for Big, Well-Defined Jobs

The Copilot coding agent (issue → PR) and Codex Cloud offload long runs to a remote environment. You spend your local context budget reviewing a diff instead of babysitting every loop.

Isolate Subtasks

Subagents / forked sessions (Codex /fork, Claude subagents) keep a heavy subtask's context out of your main thread, so exploration doesn't permanently inflate the primary conversation.


9. Structural Habits That Compound

Beyond any single session, how you keep your repo shapes long-term token efficiency.

  • Modular code — small, well-named files let agents load just the relevant piece.
  • Clear names & types — reduce the context an agent needs to infer intent.
  • Good docs at the edges — a short module README beats the agent reading every file to understand a subsystem.
  • Keep the tree clean — commit or stash unrelated changes so diffs stay small and reviews stay cheap.
  • Curate ignore files — the fewer irrelevant files the agent can see, the less it will pull in.

10. Anti-Patterns — What to Avoid

Anti-patternWhy it burns tokensDo instead
One endless mega-chatFull history re-sent every turn/clear or /compact between tasks
Pasting whole files into chatRaw input cost, persists in historyReference with #file / @file / /mention
"Explain the whole repo"Huge read + huge writeAsk per-module, per-flow
Always using the top modelOverpays on easy tasksRight-size via model picker / /model
Giant instruction filesLoaded every turnKeep them lean and factual
Vague retries ("try again")Re-explores from scratchGive a precise, specific correction
Whole-codebase mode for everythingPulls excess contextLocate broad, then narrow to files
Ignoring usage indicatorsBloat goes unnoticedWatch /status, usage panels
Letting agents loop unsupervisedRepeated tool/read cyclesPlan first, keep approvals on

11. Tool-Specific Cheat Sheets

🐙 GitHub Copilot

  • Context: #file, #selection for precision; @workspace / #codebase to locate, then narrow.
  • Reset: new chat / /clear; fresh agent session per task.
  • Model: use the picker; watch premium requests in Settings → Copilot.
  • Persist: .github/copilot-instructions.md (lean) + AGENTS.md.
  • Offload: assign well-scoped issues to the coding agent → review the PR diff.
  • Exclude: content exclusions (Business/Enterprise) to keep secrets/noise out.

🟣 Claude Code

  • Reset: /clear between tasks; /compact for long sessions.
  • Context: @file mentions; avoid pasting; curate CLAUDE.md to stay small.
  • Model: /model — Haiku for speed, Sonnet for daily, Opus for hard problems.
  • Isolate: use subagents so heavy subtasks don't inflate the main thread.
  • Memory: keep memory/CLAUDE.md high-signal; prune stale notes.

🟢 OpenAI Codex

  • Reset: /new / /clear; /compact to summarize.
  • Monitor: /status and /statusline for live token usage.
  • Context: /mention specific files; keep AGENTS.md tight.
  • Model: /model, profiles, or -c model=...; tune model_reasoning_effort.
  • Plan/branch: /plan before edits; /fork to explore without polluting context.
  • Offload: Codex Cloud for long tasks; apply the diff locally.

12. A Worked Example — Same Task, 3× Cheaper

Task: Fix a bug where the cart total ignores discounts.

❌ The Expensive Way

[Same chat that's been open for 2 hours across 5 unrelated tasks]
"Hey the cart is wrong somewhere, can you look through everything
and figure out what's going on with the totals?"
  • Drags 90 minutes of stale history (re-sent every turn).
  • Triggers a whole-codebase read.
  • Produces a long, hedged explanation before any fix.

✅ The Optimized Way

/clear
# Fresh context. Locate first, narrowly.
@workspace where is the cart total calculated?

# → points to cart.service.ts:calculateTotal()
In #file:cart.service.ts, calculateTotal() sums line items but never
applies activeDiscounts(). Apply them after subtotal, before tax.
Show only the changed method.
  • Fresh window = minimal input.
  • Broad locate, then narrow to one file/method.
  • Output constrained to the changed method.
Same fix. A fraction of the tokens. A faster, sharper answer.

13. TL;DR — The One-Screen Summary

  • Context is king (and the biggest cost). Give the least context that solves the task.
  • It re-sends every turn. So /clear and /compact are your best friends.
  • Point, don't paste. Reference files (#file, @file, /mention) instead of dumping them.
  • Constrain output. Ask for diffs and changed functions, not whole files with essays.
  • Right-size the model. Cheap for easy, reasoning-tier only for hard.
  • Persist conventions in lean instruction files (copilot-instructions.md, CLAUDE.md, AGENTS.md).
  • Plan before agent loops; keep approvals on for risky work.
  • Watch usage (/status, usage panels) and compact proactively.
  • Great prompts are specific: context reference + target + action + output limit.

Optimizing tokens isn't about being cheap — it's about feeding the model signal instead of noise. Do that, and you get faster, sharper, cheaper results all at once. ⚡