| Crates.io | ultrafast-gateway |
| lib.rs | ultrafast-gateway |
| version | 0.1.6 |
| created_at | 2025-08-23 15:40:43.895197+00 |
| updated_at | 2025-08-23 16:43:47.86012+00 |
| description | High-performance AI gateway built in Rust with unified interface to 10+ LLM providers |
| homepage | |
| repository | https://github.com/techgopal/ultrafast-ai-gateway |
| max_upload_size | |
| id | 1807619 |
| size | 1,144,723 |
A high-performance AI gateway built in Rust that provides a unified interface to 10+ LLM providers with advanced routing, caching, and monitoring capabilities.
cargo add ultrafast-gateway
git clone https://github.com/techgopal/ultrafast-ai-gateway.git
cd ultrafast-ai-gateway/ultrafast-gateway
cargo build --release
use ultrafast_gateway::{Gateway, GatewayConfig, ProviderConfig};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create gateway configuration
let config = GatewayConfig::default()
.with_provider(ProviderConfig::openai("your-openai-key"))
.with_provider(ProviderConfig::anthropic("your-anthropic-key"))
.with_cache_enabled(true)
.with_rate_limiting(true);
// Initialize gateway
let gateway = Gateway::new(config).await?;
// Start the server
gateway.serve("127.0.0.1:3000").await?;
Ok(())
}
use ultrafast_gateway::{
Gateway, GatewayConfig, ProviderConfig,
CacheConfig, RateLimitConfig, CircuitBreakerConfig
};
let config = GatewayConfig::default()
.with_provider(ProviderConfig::openai("sk-...")
.with_circuit_breaker(CircuitBreakerConfig {
failure_threshold: 5,
recovery_timeout: Duration::from_secs(60),
request_timeout: Duration::from_secs(30),
}))
.with_cache(CacheConfig {
ttl: Duration::from_secs(3600),
max_size: 10000,
eviction_policy: EvictionPolicy::LRU,
})
.with_rate_limiting(RateLimitConfig {
requests_per_minute: 100,
burst_size: 20,
per_user: true,
})
.with_authentication(true)
.with_monitoring(true);
[server]
host = "0.0.0.0"
port = 3000
workers = 4
[providers.openai]
api_key = "your-openai-key"
base_url = "https://api.openai.com/v1"
timeout = 30
max_retries = 3
[providers.anthropic]
api_key = "your-anthropic-key"
base_url = "https://api.anthropic.com"
timeout = 30
max_retries = 3
[cache]
enabled = true
ttl = 3600
max_size = 10000
eviction_policy = "lru"
[rate_limiting]
enabled = true
requests_per_minute = 100
burst_size = 20
per_user = true
[authentication]
enabled = true
jwt_secret = "your-jwt-secret"
api_key_header = "X-API-Key"
[monitoring]
enabled = true
metrics_port = 9090
health_check_interval = 30
export ULTRAFAST_GATEWAY_HOST=0.0.0.0
export ULTRAFAST_GATEWAY_PORT=3000
export ULTRAFAST_GATEWAY_OPENAI_API_KEY=sk-...
export ULTRAFAST_GATEWAY_ANTHROPIC_API_KEY=sk-ant-...
export ULTRAFAST_GATEWAY_JWT_SECRET=your-secret
# OpenAI-compatible endpoint
POST /v1/chat/completions
Content-Type: application/json
Authorization: Bearer your-api-key
{
"model": "gpt-4",
"messages": [
{"role": "user", "content": "Hello, world!"}
],
"max_tokens": 100
}
# OpenAI-compatible endpoint
POST /v1/completions
Content-Type: application/json
Authorization: Bearer your-api-key
{
"model": "text-davinci-003",
"prompt": "Hello, world!",
"max_tokens": 100
}
POST /v1/embeddings
Content-Type: application/json
Authorization: Bearer your-api-key
{
"model": "text-embedding-ada-002",
"input": "Hello, world!"
}
GET /v1/models
Authorization: Bearer your-api-key
GET /health
GET /metrics
use ultrafast_gateway::plugins::ContentFilteringPlugin;
let plugin = ContentFilteringPlugin::new()
.with_filters(vec![
"hate_speech".to_string(),
"violence".to_string(),
"sexual_content".to_string(),
])
.with_moderation_api("https://api.moderation.com");
gateway.add_plugin(plugin);
use ultrafast_gateway::plugins::CostTrackingPlugin;
let plugin = CostTrackingPlugin::new()
.with_cost_limits(vec![
("daily", 100.0),
("monthly", 1000.0),
])
.with_alert_threshold(0.8);
gateway.add_plugin(plugin);
use ultrafast_gateway::plugins::LoggingPlugin;
let plugin = LoggingPlugin::new()
.with_level(log::Level::Info)
.with_format(LogFormat::JSON)
.with_output(LogOutput::File("gateway.log".into()));
gateway.add_plugin(plugin);
Access the built-in dashboard at /dashboard for:
# prometheus.yml
scrape_configs:
- job_name: 'ultrafast-gateway'
static_configs:
- targets: ['localhost:9090']
metrics_path: '/metrics'
// Enable connection pooling
let config = GatewayConfig::default()
.with_connection_pool_size(100)
.with_keep_alive_timeout(Duration::from_secs(60));
// Optimize cache settings
let cache_config = CacheConfig {
ttl: Duration::from_secs(3600),
max_size: 50000,
eviction_policy: EvictionPolicy::LRU,
compression: true,
};
// Configure circuit breakers
let circuit_breaker = CircuitBreakerConfig {
failure_threshold: 3,
recovery_timeout: Duration::from_secs(30),
request_timeout: Duration::from_secs(10),
half_open_max_calls: 5,
};
docker run -p 3000:3000 \
-v /path/to/config:/app/config.toml \
ghcr.io/techgopal/ultrafast-ai-gateway:latest
version: '3.8'
services:
ultrafast-gateway:
image: ghcr.io/techgopal/ultrafast-ai-gateway:latest
ports:
- "3000:3000"
- "9090:9090"
volumes:
- ./config.toml:/app/config.toml
- ./logs:/app/logs
environment:
- RUST_LOG=info
- RUST_BACKTRACE=1
restart: unless-stopped
apiVersion: apps/v1
kind: Deployment
metadata:
name: ultrafast-gateway
spec:
replicas: 3
selector:
matchLabels:
app: ultrafast-gateway
template:
metadata:
labels:
app: ultrafast-gateway
spec:
containers:
- name: gateway
image: ghcr.io/techgopal/ultrafast-ai-gateway:latest
ports:
- containerPort: 3000
- containerPort: 9090
env:
- name: RUST_LOG
value: "info"
volumeMounts:
- name: config
mountPath: /app/config.toml
subPath: config.toml
volumes:
- name: config
configMap:
name: gateway-config
cargo test
cargo test --test integration
cargo test --test load_testing
cargo bench
// examples/basic_gateway.rs
use ultrafast_gateway::{Gateway, GatewayConfig, ProviderConfig};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = GatewayConfig::default()
.with_provider(ProviderConfig::openai("your-key"))
.with_cache_enabled(true);
let gateway = Gateway::new(config).await?;
gateway.serve("127.0.0.1:3000").await?;
Ok(())
}
// examples/advanced_routing.rs
use ultrafast_gateway::{
Gateway, GatewayConfig, ProviderConfig,
RoutingStrategy, LoadBalancingStrategy
};
let config = GatewayConfig::default()
.with_provider(ProviderConfig::openai("key1"))
.with_provider(ProviderConfig::anthropic("key2"))
.with_routing_strategy(RoutingStrategy::LoadBalanced(
LoadBalancingStrategy::RoundRobin
));
// examples/custom_middleware.rs
use ultrafast_gateway::{
Gateway, GatewayConfig, Middleware, Request, Response
};
struct CustomMiddleware;
#[async_trait]
impl Middleware for CustomMiddleware {
async fn process(&self, request: Request) -> Result<Response, Box<dyn std::error::Error>> {
// Custom processing logic
Ok(request.into())
}
}
let gateway = Gateway::new(config)
.with_middleware(CustomMiddleware)
.await?;
We welcome contributions! Please see our Contributing Guide for details.
git clone https://github.com/techgopal/ultrafast-ai-gateway.git
cd ultrafast-ai-gateway
cargo build
cargo test
cargo fmt before committingcargo testThis project is licensed under the MIT License - see the LICENSE file for details.
For enterprise support and consulting, contact:
Built with โค๏ธ in Rust by the Ultrafast Gateway Team