AI coding cost is driven less by the number of questions than by how much repository context is resent, how much output and reasoning are generated, and how often tools and retries loop. Comparing model list prices alone misses the execution design that creates most waste.
This review uses official OpenAI, Anthropic, Google, and GitHub documentation checked on July 29, 2026. Because prices change, it emphasizes a repeatable measurement method rather than treating a model's current dollar rate as a permanent verdict.
The first three moves
- Record fresh input, cache writes/reads, output, tools, retries, and success per request.
- Send relevant files, symbols, tests, and the current diff—not the whole repository.
- Default to a smaller model and escalate only on defined complexity or failure conditions.
Then cache stable repeated context and move non-interactive classification, documentation, and evaluation work to Batch APIs.
Cost is the sum of five components
Estimate each request by multiplying current provider rates by:
fresh input + cache writes + cache reads + output/reasoning + server tools and retries

| Item | Why track it | Hidden waste |
|---|---|---|
| Fresh input | newly processed context | resending the repository |
| Cache write | storing a common prefix | caching volatile content |
| Cache read | reused input | prefixes that never hit |
| Output/reasoning | generated and reasoning usage | verbose explanations |
| Tools/retries | search, shell, test loops | duplicated failed calls |
OpenAI, Anthropic, and Gemini expose different usage fields. Preserve the raw provider response and normalize it into an internal cost schema instead of forcing every service into one guessed token counter.
1. Measure before shortening prompts — 5/5
Log task type, model, input, cache writes and reads, output, tool calls, retries, latency, and outcome. The metric that matters is cost per successful task. A cheaper run that lowers test pass rate can cost more after human repair and another request.
2. Send the minimum sufficient code context — 5/5
The fastest coding saving comes from reducing code scope, not removing adjectives from a prompt. Prioritize the current diff, referenced symbols, the error stack, and relevant tests. Exclude build artifacts, binaries, entire lockfiles, vendor duplicates, stale logs, generated reports, unrelated conversation history, and duplicated source documents.

| Context method | Strength | Risk | Best fit |
|---|---|---|---|
| Whole repository | seems complete | cost and noise | one-time map |
| Top search results | fast and cheap | retrieval miss | focused bug |
| Symbol/call graph | structural relevance | indexing work | large codebase |
| Diff + tests | change-focused | missing background | review iteration |
| Hierarchical summary | saves long context | stale summary | repeated sessions |
Track missing-file rate and test outcomes after pruning. The goal is the smallest sufficient set, not the smallest possible prompt.
3. Route tasks instead of using one model — 5/5
Formatting and file classification rarely need the same reasoning budget as architecture or security review.
| Task | Default route | Escalate when |
|---|---|---|
| classify, summarize, format | small low-cost model | schema validation fails |
| tests and simple patches | balanced coding model | repeated test failure |
| architecture and hard bugs | high-capability reasoning | complex from the start |
| security and migration | strong model + human | always approval-gated |
| bulk docs and evaluation | low-cost model + Batch | urgent only |

Escalation should follow test failures, cross-module scope, security sensitivity, and rollback cost. Do not repeat the same failing prompt indefinitely on a small model.
4. Cache stable repeated prefixes — 4/5
Caching works for long stable repository rules, system instructions, and tool schemas. Put volatile dates, user questions, and diffs after the shared prefix.
- OpenAI supports shared and explicit prefix caching; inspect model-specific write/read pricing and usage.
- Anthropic bills cache writes and hits separately; its official pricing makes cache reads substantially cheaper than base input.
- Gemini offers implicit caching on supported models and explicit caches for large repeated context, with TTL and storage cost considerations.
Cache writes are not free. Measure hit tokens, reuse count, and TTL; a context used only once or twice may not break even.
5. Move non-urgent workloads to Batch — 5/5
Official OpenAI, Anthropic, and Gemini documentation describes asynchronous Batch pricing at 50% below the comparable interactive rate. It is well suited to large test generation, classification, documentation, static-analysis explanations, and evaluation suites.
Batch requires job state, failed-item retry, and turnaround handling. It is not a fit for IDE completion or an interactive debugging conversation.
6. Budget output, reasoning, tools, and retries — 4/5
Define an output contract: changed files, patch, test results, and remaining risk. Match output caps and reasoning level to difficulty. Limit search, shell, and test calls; stop after repeated identical failures and request missing information.
Tool descriptions and schemas are input tokens. Expose only relevant tools, and separately count provider-hosted search or execution charges that may sit outside normal token usage.
7. Regression-test cost and quality together — 5/5
Run the same representative set before and after optimization: bug fixes, test generation, refactoring, review, and documentation. A benchmark containing only syntax tasks unfairly favors the smallest model.

| Metric | Calculation | Guardrail |
|---|---|---|
| Task success | completed and tests pass ÷ total | no drop vs baseline |
| Cost per success | total cost ÷ successful tasks | primary target |
| Retry rate | additional requests ÷ total | investigate increases |
| Cache hit rate | cache reads ÷ eligible input | validates prefix design |
| Human repair | final edit minutes | hidden cost |
| Latency | median and tail | separate interactive/batch |
Provider feature review
| Feature | OpenAI | Anthropic | Gemini | Verdict |
|---|---|---|---|---|
| Preflight counting | tokenizer and usage | token counting endpoint | countTokens API | use before large calls |
| Prompt caching | shared/explicit prefixes | write/hit and TTL pricing | implicit + explicit | strong for repetition |
| Batch discount | 24-hour window, 50% | 50% input/output | 50% of standard | top non-urgent lever |
| Tool charges | model/tool dependent | server tools may add cost | grounding may add cost | track separately |
| Long context | model tier varies | premium threshold exists | model bands vary | enforce context budget |
The table reviews mechanisms, not permanent prices. Recheck official pricing pages before production changes.
Recommended rollout
- Capture a week of per-request usage and success.
- Remove unnecessary repository, log, and conversation context from the ten costliest task types.
- Define a default model and escalation rule per task class.
- Stabilize repeated prefixes and measure cache hits.
- Move non-urgent bulk jobs to Batch.
- Add output, tool-call, and retry budgets.
- Review cost per success and human repair time weekly.
Conclusion
The best saving is an observable execution policy, not a magic prompt. Send only relevant code, route by task difficulty, cache repeated context, batch non-urgent work, and cap loops. Quality and repair time must remain in the scorecard so that “cheap but repeated twice” never looks like optimization.
Official sources
- OpenAI API pricing
- OpenAI — Model guidance and prompt caching
- OpenAI — Batch API
- Anthropic — Pricing
- Gemini API pricing
- Gemini — Context caching
- Gemini — Batch API
- Gemini — Counting tokens
- GitHub Copilot — Models and pricing
Prices, models, discounts, token accounting, and terms change. Recheck official pricing and actual API usage before deployment.