Optimize AI Inference: Smart Routing, Caching, and Compression

We design and deploy artificial intelligence systems: from prototype to production-ready solutions. Our team combines expertise in machine learning, data engineering and MLOps to make AI work not in the lab, but in real business.
Showing 1 of 1All 1566 services
Optimize AI Inference: Smart Routing, Caching, and Compression
Medium
~1-2 weeks
Frequently Asked Questions

AI Development Areas

AI Solution Development Stages

Latest works

  • image_website-b2b-advance_0.webp
    B2B ADVANCE company website development
    1319
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1226
  • image_websites_belfingroup_462_0.webp
    Website development for BELFINGROUP
    927
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1161
  • image_logo-advance_0.webp
    B2B Advance company logo design
    622
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    897

Inference for AI models in production often costs more than expected. LLM API fees, GPU compute, and managed inference services scale non-linearly with traffic. A project with hundreds of thousands of daily requests can easily run tens of thousands of dollars per month. Clients in fintech and e-commerce have reported that 60–80% of their budget goes to inference. They often pay for unnecessarily high-quality responses for simple queries like "What's the dollar exchange rate today?" We systematically optimize these costs without degrading quality. Our experience shows that combining model routing, semantic caching, and quantization reduces the bill by 40–70%. This is confirmed in over 50 deployments. LLM inference optimization is critical for cost efficiency. Our methods significantly reduce LLM API costs. Model quantization is another key technique. Prompt compression drives token savings.

How model routing cuts costs

Model routing dynamically selects a model for each request. Simple tasks go to cheap models, complex ones to powerful models. The method yields 50–70% cost savings. Quality degradation is less than 5%. This is far better than using a single model for all requests.

class IntelligentModelRouter:
    def route_request(self, request: dict) -> str:
        query = request['query']
        complexity = self.complexity_estimator.estimate(query)

        if complexity < 0.3:
            return "gpt-4o-mini"    # $0.15/1M input tokens
        elif complexity < 0.7:
            return "gpt-4o"         # $5.00/1M input tokens
        else:
            return "gpt-4-turbo"    # $10.00/1M input tokens

    def estimate_complexity(self, query: str) -> float:
        # Heuristics: length, presence of code, math, multi-step reasoning
        features = [
            len(query.split()) / 200,
            1.0 if any(kw in query for kw in ['calculate', 'code', 'step-by-step']) else 0,
            1.0 if '```' in query else 0,
        ]
        return min(np.mean(features) * 1.5, 1.0)

What is semantic caching?

Semantic caching stores responses and their embeddings. On a new query, cosine similarity is computed. If above 0.95, the cached response is returned. This covers paraphrases and achieves a 20–40% hit rate without quality loss.

from redis import Redis
import numpy as np

class SemanticCache:
    def __init__(self, similarity_threshold: float = 0.95):
        self.redis = Redis()
        self.threshold = similarity_threshold
        self.embedder = SentenceTransformer('all-MiniLM-L6-v2')

    def get(self, query: str) -> str | None:
        query_emb = self.embedder.encode(query)
        cached_keys = self.redis.keys("cache:*")

        for key in cached_keys:
            cached_data = json.loads(self.redis.get(key))
            cached_emb = np.array(cached_data['embedding'])

            similarity = np.dot(query_emb, cached_emb) / (
                np.linalg.norm(query_emb) * np.linalg.norm(cached_emb)
            )
            if similarity > self.threshold:
                return cached_data['response']
        return None

    def set(self, query: str, response: str, ttl: int = 3600):
        key = f"cache:{hashlib.md5(query.encode()).hexdigest()}"
        self.redis.setex(key, ttl, json.dumps({
            'embedding': self.embedder.encode(query).tolist(),
            'response': response
        }))

Prompt compression and quantization

Context compression via LLMLingua reduces tokens by 30–50% with 1–5% quality degradation. 4-bit quantization via bitsandbytes cuts VRAM by 4x: Llama-2-70B requires ~35GB instead of 140GB. Both methods suit self-hosted scenarios.

# LLMLingua for compressing long contexts
from llmlingua import PromptCompressor

compressor = PromptCompressor(
    model_name="microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank",
    use_llmlingua2=True
)

compressed = compressor.compress_prompt(
    context,
    rate=0.5,  # Compress to 50% tokens
    force_tokens=['\n', '?']
)
# 4-bit quantization via bitsandbytes (GPTQ)
from transformers import AutoModelForCausalLM, BitsAndBytesConfig

quantization_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_compute_dtype=torch.bfloat16,
    bnb_4bit_use_double_quant=True,
    bnb_4bit_quant_type="nf4"
)

model = AutoModelForCausalLM.from_pretrained(
    "meta-llama/Llama-2-70b-chat-hf",
    quantization_config=quantization_config,
    device_map="auto"
)

Step-by-step plan for implementing model routing

  1. Audit current requests: collect logs for a week, cluster by complexity (length, code presence, number of steps). Define thresholds for switching between models.
  2. Choose models: pick 2–3 models with different price/quality trade-offs (e.g., GPT-4o-mini, GPT-4o, GPT-4-turbo).
  3. Implement the router: use the IntelligentModelRouter class above. Tune heuristics for your domain.
  4. A/B testing: run the router on 10% of traffic, compare response quality and costs. Optimize thresholds.
  5. Gradual rollout: increase traffic share to 100% once metrics are stable.

Expected cost savings

Method Cost reduction Quality degradation
Model routing 50–70% <5%
Semantic caching 20–40% 0%
Prompt compression 30–50% 1–5%
4-bit quantization 40–60% (self-hosted) 1–3%
Batch inference 30–50% 0%

Combining model routing and semantic caching gives the greatest effect without risk for most production scenarios. For example, an e-commerce client with 2M daily requests cut monthly costs from $28,000 to $8,400. Another fintech project reduced costs from $50,000 to $15,000 using the same methods. Typical monthly savings range from $10,000 to $50,000 for enterprise clients. Deployment evaluations show average savings of 60%.

Comparison of quantization tools

Tool Model support Format Inference speed (tokens/s)
bitsandbytes HuggingFace, LLaMA, Mistral INT8/INT4 ~85% of FP16
GPTQ AutoGPTQ, HuggingFace INT4 ~95% of FP16
AWQ vLLM, HuggingFace INT4 ~90% of FP16

More details on quantization can be found in the bitsandbytes documentation.

What our optimization service includes

  • Cost audit: analyze spending structure by model, cache hit rate, context length, GPU utilization.
  • Recommendations: select model routing strategy, configure cache, set up batch inference.
  • Implementation: deploy code, tune pipeline, optimize infrastructure.
  • Documentation: describe the schema, monitoring instructions, rollback plan.
  • Training: we provide team training on the new pipeline.
  • Access: we provide access to monitoring dashboards.
  • Support: one month of post-deployment consultation.
Additional capabilities We can also integrate prompt compression and quantization for self-hosted models. Deep GPU utilization analysis via NVIDIA Nsight and MLflow identifies bottlenecks.

We will assess your project in 2–3 days. Order an audit — we guarantee transparent pricing and a clear savings plan. Our team has 5+ years of MLOps experience and has delivered over 50 inference optimization projects for fintech, e-commerce, and SaaS clients. With over 5 years in the market and 50+ projects delivered, we are a trusted partner in AI optimization. AI inference cost optimization is our specialty. Get a free consultation to discuss your case.

Our optimization covers all key techniques: model routing, semantic caching, prompt compression, model quantization, batch inference, and GPU utilization. Typical monthly savings for enterprise clients range from $10,000 to $50,000.