Crates.io | ai-gateway |
lib.rs | ai-gateway |
version | |
source | src |
created_at | 2025-02-04 16:52:32.499018+00 |
updated_at | 2025-04-04 13:14:54.582526+00 |
description | AI gateway for managing and routing LLM requests - Govern, Secure, and Optimize your AI Traffic. |
homepage | |
repository | https://github.com/langdb/ai-gateway |
max_upload_size | |
id | 1542183 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
Govern, Secure, and Optimize your AI Traffic. LangDB AI Gateway provides unified interface to all LLMs using OpenAI API format. Built with performance and reliability in mind.
🚀 High Performance
📊 Enterprise Ready
🔒 Data Control
🌟 Hosted Version - Get started in minutes with our fully managed solution
💼 Enterprise Version - Enhanced features for large-scale deployments
Contact our team to learn more about enterprise solutions.
Choose one of these installation methods:
docker run -it \
-p 8080:8080 \
-e LANGDB_KEY=your-langdb-key-here \
langdb/ai-gateway serve
Install from crates.io:
export RUSTFLAGS="--cfg tracing_unstable --cfg aws_sdk_unstable"
cargo install ai-gateway
export LANGDB_KEY=your-langdb-key-here
ai-gateway serve
Test the gateway with a simple chat completion:
# Chat completion with GPT-4
curl http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": "What is the capital of France?"}]
}'
# Or try Claude
curl http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "claude-3-opus",
"messages": [
{"role": "user", "content": "What is the capital of France?"}
]
}'
LangDB AI Gateway currently supports the following LLM providers. Find all the available models here.
Provider | |
---|---|
![]() |
OpenAI |
![]() |
Google Gemini |
![]() |
Anthropic |
![]() |
DeepSeek |
TogetherAI | |
![]() |
XAI |
![]() |
Meta ( Provided by Bedrock ) |
![]() |
Cohere ( Provided by Bedrock ) |
![]() |
Mistral ( Provided by Bedrock ) |
The gateway provides the following OpenAI-compatible endpoints:
POST /v1/chat/completions
- Chat completions
GET /v1/models
- List available models
POST /v1/embeddings
- Generate embeddings
POST /v1/images/generations
- Generate images
Create a config.yaml
file:
providers:
openai:
api_key: "your-openai-key-here"
anthropic:
api_key: "your-anthropic-key-here"
# Supports mustache style variables
gemini:
api_key: {{LANGDB_GEMINI_API_KEY}}
http:
host: "0.0.0.0"
port: 8080
# Run with custom host and port
ai-gateway serve --host 0.0.0.0 --port 3000
# Run with CORS origins
ai-gateway serve --cors-origins "http://localhost:3000,http://example.com"
# Run with rate limiting
ai-gateway serve --rate-hourly 1000
# Run with cost limits
ai-gateway serve --cost-daily 100.0 --cost-monthly 1000.0
# Run with custom database connections
ai-gateway serve --clickhouse-url "clickhouse://localhost:9000"
Download the sample configuration from our repo.
curl -sL https://raw.githubusercontent.com/langdb/ai-gateway/main/config.sample.yaml -o config.sample.yaml
cp config.sample.yaml config.yaml
Command line options will override corresponding config file settings when both are specified.
Rate limiting helps prevent API abuse by limiting the number of requests within a time window. Configure rate limits using:
# Limit to 1000 requests per hour
ai-gateway serve --rate-hourly 1000
Or in config.yaml
:
rate_limit:
hourly: 1000
daily: 10000
monthly: 100000
Cost control helps manage API spending by setting daily, monthly, or total cost limits. Configure cost limits using:
# Set daily and monthly limits
ai-gateway serve \
--cost-daily 100.0 \
--cost-monthly 1000.0 \
--cost-total 5000.0
Or in config.yaml
:
cost_control:
daily: 100.0 # $100 per day
monthly: 1000.0 # $1000 per month
total: 5000.0 # $5000 total
When a cost limit is reached, the API will return a 429 response with a message indicating the limit has been exceeded.
When a rate limit is exceeded, the API will return a 429 (Too Many Requests) response.
LangDB AI Gateway empowers you to implement sophisticated routing strategies for your LLM requests. By utilizing features such as fallback routing, script-based routing, and latency-based routing, you can optimize your AI traffic to balance cost, speed, and availability.
Here's an example of a dynamic routing configuration:
{
"model": "router/dynamic",
"messages": [
{ "role": "system", "content": "You are a helpful assistant." },
{ "role": "user", "content": "What is the formula of a square plot?" }
],
"router": {
"router": "router",
"type": "fallback", // Type: fallback/script/optimized/percentage/latency
"targets": [
{ "model": "openai/gpt-4o-mini", "temperature": 0.9, "max_tokens": 500, "top_p": 0.9 },
{ "model": "deepseek/deepseek-chat", "frequency_penalty": 1, "presence_penalty": 0.6 }
]
},
"stream": false
}
This configuration demonstrates how you can define multiple targets with specific parameters to ensure your requests are handled by the most suitable models. For more detailed information, explore our routing documentation.
The gateway supports OpenTelemetry tracing with ClickHouse as the storage backend. All traces are stored in the langdb.traces
table.
# Create langdb database if it doesn't exist
clickhouse-client --query "CREATE DATABASE IF NOT EXISTS langdb"
# Import the traces table schema
clickhouse-client --query "$(cat sql/traces.sql)"
ai-gateway serve --clickhouse-url "clickhouse://localhost:9000"
You can also set the URL in your config.yaml
:
clickhouse:
url: "http://localhost:8123"
The traces are stored in the langdb.traces
table. Here are some example queries:
-- Get recent traces
SELECT
trace_id,
operation_name,
start_time_us,
finish_time_us,
(finish_time_us - start_time_us) as duration_us
FROM langdb.traces
WHERE finish_date >= today() - 1
ORDER BY finish_time_us DESC
LIMIT 10;
Did you know you can call LangDB APIs directly within ClickHouse? Check out our UDF documentation to learn how to use LLMs in your SQL queries!
For a complete setup including ClickHouse for analytics and tracing, follow these steps:
docker-compose up -d
This will start:
docker/clickhouse/server/config.d
ai-gateway
The gateway will now be running with full analytics and logging capabilities, storing data in ClickHouse.
curl http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": "Ping the server using the tool and return the response"}],
"mcp_servers": [{"server_url": "http://localhost:3004"}]
}'
To get started with development:
config.sample.yaml
to config.yaml
and configure as neededcargo build
to compilecargo test
to run testsWe welcome contributions! Please check out our Contributing Guide for guidelines on:
The gateway uses tracing
for logging. Set the RUST_LOG
environment variable to control log levels:
RUST_LOG=debug cargo run serve # For detailed logs
RUST_LOG=info cargo run serve # For standard logs
This project is released under the Apache License 2.0. See the license file for more information.