LLM Cost Optimization Cheat Sheet

Optimize LLM costs — token counting and pricing, prompt caching strategies, prompt compression, model selection by cost/task, and production cost reduction techni.

Last Updated: May 1, 2025

Token Counting & Pricing

ModelInput $/1M tokensOutput $/1M tokensContext Window
GPT-4o$2.50$10.00128K
GPT-4o-mini$0.15$0.60128K
GPT-4 Turbo$10.00$30.00128K
Claude 3 Opus$15.00$75.00200K
Claude 3 Sonnet$3.00$15.00200K
Claude 3 Haiku$0.25$1.25200K
Gemini 1.5 Pro$1.25–$5.00$5.00–$20.001M (tiered)
Llama 3 70B (Together)$0.90$0.908K
Mixtral 8x7B (Together)$0.60$0.6032K

Prompt Caching Strategies

Anthropic Prompt Caching
Cache repeated content across API calls — 90% cost reduction on cache hits
Cache Breakpoints
Mark cache boundaries in your prompt — system instructions, tool definitions, few-shot examples
Minimum Cache Size
Anthropic: 1024 tokens min for cached blocks (Sonnet/Haiku); 2048 for Opus
Cache TTL
Cached content lives for ~5 minutes of inactivity — keep requests flowing within the window
Google Context Caching
Gemini: cache system instructions + large docs — pay reduced rate for cached tokens
OpenAI Prompt Caching (beta)
Automatic caching — no API changes needed. 50% discount on cached prefixes
tiktoken for counting
Use tiktoken to count tokens before sending — predict costs before API call
Streaming + Cost
Streaming doesn't change token count but you only see output cost after completion

Prompt Compression

ItemDescription
LLMLinguaCompress prompts by 2-5x while preserving task performance — removes non-essential tokens
Selective ContextKeep last N turns + summarize earlier conversation — reduces context bloat in long chats
Few-Shot PruningRemove few-shot examples that are irrelevant to the current query — dynamic example selection
Tool Description TrimmingOnly include tools relevant to current task phase — don't send all tools every turn
LongContextFusionFuse multiple retrieved documents into one condensed context before sending
Extractive SummarizationSummarize retrieved chunks to 1-2 sentences each before passing to LLM
Token BudgetSet hard token budget per request: if context > budget, summarize oldest content first

Model Selection by Task

Task TypeRecommended ModelCost LevelWhy
Simple classificationGPT-4o-mini / Haiku$Fast, cheap, sufficient accuracy
Chat / customer supportGPT-4o / Claude Sonnet$$Balance of quality and cost
Complex reasoningClaude Opus / GPT-4o$$$Deep reasoning, code generation
SummarizationGPT-4o-mini / Haiku$Pattern matching, not deep reasoning
Code generationClaude Sonnet / GPT-4o$$Strong code capabilities
TranslationGPT-4o-mini / Haiku$Specialized models often overkill
RAG synthesisGPT-4o / Claude Sonnet$$Needs to synthesize multiple sources
Safety/filteringGPT-4o-mini / Haiku$Lightweight guard model before main call
Pro Tip: Prompt caching (Anthropic) and context caching (Google) can reduce costs by 90% for repeated system prompts. Design your application to reuse cached prefixes — it's the single biggest cost win.