A lightning-fast, production-ready API gateway that unifies OpenAI and Google Gemini models into a single endpoint. Built with Rust for maximum performance and reliability.
- π Multi-Provider Support - Seamlessly integrate OpenAI and Gemini
- β‘ Automatic Failover - Falls back to secondary provider if primary fails
- πΎ Smart Caching - In-memory cache for improved performance and reduced costs
- π― Manual Provider Selection - Override automatic routing when needed
- π Request Logging - Structured logging with tracing for observability
- π Type-Safe - Leverages Rust's type system for reliability
- βοΈ Async/Await - Non-blocking I/O for high concurrency
βββββββββββββββ
β Client β
ββββββββ¬βββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββ
β Axum API Gateway β
β /generate endpoint β
ββββββββ¬βββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββ
β Router (model selection) β
β - Automatic failover β
β - Manual override β
ββββββββ¬βββββββββββββββββββββββββββ
β
ββββββββββββββββ¬ββββββββββββββ
βΌ βΌ βΌ
ββββββββββββ ββββββββββββ βββββββββββ
β OpenAI β β Gemini β β Cache β
β Client β β Client β β Layer β
ββββββββββββ ββββββββββββ βββββββββββ
- Rust 1.70+ (Install Rust)
- OpenAI API Key (Get one here)
- Google Gemini API Key (Get one here)
-
Clone the repository
git clone https://github.com/ProngsDev/ai-gateway.git cd ai-gateway -
Set up environment variables
cp .env.example .env
Edit
.envwith your API keys:PORT=8080 OPENAI_API_KEY=sk-your-openai-key-here GEMINI_API_KEY=your-gemini-key-here
-
Build and run
cargo run --release
The server will start on http://localhost:8080
GET /healthResponse:
AI Gateway is healthy
POST /generate
Content-Type: application/jsonRequest Body:
{
"prompt": "Explain Rust ownership in one sentence",
"provider": "OpenAI" // Optional: "OpenAI" or "Gemini"
}Response:
{
"provider": "OpenAI",
"output": "Rust's ownership system ensures memory safety by enforcing that each value has a single owner, and when the owner goes out of scope, the value is automatically deallocated.",
"cached": false
}Fields:
provider(optional): Specify "OpenAI" or "Gemini". If omitted, automatic failover is used.prompt(required): The text prompt to send to the AI model.
Response Fields:
provider: Which provider generated the responseoutput: The generated textcached: Whether the response was served from cache
curl -X POST http://localhost:8080/generate \
-H "Content-Type: application/json" \
-d '{
"prompt": "What is Rust?"
}'The gateway will:
- Try OpenAI first
- If OpenAI fails, automatically fall back to Gemini
- Cache the successful response
Use OpenAI specifically:
curl -X POST http://localhost:8080/generate \
-H "Content-Type: application/json" \
-d '{
"prompt": "Explain async/await",
"provider": "OpenAI"
}'Use Gemini specifically:
curl -X POST http://localhost:8080/generate \
-H "Content-Type: application/json" \
-d '{
"prompt": "Explain async/await",
"provider": "Gemini"
}'Send the same prompt twice:
# First request - hits OpenAI
curl -X POST http://localhost:8080/generate \
-H "Content-Type: application/json" \
-d '{"prompt": "Hello"}' | jq
# Response: {"provider": "OpenAI", "output": "...", "cached": false}
# Second request - served from cache
curl -X POST http://localhost:8080/generate \
-H "Content-Type: application/json" \
-d '{"prompt": "Hello"}' | jq
# Response: {"provider": "OpenAI", "output": "...", "cached": true}cargo testRUST_LOG=debug cargo runcargo build --releaseThe optimized binary will be in target/release/ai-gateway
docker build -t ai-gateway .docker run -p 8080:8080 \
-e OPENAI_API_KEY=your-key \
-e GEMINI_API_KEY=your-key \
ai-gatewaydocker-compose up -dSee Docker Setup section for details.
ai-gateway/
βββ src/
β βββ main.rs # Server setup & initialization
β βββ routes.rs # API endpoint handlers
β βββ router.rs # Provider routing & failover logic
β βββ cache.rs # In-memory cache implementation
β βββ error.rs # Custom error types
β βββ providers/
β βββ mod.rs # AIProvider trait definition
β βββ openai.rs # OpenAI client implementation
β βββ gemini.rs # Gemini client implementation
βββ Cargo.toml # Dependencies & metadata
βββ .env # Environment variables (gitignored)
βββ Dockerfile # Docker build configuration
βββ docker-compose.yml # Docker Compose setup
βββ README.md # This file
| Variable | Description | Required | Default |
|---|---|---|---|
PORT |
Server port | No | 8080 |
OPENAI_API_KEY |
OpenAI API key | Yes | - |
GEMINI_API_KEY |
Google Gemini API key | Yes | - |
The default failover order is:
- OpenAI (primary)
- Gemini (fallback)
To change this, modify the provider order in src/main.rs:
// Current order (OpenAI first)
ai_router.add_provider(openai_client);
ai_router.add_provider(gemini_client);
// To make Gemini primary, swap the order:
ai_router.add_provider(gemini_client);
ai_router.add_provider(openai_client);- SaaS Applications - Add AI features with built-in resilience
- Cost Optimization - Route to cheaper providers, cache expensive calls
- High Availability - Automatic failover ensures uptime
- Multi-Model Apps - Leverage strengths of different models
- Development/Testing - Single API for multiple providers
- Store API keys in environment variables, never in code
- Use HTTPS in production
- Consider adding rate limiting for public deployments
- Implement authentication/authorization as needed
- Async I/O - Non-blocking requests for high concurrency
- In-Memory Cache - Sub-millisecond cache hits
- Compiled - Native performance with Rust
- Lightweight - Minimal resource footprint
Contributions are welcome! Areas for improvement:
- Add more providers (Claude, Llama, Cohere)
- Implement Redis caching for distributed systems
- Add rate limiting
- Prometheus metrics
- Streaming responses (SSE)
- Configuration via YAML/TOML
MIT License - See LICENSE file for details
Built with:
- Axum - Web framework
- Tokio - Async runtime
- Reqwest - HTTP client
- Serde - JSON serialization
- Tracing - Logging
Made with β€οΈ and Rust