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.
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:
unbelievablemight becomeun+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.
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:
| Text | Approx. 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
tiktokenlibrary (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.
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:
| Sink | What It Is | Who Controls It |
|---|---|---|
| Input / context | Your prompt + attached files + history + system prompt | You (mostly) |
| Output | The model's reply (code, explanations) | You (via instructions) |
| Reasoning | "Thinking" tokens on reasoning models | You (via effort settings) |
| Agent loops | Repeated tool calls, file reads, retries | You (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
...
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)
- Scope tightly. Give the agent the smallest context that makes the task solvable.
- Start fresh often. New task → new conversation. Don't drag stale history around.
- Point, don't dump. Reference specific files/symbols instead of pasting whole folders.
- Compact long sessions. Summarize and reset before the window fills.
- Constrain the output. Ask for a diff or the changed function, not the whole file.
- Right-size the model. Cheap/fast model for simple work; reasoning model only when needed.
- Invest in instructions files. Persistent guidance beats repeating yourself every prompt.
- 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."
✅ Do: Batch Related Asks
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.
Context reference + specific target + desired action + output constraint.
"In
#file:cart.ts, makeapplyDiscount()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.
| Tool | Reset Command |
|---|---|
| Claude Code | /clear (wipe context) · /compact (summarize + shrink) |
| Codex | /new or /clear · /compact to summarize |
| Copilot | Start 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:
/statusshows the active model, token usage, and remaining context./statuslinekeeps counters visible. - Claude Code: the CLI surfaces context usage;
/compactwhen it climbs. - Copilot: check
Settings → Copiloton github.com for request/usage; the editor shows premium-request consumption.
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:
| Tool | How 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.
| Tool | File(s) |
|---|---|
| Copilot | .github/copilot-instructions.md, *.instructions.md (scoped), AGENTS.md |
| Claude Code | CLAUDE.md (+ AGENTS.md), memory files |
| Codex | AGENTS.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.
# 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 type | Recommended tier |
|---|---|
| Autocomplete, tiny edits, renames | Fast / cheap model |
| Everyday coding, tests, refactors | Balanced mid-tier (e.g. Sonnet-class) |
| Architecture, deep debugging, tricky algorithms | Reasoning / top tier (e.g. Opus-class) — sparingly |
- Copilot: use the model picker; premium models may consume premium requests.
- Claude Code:
/modelto switch Haiku ↔ Sonnet ↔ Opus by task. - Codex:
/model, profiles, or-c model=...; tunemodel_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"
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:
/planto 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-pattern | Why it burns tokens | Do instead |
|---|---|---|
| One endless mega-chat | Full history re-sent every turn | /clear or /compact between tasks |
| Pasting whole files into chat | Raw input cost, persists in history | Reference with #file / @file / /mention |
| "Explain the whole repo" | Huge read + huge write | Ask per-module, per-flow |
| Always using the top model | Overpays on easy tasks | Right-size via model picker / /model |
| Giant instruction files | Loaded every turn | Keep them lean and factual |
| Vague retries ("try again") | Re-explores from scratch | Give a precise, specific correction |
| Whole-codebase mode for everything | Pulls excess context | Locate broad, then narrow to files |
| Ignoring usage indicators | Bloat goes unnoticed | Watch /status, usage panels |
| Letting agents loop unsupervised | Repeated tool/read cycles | Plan first, keep approvals on |
11. Tool-Specific Cheat Sheets
🐙 GitHub Copilot
- Context:
#file,#selectionfor precision;@workspace/#codebaseto 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:
/clearbetween tasks;/compactfor long sessions. - Context:
@filementions; avoid pasting; curateCLAUDE.mdto 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.mdhigh-signal; prune stale notes.
🟢 OpenAI Codex
- Reset:
/new//clear;/compactto summarize. - Monitor:
/statusand/statuslinefor live token usage. - Context:
/mentionspecific files; keepAGENTS.mdtight. - Model:
/model, profiles, or-c model=...; tunemodel_reasoning_effort. - Plan/branch:
/planbefore edits;/forkto explore without polluting context. - Offload: Codex Cloud for long tasks; apply the diff locally.