HomeInsightsLLM Cost Optimisation
Generative AICost EngineeringJune 12, 202611 min read

LLM Cost Optimisation: How We Reduced GPT-4 Costs by 65%

Running GPT-4 at scale is expensive. A production AI platform processing 50,000 requests per day can easily rack up $40,000+ per month in API costs. Here's exactly how we cut that bill by 65% — without sacrificing output quality.

VE
Vikgol Engineering Team
AI Engineering & LLM Development · Vikgol
LLM Cost OptimisationHow Vikgol reduced GPT-4 API costs by 65% in productionBEFOREAFTERRaw GPT-4 API calls$28,000Redundant API calls$8,200Over-sized prompts$5,600Total: $41,800/month↓65%GPT-4o (routed)$9,800After Redis cache$3,200Compressed prompts$1,620Total: $14,620/month5 OPTIMISATION STRATEGIES USED1. Redis Caching2. Model Routing3. Prompt Compression4. Semantic Cache5. Batch Processing↓65%Total cost reduction$27KSaved per month40%Cache hit rate0%Quality degradationVikgolBelieve In Doers

When Genstori came to us, they were burning $41,800 per month on GPT-4 API calls. Their AI content platform was processing roughly 50,000 requests daily — and the costs were growing faster than their revenue.

Six weeks later, that bill was $14,620. Same platform. Same request volume. Same output quality. 65% lower cost.

Here's exactly what we did — every strategy, every decision, and the code that made it happen.

📌 Quick Answer: How to Reduce LLM Costs

The fastest wins come from: (1) Redis caching for repeated queries, (2) model routing — GPT-4o Mini for simple tasks, GPT-4o for complex ones, (3) prompt compression to cut token count, (4) semantic caching for similar queries, and (5) batch processing for non-real-time tasks. Combined, these strategies typically reduce LLM costs by 50-70%.

The Problem: Why LLM Costs Spiral

Before diving into solutions, it's worth understanding why LLM costs get out of control so quickly. Most teams build their first AI feature the obvious way: user sends query → call GPT-4 API → return response. It works. It's fast to build. And it's expensive.

The three biggest cost drivers we see in production AI systems:

  • Repeated queries — The same (or near-identical) questions asked thousands of times per day, each generating a fresh, expensive API call
  • Wrong model for the task — Using GPT-4o (expensive) for tasks that GPT-4o Mini (10× cheaper) handles equally well
  • Bloated prompts — System prompts and context windows that are 3× longer than they need to be
$41.8K
Monthly LLM cost before optimisation
$14.6K
Monthly LLM cost after optimisation
↓65%
Cost reduction with zero quality loss

Strategy 1: Redis Semantic Caching

This was our biggest single win — a 28% cost reduction on its own. The insight: a large percentage of LLM queries in any production system are either identical or semantically equivalent. "What's the refund policy?" and "How do I get a refund?" should return the same answer.

We implemented two layers of caching:

Layer 1 — Exact Match Cache (Redis)

For identical queries, we cache the response in Redis with a TTL based on content freshness requirements. Exact matches return instantly at zero LLM cost.

Python — Redis Exact Cache
import redis
import hashlib
import json

class LLMCache:
    def __init__(self):
        self.redis = redis.Redis(host='localhost', port=6379)
        self.ttl = 3600  # 1 hour default

    def get_cache_key(self, prompt: str, model: str) -> str:
        content = f"{model}:{prompt}"
        return hashlib.sha256(content.encode()).hexdigest()

    def get(self, prompt: str, model: str):
        key = self.get_cache_key(prompt, model)
        cached = self.redis.get(key)
        if cached:
            return json.loads(cached)
        return None

    def set(self, prompt: str, model: str, response: dict):
        key = self.get_cache_key(prompt, model)
        self.redis.setex(key, self.ttl, json.dumps(response))

Layer 2 — Semantic Cache (Embeddings)

For similar-but-not-identical queries, we use embedding similarity. If a new query is >92% similar to a cached query, we return the cached response. This alone eliminated 40% of our redundant API calls.

✅ Result

40% cache hit rate achieved. 40% of all API calls eliminated entirely. Monthly saving: ~$11,200. Implementation time: 3 days.

Strategy 2: Intelligent Model Routing

Not every query needs GPT-4o. Most don't. The problem is that teams default to the most powerful model because it's the safest choice — not the most cost-effective one.

We built a lightweight classification layer that routes queries to the right model:

Task TypeModel UsedCost per 1M tokens% of Queries
Simple Q&A, FAQ responsesGPT-4o Mini$0.1555%
Content generation, summariesGPT-4o Mini$0.1525%
Complex reasoning, analysisGPT-4o$2.5020%

Result: 80% of queries now run on GPT-4o Mini (17× cheaper than GPT-4o). The classifier itself costs almost nothing — it's a tiny fine-tuned model that runs locally.

Python — Model Router
def route_to_model(query: str, context_length: int) -> str:
    # Complex tasks → GPT-4o
    complex_indicators = [
        "analyze", "compare", "reasoning",
        "multi-step", "code", "explain why"
    ]

    # Long context → GPT-4o
    if context_length > 8000:
        return "gpt-4o"

    # Complex reasoning → GPT-4o
    if any(ind in query.lower() for ind in complex_indicators):
        return "gpt-4o"

    # Default: GPT-4o Mini (17x cheaper)
    return "gpt-4o-mini"

Strategy 3: Prompt Compression

This one surprised us. The system prompt for Genstori's AI was 2,847 tokens. After compression — removing redundancy, tightening instructions, removing examples that weren't needed — it was 890 tokens. 69% smaller.

Since you pay per token in both input AND output, every token in your system prompt is paid for on every single API call. At 50,000 daily requests, a 1,957-token reduction saved:

  • 1,957 tokens × 50,000 requests × 30 days = 2.9 billion tokens/month
  • At GPT-4o pricing: 2.9B tokens × $2.50/1M = $7,250/month saved
⚠️ Common Mistake

Don't compress prompts by removing important instructions — test quality at every step. We compressed in 5 iterations, evaluating output quality after each one. Stop when quality degrades. We found the sweet spot at 890 tokens with no quality impact.

Strategy 4: Response Streaming + Early Termination

For use cases where users don't need full responses, streaming with early termination reduces output tokens — and you only pay for tokens generated, not the full potential response.

For Genstori's content summarisation feature, users typically read the first 200 words before deciding to expand. We implemented streaming and let users stop generation early — reducing average output tokens by 35% on that feature alone.

Strategy 5: Batch Processing for Async Tasks

OpenAI's Batch API offers 50% cost reduction for non-real-time tasks. Any request that doesn't need an immediate response — background analysis, scheduled reports, bulk content generation — should use the Batch API.

For Genstori, 30% of their API calls were background tasks (content indexing, metadata generation, categorisation). Moving these to the Batch API halved their cost on those requests — without any user-facing impact.

Python — OpenAI Batch API
from openai import OpenAI
import json

client = OpenAI()

# Create batch file
batch_requests = [
    {
        "custom_id": f"task-{i}",
        "method": "POST",
        "url": "/v1/chat/completions",
        "body": {
            "model": "gpt-4o-mini",
            "messages": [{
                "role": "user",
                "content": task["prompt"]
            }]
        }
    }
    for i, task in enumerate(background_tasks)
]

# Submit batch — 50% cheaper, 24hr turnaround
batch = client.batches.create(
    input_file_id=upload_batch_file(batch_requests),
    endpoint="/v1/chat/completions",
    completion_window="24h"
)

Full Results — Before vs After

OptimisationMonthly SavingImplementation TimeComplexity
Redis Semantic Caching$11,2003 daysMedium
Model Routing$8,4004 daysMedium
Prompt Compression$7,2501 weekLow
Batch API (async tasks)$3,1002 daysLow
Response streaming optimisation$1,8501 dayLow
Total Saved$31,800/month~3 weeks
✅ Final Result

$41,800/month → $14,620/month. 65% reduction. Zero quality degradation. The 3-week engineering investment paid for itself in the first month — and continues to save $27,000+ every single month.

Frequently Asked Questions

How much can I realistically reduce LLM costs?
Most production AI systems can reduce LLM costs by 40-70% with the strategies above. The exact number depends on your query distribution — how many are repeated, how many are simple vs complex. Systems with high query repetition (like customer support AI) see the biggest gains from caching.
Does model routing reduce output quality?
Not if done correctly. GPT-4o Mini performs equivalently to GPT-4o on most tasks — simple Q&A, summarisation, basic content generation. The key is identifying which 20% of tasks genuinely need the more powerful model (complex reasoning, code generation, nuanced analysis) and routing only those.
What's the fastest LLM cost reduction I can implement today?
Prompt compression. Audit your system prompt right now — most teams have 2-3× more tokens than necessary. Remove redundancy, tighten instructions, cut examples that aren't critical. You can reduce prompt tokens by 40-60% in a day, with zero infrastructure changes.
Should I switch from GPT-4 to an open-source model to save costs?
Possibly — but it's not the first step. Optimise your existing setup first (caching, routing, compression). These are low-risk and fast to implement. Open-source models like Llama 3 require more infrastructure investment and quality tuning. Start with API optimisation, then evaluate self-hosting if costs are still too high at scale.

What to Do Next

If you're running an AI product in production, start with this priority order:

  1. Audit your prompts — count your system prompt tokens. If it's over 1,000 tokens, it probably has room to compress.
  2. Implement exact-match caching — Redis, one afternoon of work, immediate cost reduction.
  3. Set up model routing — classify queries by complexity, route simple ones to Mini.
  4. Move async tasks to Batch API — instant 50% reduction on non-real-time calls.
  5. Add semantic caching — the most impactful but also most complex step. Worth it at scale.

Do them in order. Each one pays for the next. After all five, you'll be looking at a very different API bill.

Running High LLM Costs in Production?

We've helped AI platforms cut LLM costs by 40-70% without touching output quality. Book a free 30-minute cost audit — we'll review your architecture and tell you exactly where the savings are.

#LLMCosts#GPT4Optimisation#AIEngineering#GenerativeAI#CostOptimisation#OpenAI#LangChain#AIProductDevelopment
VE
Vikgol Engineering Team
AI Engineering & LLM Development · Vikgol
The Vikgol engineering team has shipped 90+ AI, web, and cloud projects for startups and enterprises across US, UK, UAE, and India. We write about what we build — practical guides, real case studies, and honest takes on what works in production AI.

Transform Your Business with Vikgol's Comprehensive Digital Solutions

Contact us
Business transformation services illustration