Production HTTP server for LangChain/LangGraph agents โ OpenAI-compatible API, streaming, session memory and agent discovery
Project description
Fast LangChain Server
๐ Production HTTP server for LangChain/LangGraph agents with OpenAI-compatible API, streaming, session memory, and agent discovery.
Overview
Fast LangChain Server transforms your LangChain/LangGraph agents into production-ready HTTP services. Deploy agents with a single line of code, get streaming responses, automatic session management, and built-in observability.
Perfect for building:
- ๐ค Agent APIs for client applications
- ๐ Multi-agent orchestration backends
- ๐ LLM-powered microservices
- ๐ฏ RAG systems with tool calling
Features
- OpenAI-Compatible API โ Drop-in compatible with OpenAI clients and third-party tools
- Streaming Responses โ Real-time token streaming with Server-Sent Events (SSE)
- Session Memory โ Automatic conversation history management with optional Redis persistence
- Agent Discovery โ Built-in card endpoint to inspect agent capabilities and tools
- Tool Calling โ Native LangChain tool integration with automatic marshaling
- Production Ready โ Health checks, structured logging, request tracing, OpenTelemetry support
- Type Safe โ Pydantic validation on all endpoints
- Multiple Patterns โ Works with
create_agent()or customCompiledStateGraph
Quick Start
Installation
pip install fast-langchain-server
Or with development dependencies:
git clone https://github.com/yourusername/fast-langchain-server.git
cd fast-langchain-server
make dev
Minimal Example
Create an agent.py:
from langchain.agents import create_agent
from langchain_core.tools import tool
from fast_langchain_server import serve
@tool
def add(a: float, b: float) -> float:
"""Add two numbers."""
return a + b
@tool
def greet(name: str) -> str:
"""Greet someone."""
return f"Hello, {name}!"
# Create your agent
agent = create_agent(
model="openai:gpt-4o",
tools=[add, greet],
system_prompt="You are a helpful assistant."
)
# Wrap it as an HTTP service
app = serve(agent, tools=[add, greet])
Run it:
AGENT_NAME=my-agent \
MODEL_API_URL=https://api.openai.com/v1 \
MODEL_NAME=gpt-4o \
OPENAI_API_KEY=sk-... \
fast-langchain-server run agent.py
Visit http://localhost:8000/health to verify it's running.
Usage Patterns
Pattern A: Using create_agent
The modern LangChain 1.x API. Uses LangGraph under the hood:
from langchain.agents import create_agent
from fast_langchain_server import serve
agent = create_agent(
model="openai:gpt-4o",
tools=TOOLS,
system_prompt="Your system prompt here"
)
app = serve(agent, tools=TOOLS)
Pattern B: Custom LangGraph Graph
For more control, bring your own CompiledStateGraph:
from langgraph.prebuilt import create_react_agent
from fast_langchain_server import serve
graph = create_react_agent(model, tools=TOOLS)
app = serve(graph, tools=TOOLS)
API Endpoints
POST /invoke
Invoke the agent synchronously.
curl -X POST http://localhost:8000/invoke \
-H "Content-Type: application/json" \
-d '{"messages": [{"role": "user", "content": "What is 2+2?"}]}'
Response:
{
"output": "4",
"session_id": "sess_abc123...",
"messages": [...]
}
POST /stream
Invoke the agent with streaming (SSE).
curl -N http://localhost:8000/stream \
-H "Content-Type: application/json" \
-d '{"messages": [{"role": "user", "content": "Tell me a joke"}]}'
Streams delimited JSON events in real-time.
GET /card
Inspect the agent's configuration and available tools.
curl http://localhost:8000/card
Response:
{
"name": "my-agent",
"description": "A helpful assistant",
"tools": [
{
"name": "add",
"description": "Add two numbers",
"input_schema": {...}
}
]
}
GET /health
Health check endpoint.
curl http://localhost:8000/health
Configuration
Set via environment variables:
| Variable | Required | Default | Description |
|---|---|---|---|
AGENT_NAME |
โ | โ | Identifier for your agent |
MODEL_API_URL |
โ | โ | Base URL for LLM API (e.g., OpenAI, Ollama) |
MODEL_NAME |
โ | โ | Model identifier (e.g., gpt-4o, llama3.2) |
OPENAI_API_KEY |
For OpenAI | โ | API key (or use model-specific auth) |
AGENT_TIMEOUT |
โ | 300 |
Request timeout in seconds |
REDIS_URL |
For persistence | โ | Redis connection URL for session memory |
OTEL_EXPORTER_OTLP_ENDPOINT |
For tracing | โ | OpenTelemetry collector endpoint |
Docker
Build
make docker-build
Or manually:
docker build -t langchain-agent-server .
Run
docker run -p 8000:8000 \
-e AGENT_NAME=my-agent \
-e MODEL_API_URL=https://api.openai.com/v1 \
-e MODEL_NAME=gpt-4o \
-e OPENAI_API_KEY=sk-... \
-v $(pwd)/agent.py:/app/agent.py \
langchain-agent-server
Development
Install dev dependencies
make dev
Run tests
make test
Lint
make lint
Run locally with reload
make run
(Edit agent.py and it will reload automatically)
Clean build artifacts
make clean
Architecture
โโโโโโโโโโโโโโโโโโโโโโโ
โ Client / LLM App โ
โโโโโโโโโโโโฌโโโโโโโโโโโ
โ
HTTP โ OpenAI-compatible
โ
โโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโ
โ FastAPI Server โ
โ โโ /invoke โ
โ โโ /stream โ
โ โโ /card โ
โ โโ /health โ
โโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโดโโโโโโโ
โ โ
โโโโโผโโโโโ โโโโโผโโโโโโโโโโโ
โ Agent โ โ Session Mem โ
โ (User) โ โ (Optional โ
โ โ โ Redis) โ
โโโโโโโโโโ โโโโโโโโโโโโโโโโ
Performance
- ~100ms latency for simple tool calls (OpenAI API)
- Streaming reduces perceived latency by delivering tokens in real-time
- Async I/O handles concurrent requests efficiently
- Redis caching of session memory reduces round-trips
Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Roadmap
- Batching API for multiple agent invocations
- Custom session backends (PostgreSQL, DynamoDB)
- Cost tracking and rate limiting per agent
- Agent versioning and canary deployments
- Built-in monitoring dashboard
- WebSocket support for real-time bidirectional communication
License
This project is licensed under the MIT License โ see the LICENSE file for details.
Support
- ๐ Documentation
- ๐ Issues
- ๐ฌ Discussions
Made with โค๏ธ for the AI community
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fast_langchain_server-0.1.2.tar.gz.
File metadata
- Download URL: fast_langchain_server-0.1.2.tar.gz
- Upload date:
- Size: 53.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d81513a2f6c94c19e89442e5e3705f2085d3adbc5777f07aaca764f17347767a
|
|
| MD5 |
56a4bb26bc0896bb894ccd257982404f
|
|
| BLAKE2b-256 |
13710d63265e2c36bb7c1ca3b7a8b2b06cf1209f8f1f617303a7cee8a0e128d1
|
File details
Details for the file fast_langchain_server-0.1.2-py3-none-any.whl.
File metadata
- Download URL: fast_langchain_server-0.1.2-py3-none-any.whl
- Upload date:
- Size: 29.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
02831edcf526259db8dfbb32503a1bb1ede635d4d91d0adab3b28b6480944b59
|
|
| MD5 |
160d6c1852f18a421df0fa7bec211623
|
|
| BLAKE2b-256 |
59b3eef6c42c61223b625d232bd9308ae5a6403f2226c1bc5fb44afc6b3788aa
|