Skip to main content
Guides & Strategies7 min readUpdated July 2026

Framework Integrations

Seamless setup for LangChain, LlamaIndex, CrewAI, AutoGen, and FastAPI.

FastAPI Compression Middleware

Compress outgoing LLM prompts or incoming RAG payloads at the API gateway layer:
server.py
from fastapi import FastAPI, Request
from llmslim import compress

app = FastAPI()

@app.post("/v1/chat/completions")
async def generate_response(payload: dict):
    # Intercept system prompt and compress
    raw_prompt = payload["messages"][0]["content"]
    slim_prompt = compress(raw_prompt, target_ratio=0.5).compressed_text
    payload["messages"][0]["content"] = slim_prompt
    
    # Forward compressed payload to LLM provider
    return {"status": "dispatched", "compressed_length": len(slim_prompt)}

Frequently Asked Questions

Is there a LangChain DocumentTransformer available?

Yes. You can use LLMSlimContextCompressor directly as a retriever post-processor or document transformer.

Next Recommended Guides