OpenModex Rust SDK
The official Rust SDK for the OpenModex AI Gateway API. Access 100+ LLM models from OpenAI, Anthropic, Google, DeepSeek, Mistral, and Qwen through a single unified API with intelligent routing, automatic fallbacks, and built-in cost tracking.
Features
- Unified API -- One client for all major LLM providers
- Smart Routing -- Automatic model selection optimized for cost, latency, or quality
- Client-Side Fallbacks -- Automatic retry with backup models on failure
- Streaming -- First-class SSE streaming via
futures::Stream - Async/Await -- Built on
tokio+reqwestfor high-performance async I/O - Type Safe -- Strongly typed request/response structs with
serde - Builder Pattern -- Ergonomic request construction
- Automatic Retries -- Exponential backoff on 429/5xx errors
- OpenAI Compatible -- Drop-in replacement by changing
base_url
Requirements
- Rust 1.70+ (edition 2021)
Installation
Add to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["full"] }
= "0.3" # Only needed for streaming
Quick Start
use ;
async
Or use the OPENMODEX_API_KEY environment variable:
let client = from_env?;
Usage
Chat Completions
let response = client.chat.completions.create.await?;
println!;
Streaming
use StreamExt;
let mut stream = client.chat.completions.create_stream.await?;
while let Some = stream.next.await
Smart Routing (OpenModex Extension)
Let the gateway pick the best model or optimize for cost/latency:
use RoutingConfig;
let response = client.chat.completions.create.await?;
OpenModex Metadata
Every response includes OpenModex-specific metadata:
let response = client.chat.completions.create.await?;
if let Some = &response.openmodex
Cache Control
use CacheConfig;
let response = client.chat.completions.create.await?;
Client-Side Fallbacks
Automatically retry with backup models on failure:
let client = builder
.api_key
.fallback_models
.build?;
// If gpt-4o fails (5xx/timeout), automatically tries the next model
let response = client.chat.completions.create.await?;
Embeddings
use EmbeddingRequest;
let response = client.embeddings.create.await?;
println!;
Models
// List all available models
let models = client.models.list.await?;
for m in &models.data
// Get a specific model
let model = client.models.get.await?;
println!;
// Compare models side by side
let comparison = client.models.compare.await?;
if let Some = &comparison.highlights
Legacy Completions
use CompletionRequest;
let response = client.completions.create.await?;
println!;
Error Handling
use ;
match client.chat.completions.create.await
Configuration
| Method | Description | Default |
|---|---|---|
.api_key(key) |
Your OpenModex API key | OPENMODEX_API_KEY env var |
.base_url(url) |
API base URL | https://api.openmodex.com/v1 |
.timeout(duration) |
Request timeout | 30s |
.max_retries(n) |
Max retry attempts on transient errors | 2 |
.default_model(model) |
Default model when none specified | None |
.fallback_models(models) |
Ordered fallback model chain | [] |
.default_headers(headers) |
Headers sent with every request | {} |
All options are set via ClientBuilder:
use Duration;
let client = builder
.api_key
.base_url
.timeout
.max_retries
.default_model
.fallback_models
.build?;
OpenAI SDK Compatibility
OpenModex Gateway supports drop-in compatibility. If you are already using an OpenAI-compatible Rust crate, you can route through OpenModex by changing just the base URL:
// With the `async-openai` crate
let config = new
.with_api_base
.with_api_key;
let client = with_config;
Examples
See the examples/ directory for runnable examples:
# Set your API key
# Run examples
- quickstart -- Basic chat completion
- streaming -- SSE streaming
- models -- List, retrieve, and compare models
- fallback -- Client-side fallback chain