AI API Cost Optimization for Developers
AI API Cost Optimization for Developers
AI API costs can spiral quickly. Understanding token pricing, caching strategies, and model selection helps you build AI features sustainably.
Understanding Token Pricing
LLM APIs charge per token (roughly 4 characters or 0.75 words). Most differentiate between input and output tokens.
2026 Approximate Pricing (per 1M tokens)
| Model | Input | Output |
|-------|-------|--------|
| GPT-4o | $2.50 | $10.00 |
| Claude Sonnet | $3.00 | $15.00 |
| Claude Haiku | $0.25 | $1.25 |
| Gemini Pro | $1.25 | $5.00 |
| GPT-4o mini | $0.15 | $0.60 |
Cost Reduction Strategies
1. Choose the Right Model
Not every task needs the most powerful model. Route by complexity:
2. Minimize Input Tokens
Trim context — don't send your entire codebase. Send only relevant files. Compress prompts — remove redundant instructions. "Respond in JSON" is shorter than "Please format your response as a JSON object with the following structure..." Use system prompts wisely — they're cached in some APIs (Claude's prompt caching), so put stable instructions there.3. Cache Responses
Cache identical or similar requests:
const cacheKey = crypto.createHash("md5")
.update(JSON.stringify({ model, messages }))
.digest("hex");
const cached = await redis.get(cacheKey);
if (cached) return JSON.parse(cached);
const response = await openai.chat.completions.create({ model, messages });
await redis.set(cacheKey, JSON.stringify(response), "EX", 3600);
4. Use Streaming
Streaming doesn't reduce cost, but it improves perceived performance. Users see the first token in ~200ms instead of waiting 5-10 seconds for the full response.
5. Set Max Tokens
Always set max_tokens to prevent runaway responses:
const response = await openai.chat.completions.create({
model: "gpt-4o",
messages,
max_tokens: 500, // Cap output length
});
6. Batch Processing
Use batch APIs for non-real-time workloads. OpenAI's batch API is 50% cheaper.
Monitoring Costs
Track token usage per request, user, and feature:
const usage = response.usage;
console.log({
inputTokens: usage.prompt_tokens,
outputTokens: usage.completion_tokens,
estimatedCost: (usage.prompt_tokens * 2.5 + usage.completion_tokens * 10) / 1_000_000,
});
Try It
Use our AI Cost Calculator to compare pricing across models and estimate costs for your expected volume.
Conclusion
AI costs are manageable with the right strategy: use cheaper models for simple tasks, cache responses, minimize context, and monitor usage. The difference between a naive and optimized implementation can be 10x.