GitHub - faahmed/LitServe: Easiest way to serve AI in production. Build custom runtimes for models, agents, multi-modal systems, RAG, pipelines and more. · GitHub
You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The framework to build custom model runtimes with expert control.
Runtimes for models, agents, MCP, multi-modal, RAG, and pipelines.
No MLOps. No YAML.
✅ Build your own inference engine ✅ 2× faster than FastAPI ✅ Agents, RAG, pipelines, more
✅ Custom logic + control ✅ Any PyTorch model ✅ Self-host or managed
✅ Multi-GPU autoscaling ✅ Batching + streaming ✅ BYO model or vLLM
✅ No MLOps glue code ✅ Easy setup in Python ✅ Serverless support
LitServe lets you build your own inference engine. Serving engines such as vLLM serve specific model types (LLMs) with rigid abstractions. LitServe gives you the low-level control to serve any model (vision, audio, text, multi-modal), and define exactly how inference works - from batching, caching, streaming, and routing, to multi-model orchestration and custom logic. LitServe is perfect for building inference APIs, agents, chatbots, MCP servers, RAG, pipelines and more.
Self host LitServe or deploy in one-click to Lightning AI.
importlitserveasls# define the api to include any number of models, dbs, etc...classInferencePipeline(ls.LitAPI):
defsetup(self, device):
self.model1=lambdax: x**2self.model2=lambdax: x**3defpredict(self, request):
x=request["input"]
# perform calculations using both modelsa=self.model1(x)
b=self.model2(x)
c=a+breturn {"output": c}
if__name__=="__main__":
# 12+ features like batching, streaming, etc...server=ls.LitServer(InferencePipeline(max_batch_size=1), accelerator="auto")
server.run(port=8000)
# Deploy for free with autoscaling, monitoring, etc...
lightning deploy server.py --cloud
# Or run locally (self host anywhere)
lightning deploy server.py
# python server.py
Test the server: Simulate an http request (run this on any terminal):
curl -X POST http://127.0.0.1:8000/predict -H "Content-Type: application/json" -d '{"input": 4.0}'
Agent example
importre, requests, openaiimportlitserveaslsclassNewsAgent(ls.LitAPI):
defsetup(self, device):
self.openai_client=openai.OpenAI(api_key="OPENAI_API_KEY")
defpredict(self, request):
website_url=request.get("website_url", "https://text.npr.org/")
website_text=re.sub(r'<[^>]+>', ' ', requests.get(website_url).text)
# ask the LLM to tell you about the newsllm_response=self.openai_client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": f"Based on this, what is the latest: {website_text}"}],
)
output=llm_response.choices[0].message.content.strip()
return {"output": output}
if__name__=="__main__":
server=ls.LitServer(NewsAgent())
server.run(port=8000)
Test it:
curl -X POST http://127.0.0.1:8000/predict -H "Content-Type: application/json" -d '{"website_url": "https://text.npr.org/"}'
Key benefits
A few key benefits:
Deploy any pipeline or model: Agents, pipelines, RAG, chatbots, image models, video, speech, text, etc...
No MLOps glue: LitAPI lets you build full AI systems (multi-model, agent, RAG) in one place (more).
Instant setup: Connect models, DBs, and data in a few lines with setup() (more).
Optimized: autoscaling, GPU support, and fast inference included (more).
Deploy anywhere: self-host or one-click deploy with Lightning (more).
FastAPI for AI: Built on FastAPI but optimized for AI - 2× faster with AI-specific multi-worker handling (more).
Expert-friendly: Use vLLM, or build your own with full control over batching, caching, and logic (more).
⚠️ Not a vLLM or Ollama alternative out of the box. LitServe gives you lower-level flexibility to build what they do (and more) if you need it.
Featured examples
Here are examples of inference pipelines for common model types and use cases.
Self-host with full control, or deploy with Lightning AI in seconds with autoscaling, security, and 99.995% uptime. Free tier included. No setup required. Run on your cloud
LitServe is designed for AI workloads. Specialized multi-worker handling delivers a minimum 2x speedup over FastAPI.
Additional features like batching and GPU autoscaling can drive performance well beyond 2x, scaling efficiently to handle more simultaneous requests than FastAPI and TorchServe.
Reproduce the full benchmarks here (higher is better).
These results are for image and text classification ML tasks. The performance relationships hold for other ML tasks (embedding, LLM serving, audio, segmentation, object detection, summarization etc...).
💡 Note on LLM serving: For high-performance LLM serving (like Ollama/vLLM), integrate vLLM with LitServe, use LitGPT, or build your custom vLLM-like server with LitServe. Optimizations like kv-caching, which can be done with LitServe, are needed to maximize LLM performance.