Skip to main content
Back to Integrations Directory
Backend ServicesPython Microservice Gateway

LLMSlim + FastAPI

Deploy a high-throughput context compression reverse proxy gateway.

Build an enterprise reverse proxy middleware using FastAPI and LLMSlim to intercept and compress API payloads at 100% reliability.

1. Package Installation

Install LLMSlim and the official FastAPI SDK using your package manager:

terminal
pip install llmslim fastapi uvicorn httpx

2. Architecture & Execution Flow

STEP 01

1. Client sends chat completion POST request to FastAPI gateway.

STEP 02

2. Middleware inspects message tokens; applies LLMSlim if length > threshold.

STEP 03

3. Gateway proxies token-dense payload to upstream OpenAI/Anthropic provider.

3. Production Code Pattern

Complete, runnable implementation wrapper for FastAPI:

gateway_server.py
from fastapi import FastAPI, Request, Response
import httpx
from llmslim import compress

app = FastAPI()
client = httpx.AsyncClient()

@app.post("/v1/chat/completions")
async def proxy_chat(request: Request):
    payload = await request.json()
    
    # Intercept and compress prompt contents
    for msg in payload.get("messages", []):
        if len(msg.get("content", "")) > 500:
            msg["content"] = compress(msg["content"], target_ratio=0.5).compressed_text
            
    # Forward payload to upstream model provider
    res = await client.post("https://api.openai.com/v1/chat/completions", json=payload, headers=dict(request.headers))
    return Response(content=res.content, status_code=res.status_code, headers=dict(res.headers))

4. Production Deployment Best Practices

Run FastAPI with Uvicorn or Gunicorn inside Docker container clusters on Kubernetes / AWS ECS.

5. Key Optimization Tips

  • 01.Deploy with gunicorn -w 4 -k uvicorn.workers.UvicornWorker for high concurrent throughput.
  • 02.Set an explicit minimum token threshold before applying compression to skip short queries.

6. Performance Metrics & Benchmark Matrix

Metric DimensionUncompressed PayloadLLMSlim CompressedRecorded Impact
Gateway Payload ThroughputUncompressed Payloads50% Token Reduced PayloadsSub-30ms Proxy Overhead

7. Frequently Asked Questions

Does LLMSlim introduce asynchronous blocking in FastAPI?

No. Core LLMSlim CPU compression finishes in under 30ms, running synchronously within route handlers.

8. Troubleshooting & Diagnostics

Issue: Gateway timeout on high concurrency.
Solution: Increase Uvicorn worker count or deploy behind an NGINX / Envoy load balancer.