GenAIHub
Back to Technical
Cost Management

Token Cost Optimization

Master the economics of LLM applications. Understand pricing models, calculate costs accurately, and implement strategies to reduce spend by 50-80% without sacrificing quality.

๐Ÿ’ฐ Why Token Costs Matter

$10K+
Monthly cost at 1M requests
using GPT-4o naively
50-80%
Savings possible
with proper optimization
4-5x
Output vs input cost
output tokens more expensive

๐Ÿ“ฅ Input Tokens

System prompt + context + user message. Cheaper but adds up with long contexts.

~4 chars = 1 token (English) | ~2 chars = 1 token (code)

๐Ÿ“ค Output Tokens

Generated response. 3-5x more expensive and harder to predict accurately.

Control via max_tokens and prompt instructions

๐Ÿ“Š Model Pricing Comparison (2024)

Model Provider Input $/M Output $/M Context Best For
GPT-4o OpenAI $2.50 $10.00 128K Multimodal, vision
Claude 3.5 Sonnet Anthropic $3.00 $15.00 200K Coding, analysis
Gemini 1.5 Pro Google $1.25 $5.00 2M Long context
GPT-4o-mini OpenAI $0.15 $0.60 128K Cost-effective general
Claude 3.5 Haiku Anthropic $0.80 $4.00 200K Fast responses
Gemini 2.0 Flash Google $0.075 $0.30 1M Best value
Llama 3.1 70B Self-host ~$0.20* ~$0.20* 128K Open, customizable
Mixtral 8x22B Self-host ~$0.15* ~$0.15* 64K MoE efficiency

* Self-hosted costs vary based on GPU and utilization. Estimates based on cloud GPU pricing.

๐Ÿงฎ Quick Cost Estimation

Formula

cost = (input_tokens ร— input_price) + (output_tokens ร— output_price)
1K tokens โ‰ˆ 750 words (English)
1 page of text โ‰ˆ ~500 tokens
1 API call average โ‰ˆ 1-3K tokens

Example Calculation

Scenario: 1M requests/month, GPT-4o-mini
โ€ข Avg input: 1,500 tokens โ†’ 1.5B total
โ€ข Avg output: 500 tokens โ†’ 500M total

Input: 1,500 ร— $0.15/M = $225
Output: 500 ร— $0.60/M = $300
Total: $525/month

๐ŸŽฏ Cost Optimization Strategies

Model Routing

Route requests to the cheapest capable model:

  • โ€ข Simple queries: GPT-4o-mini, Gemini Flash
  • โ€ข Complex reasoning: GPT-4o, Claude Sonnet
  • โ€ข Classification: Fine-tuned small model
๐Ÿ’ก Savings: 40-60%

Prompt Optimization

Reduce tokens without losing quality:

  • โ€ข Remove redundant instructions
  • โ€ข Use shorter variable names in few-shot
  • โ€ข Compress context with summaries
๐Ÿ’ก Savings: 20-40%

Caching

Reuse previous responses:

  • โ€ข Exact match: Cache identical queries
  • โ€ข Semantic: Embeddings similarity (>0.95)
  • โ€ข Prompt caching: Anthropic/OpenAI native
๐Ÿ’ก Savings: 30-70% (varies)

Output Control

Limit expensive output tokens:

  • โ€ข Set appropriate max_tokens
  • โ€ข Request concise responses in prompt
  • โ€ข Use structured output (JSON)
๐Ÿ’ก Savings: 20-50%

โšก Prompt Caching (Native)

Both Anthropic and OpenAI offer native prompt caching for repeated system promptsโ€”up to 90% cheaper!

Anthropic Prompt Caching

  • โ€ข Mark cacheable content with cache_control
  • โ€ข 90% discount on cached input tokens
  • โ€ข 5-minute TTL (refreshes on use)
  • โ€ข Min 1024 tokens to cache

OpenAI Prompt Caching

  • โ€ข Automatic for repeated prompts
  • โ€ข 50% discount on cached tokens
  • โ€ข Works on GPT-4o and mini
  • โ€ข Min 1024 tokens prefix match

๐Ÿ“ˆ Cost Monitoring Best Practices

Cost per Request

Track by endpoint/feature

Daily/Monthly Spend

Set budget thresholds

Cost per User

Identify heavy users

Anomaly Alerts

Spike detection

๐Ÿ› ๏ธ Recommended Tools

Langfuse LangSmith OpenLLMetry Helicone Datadog LLM

Related Topics