AWS Builder Center
Building an LLM Cost Optimization Gateway with FastAPI: A Practical Guide

Building an LLM Cost Optimization Gateway with FastAPI: A Practical Guide

Learn how to design a FastAPI gateway that routes LLM requests based on complexity, latency, and cost. Explore model routing, fallback strategies, observability, and practical approaches to building more efficient AI applications.

Large Language Models (LLMs) make it easier to build intelligent applications, from document assistants to autonomous agents. However, sending every prompt to top-tier foundation models introduces substantial computational and financial overhead. While frontier models excel at multi-step reasoning, smaller, task-specific models handle extraction, formatting, and lightweight classification equally well at a fraction of the cost.An effective solution is an LLM Gateway positioned between your client applications and model providers. Instead of hardcoding model endpoints, the gateway evaluates each request, applies intelligent routing logic, executes fallbacks when needed, and collects runtime telemetry.In this tutorial, we will explore how to design a clean, extensible LLM cost optimization gateway using Python and FastAPI.1. Gateway Architecture OverviewA production-grade LLM routing gateway relies on five core components:API Layer: Exposes client-facing endpoints and handles authentication, schema validation, and rate limiting.Request Classifier: Evaluates the prompt to determine task category and infer computational complexity.Model Router: Maps classification signals to target models using dynamic, cost-aware rules.Fallback Handler: Gracefully handles provider rate limits, network timeouts, and downtime.Observability Layer: Tracks latency, token counts, error rates, and financial metrics per request.PlaintextClient Application
│
▼
FastAPI Gateway
│
▼
Request Classifier ──► Model Router ──► Target LLM Provider
│ │
Fallback Handler ◄──────┘ (on timeout/error)
│
▼
Observability Layer (Metrics & Traces)
2. Project SetupCreate an isolated directory and configure a virtual environment:Bashmkdir llm-cost-gateway
cd llm-cost-gateway
python -m venv .venv
Activate the virtual environment:macOS / Linux:Bashsource .venv/bin/activate
Windows:Bash.venv\Scripts\activate
Install FastAPI and Uvicorn:Bashpip install fastapi uvicorn pydantic
3. Implementing the Routing APICreate a main.py file with an extensible routing skeleton:Pythonfrom fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field
app = FastAPI(
title="LLM Cost Optimization Gateway",
version="1.0.0"
)
class ChatRequest(BaseModel):
prompt: str = Field(..., min_length=1, description="The input prompt for inference")
class RouteResponse(BaseModel):
selected_model: str
prompt_length: int
routing_tier: str
message: str
def select_model(prompt: str) -> dict:
word_count = len(prompt.split())
1
2
3
4
5
6
7
8
9
10
11
# Baseline heuristic routing logic
if word_count > 80:
return {
"model": "claude-3-opus / gpt-4o",
"tier": "advanced"
}

return {
"model": "claude-3-haiku / gpt-4o-mini",
"tier": "efficient"
}
@app.post("/route", response_model=RouteResponse)
def route_request(request: ChatRequest):
routing_decision = select_model(request.prompt)
1
2
3
4
5
6
return RouteResponse(
selected_model=routing_decision["model"],
prompt_length=len(request.prompt.split()),
routing_tier=routing_decision["tier"],
message="Routing decision evaluated successfully"
)
@app.get("/health")
def health_check():
return {"status": "healthy"}
Launch the local development server:Bashuvicorn main:app --reload
Navigate to your local interactive documentation endpoint at /docs (e.g., localhost:8000/docs) in your browser to inspect schemas and execute test queries.4. Designing a Resilient Routing StrategyWord count or prompt token length serves only as a starting point. A concise query ("Write a formal verification proof for this concurrent cache") often requires vastly more reasoning capacity than a 500-word passage summarization.To build an enterprise-ready router, evaluate:Task Categorization: Distinguish semantic classification and data extraction from code generation and symbolic reasoning.Token Budgets: Project input and anticipated completion tokens before assigning the target model.SLA & Latency Constraints: Route latency-critical background pipelines to faster SLMs (Small Language Models).Confidence & Fallback Scoring: Attempt completion with an efficient model first; escalate to a reasoning model if confidence or formatting validation fails.5. Reliability and Production SafeguardsTo prevent cascade failures and billing anomalies across upstream providers, configure:Timeouts & Exponential Backoff: Avoid hanging connections by bounding request lifetimes to strict thresholds (e.g., 5 seconds for lightweight models).Tiered Fallbacks: If a high-throughput provider experiences rate limits (HTTP 429), immediately failover to a configured secondary engine.Payload Validation: Enforce maximum character and token allowances at the gateway boundary to mitigate prompt injection and runaway token usage.Secrets Security: Never hardcode provider credentials. Inject keys using AWS Secrets Manager or secure runtime environment variables.6. Measuring Cost Optimization & ObservabilityCost savings must be empirically validated against a baseline where all requests route to a flagship model.Track the following dimensions per transaction:Token usage (input vs. completion breakdown)Calculated cost per invocationRound-trip latency (P50, P95, P99)Error and fallback invocation frequency$$\text{Cost Reduction (%)} = \left( \frac{\text{Baseline Cost} - \text{Gateway Cost}}{\text{Baseline Cost}} \right) \times 100$$For example, on a workload where standard routing costs $100 and gateway optimization lowers expenses to $68, your operational savings equate to 32%.7. Cloud Deployment on AWSOnce validated locally, package the FastAPI gateway into a container image and deploy it across AWS compute primitives:Compute: Run containers on AWS Fargate with Amazon ECS for auto-scaling serverless microservices, or deploy lightweight endpoints on AWS Lambda via Mangum.Observability: Instrument spans with the AWS Distro for OpenTelemetry (ADOT), funneling application traces into AWS X-Ray and performance metrics into Amazon CloudWatch.Edge Security: Position an Amazon API Gateway or Application Load Balancer (ALB) in front of your container cluster with AWS WAF enabled to prevent unwanted traffic.ConclusionAn LLM cost optimization gateway decouples your application logic from downstream AI model providers, providing centralized control over inference expenses, routing intelligence, and resilience.Start by auditing your application's prompt distribution, establish a benchmark evaluation suite, and gradually transition from simple heuristic routing to ML-driven classification.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article