An enterprise-grade LLM gateway workspace built with LiteLLM and LangChain wrappers to decouple client-side execution from provider API endpoints. Implements multi-model routing policies, automatic fallback pipelines for resilient disaster recovery, token caching layers, cost estimation callbacks, and real-time input-scrubbing safety guardrails.
The gateway operates as a unified proxy layer intercepting raw LLM requests and routing them dynamically. Through pre-call validation pipelines and post-call auditing hooks, it provides high availability and governance over API costs.
View LLM Gateway Flow Diagram
graph TD
classDef main fill:#1C3C3C,stroke:#333,stroke-width:1px,color:#fff;
classDef llm fill:#3776AB,stroke:#333,stroke-width:1px,color:#fff;
classDef eval fill:#F55036,stroke:#333,stroke-width:1px,color:#fff;
classDef rag fill:#008080,stroke:#333,stroke-width:1px,color:#fff;
classDef store fill:#f4f4f4,stroke:#333,stroke-width:1px,color:#333;
Client([Client Request]) --> Route{LLM Gateway Middleware}
%% Input Guardrails
Route -->|1. Request Scrubbing| P1[Input Guardrail Stack]:::eval
P1 -->|PII Redactor| P1_1["re.sub(EMAIL/PHONE/PAN)"]:::eval
P1 -->|Prompt Injection Check| P1_2["Regex Injection Patterns"]:::eval
P1 -->|Forbidden Topic Check| P1_3["Topic Keyword Filter"]:::eval
%% Routing Engine
P1_1 & P1_2 & P1_3 -->|2. Route Determination| Engine{Routing & Load Balancer Engine}:::main
Engine -->|Latency Strategy| Strat1["Latency-Based Router"]:::main
Engine -->|Least Busy Strategy| Strat2["Least-Busy Router"]:::main
Engine -->|Simple Shuffle Strategy| Strat3["Simple-Shuffle Router"]:::main
%% Cache Check
Strat1 & Strat2 & Strat3 --> CacheCheck{3. Check Cache}:::rag
CacheCheck -->|Cache Hit| ServeCache["Local Cache Response (0ms)"]:::rag
CacheCheck -->|Cache Miss| TargetCall["4. Downstream LLM Invocation"]:::llm
%% Downstream Models & Fallback
TargetCall -->|Primary Model Call| Model1["gemini-1.5-flash / gpt-4o"]:::llm
Model1 -->|Failure / Rate Limit| FallbackChain["Automatic Fallback Engine"]:::llm
FallbackChain -->|Secondary Model Call| Model2["groq/llama-3.3-70b-versatile"]:::llm
FallbackChain -->|Tertiary Model Call| Model3["gpt-4o-mini"]:::llm
%% Success/Failure Response
Model1 & Model2 & Model3 -->|Success| SuccessHook["Success Callback"]:::main
SuccessHook -->|Audit Trail Log| CallLogs([In-Memory / Redis Log Store])
SuccessHook -->|Completion Cost Calc| CostCalc["completion_cost() Computation"]:::main
SuccessHook -->|Cache Write| CacheWrite["Update Local Cache"]:::rag
CacheWrite & ServeCache & CallLogs --> FinalResponse([Final Client Response])
To prevent prompt exploits, security violations, and provider outages, the gateway routes calls through an input-to-output guardrail validation sequence:
graph TD
classDef safety fill:#F55036,stroke:#333,stroke-width:1px,color:#fff;
classDef pipeline fill:#3776AB,stroke:#333,stroke-width:1px,color:#fff;
classDef metric fill:#008080,stroke:#333,stroke-width:1px,color:#fff;
Input([User Prompt]) --> PI_Gate{1. Prompt Injection Gate}:::safety
PI_Gate -->|Safe| PII_Scrub{2. PII Scrubbing Gate}:::safety
PII_Scrub -->|Clean Prompt| Topic_Gate{3. Topic Restriction Gate}:::safety
Topic_Gate -->|Allowed| Router_Exec[Dynamic Router & Load Balancer]:::pipeline
Router_Exec --> Fallback_Gate{4. Auto-Fallback Gate}:::safety
Fallback_Gate -->|Success Response| Log_Gate{5. Cost & Latency Logging Gate}:::pipeline
Log_Gate --> Output([Safe, Cost-Optimized Response]):::metric
-
Prompt Injection Gate (
input_callback)- Mechanism: Inspects user queries against strict heuristics targeting system bypass commands, context override phrases, and system prompt leaks.
- Boundary: Instantly throws validation errors or cancels execution upon detecting override attempts.
-
PII Scrubbing Gate (
redact_pii)- Mechanism: Automatically redacts sensitive identifiers (Emails, Phone Numbers, Aadhaar, PAN, SSN) before logs are serialized or transmitted.
- Boundary: Ensures zero leakages of personal data to upstream API hosts.
-
Fallback & Retry Controller (
fallbacks)-
Mechanism: Transparent failover engine that instantly redirects failed calls to alternative models (e.g., falling back to
gpt-4o-miniifgemini-1.5-flashexperiences rate limits). - Boundary: Shields customer-facing applications from downstream provider downtime.
-
Mechanism: Transparent failover engine that instantly redirects failed calls to alternative models (e.g., falling back to
-
Dynamic Caching Gate (
litellm.cache)- Mechanism: Implements local caching to intercept identical user prompts before hitting model APIs.
-
Boundary: Reduces repeat call latencies to
$< 3\text{ms}$ and cuts downstream billing costs to zero.
- Unified API Gateway β Wraps 100+ model providers under a single
completion()interface. - Intelligent Load Balancing β Routes requests across healthy provider endpoints using shuffle, least-busy, latency-based, or cost-based balancing metrics.
- Dynamic Pre- & Post-Call Hooks β Provides interceptors for real-time guardrail execution, PII scrubbing, and custom request validation.
- In-Memory & Redis Caching β Instantly cache responses to eliminate redundant token consumption.
- Detailed Cost Tracking β Programmatically calculate query and completion prices using LiteLLM's pricing engine.
- Robust LangChain Integration β Incorporate robust model fallbacks and load balancing directly into LangChain execution chains with
ChatLiteLLM.
LLM-Gateways-LITE-LLM/
βββ main.py # Global application runner
βββ pyproject.toml # Python uv dependencies and project metadata
βββ .gitignore # Workspace gitignore rules (safeguards .env keys)
βββ README.md # Project documentation
βββ LLM-Gateway/
βββ llm_gateway.ipynb # Primary implementation notebook (Routing, Caching, Safety, LangChain)
- Python 3.13+
- Installed package manager
uv(recommended) or standardpip - API keys from Google AI Studio, Groq, or OpenAI
# Clone the repository
git clone https://github.com/your-username/LLM-Gateways-LITE-LLM.git
cd LLM-Gateways-LITE-LLM
# Create and sync virtual environment using uv
uv sync
# Add development dependencies if compiling from scratch
uv add litellm langchain langchain-community langchain-openai langchain-litellm python-dotenvCreate a .env file in the root directory:
# Upstream API Credentials
OPENAI_API_KEY=your_openai_key_here
GROQ_API_KEY=your_groq_key_here
GEMINI_API_KEY=your_gemini_key_here
ANTHROPIC_API_KEY=your_anthropic_key_hereLocated in LLM-Gateway/llm_gateway.ipynb, this implementation chains models together. If your primary inference endpoint encounters rate limits or service dropouts, the router instantly redirects the traffic to fallback providers.
from litellm import completion
# Robust multi-provider fallback invocation
response = completion(
model="gemini/gemini-1.5-flash",
messages=[{"role": "user", "content": "Explain load balancing in one sentence."}],
fallbacks=["gpt-4o-mini", "groq/llama-3.3-70b-versatile"]
)
print("Response served by:", response.model)This workflow configures input hook callbacks (litellm.input_callback) to scrub PII and filter malicious prompts before they are sent to the provider APIs.
import litellm
# PII Scrubbing Callback Hook
def pii_input_guardrail(kwargs):
messages = kwargs.get("messages", [])
for msg in messages:
if msg.get("role") == "user":
clean, detected = redact_pii(msg["content"])
if detected:
msg["content"] = clean
litellm.input_callback = [pii_input_guardrail]The gateway is built for modular extensions to match specific production requirements:
| Component | Target File | Modification Details |
|---|---|---|
| Add New Routing Policies | llm_gateway.ipynb | Define custom Router classes or modify routing_strategy parameters (shuffle, least-busy, latency). |
| Integrate Persistent Cache | llm_gateway.ipynb | Swap out the local local Cache type for a redis production-ready cache. |
| Add Custom Logging Backends | llm_gateway.ipynb | Add additional callbacks to litellm.success_callback to log traces directly to Langfuse, Helicone, or internal SQL databases. |
| Extend PII Scrubbing Rules | llm_gateway.ipynb | Append new custom regex definitions to the PII_PATTERNS dictionary to filter domain-specific IDs. |
- LiteLLM Core Engine for robust API orchestration.
- LangChain Integrations for standard LLM wrappers.
Built with LiteLLM & LangChain