Overview
The LLM Gateway server is built with Hono and Bun, providing HTTP/SSE endpoints for agent orchestration. The server manages per-session orchestrators with automatic cleanup and supports multiple AI providers through a unified interface.Prerequisites
Before setting up the server, ensure you have:- Bun v1.0 or later installed (installation guide)
- API keys for at least one provider:
- OpenRouter API key
- Zen API key
- Or other supported providers (Anthropic, OpenAI)
Installation
Clone the repository and install dependencies:Environment Setup
Create a.env file from the example:
Development Mode
Start the server with hot reload:http://localhost:4000 (or the port specified in your .env file).
Development mode includes hot reload - the server automatically restarts when you modify source files.
Production Mode
For production deployment, run the server directly:Production Considerations
Process Management
Use a process manager like PM2, systemd, or Docker to ensure your server stays running:
Reverse Proxy
Place the server behind a reverse proxy (nginx, Caddy) for:
- SSL/TLS termination
- Load balancing
- Request rate limiting
Server Architecture
The server is built around thecreateApp factory function with the following structure:
Core Components
Available Endpoints
| Endpoint | Method | Description |
|---|---|---|
/models | GET | List available models from configured providers |
/chat | POST | Stream agent responses via SSE |
/chat/relay/:relayId | POST | Resolve permission relays (human-in-the-loop) |
/summarize | POST | Summarize conversation history |
See the HTTP API Overview for detailed endpoint documentation.
Custom Configuration
You can customize the server by providing configuration options tocreateApp:
Session Management
The server creates a freshAgentOrchestrator for each /chat request:
Monitoring & Logging
The server outputs structured logs for each request:LOG_LEVEL environment variable (see Environment Variables).
Health Checks
Implement health checks by querying the/models endpoint:
Troubleshooting
Server won't start
Server won't start
- Verify Bun is installed:
bun --version - Check port availability:
lsof -i :4000 - Validate
.envfile exists and has proper permissions - Review console output for specific error messages
API key errors
API key errors
- Ensure at least one provider API key is set in
.env - Verify API keys are valid and have proper permissions
- Check provider-specific documentation for key requirements
SSE connection issues
SSE connection issues
- Disable proxy buffering in reverse proxy configuration
- Increase proxy timeout settings for long-running requests
- Verify client supports EventSource or proper SSE handling
Memory leaks with long sessions
Memory leaks with long sessions
- Verify orchestrators are being cleaned up (check logs)
- Monitor memory usage:
bun --inspect server/index.ts - Consider implementing session timeouts for idle connections
Next Steps
Configuration
Learn about advanced configuration options
Environment Variables
Complete reference for all environment variables
