ai-lib 🦀✨
A unified, reliable, high-performance multi-provider AI SDK for Rust
A production-grade, provider-agnostic SDK that provides a unified Rust API for 20+ AI platforms and growing (OpenAI, Groq, Anthropic, Gemini, Mistral, Cohere, Azure OpenAI, Ollama, DeepSeek, Qwen, Baidu ERNIE, Tencent Hunyuan, iFlytek Spark, Kimi, HuggingFace, TogetherAI, xAI Grok, OpenRouter, Replicate, Perplexity, AI21, ZhipuAI, MiniMax, and more).
Eliminates fragmented authentication flows, streaming formats, error semantics, model naming differences, and inconsistent function calling. Scale from one-liner scripts to production systems without rewriting integration code.
🚀 Core Value
ai-lib unifies AI provider complexity into a single, ergonomic Rust interface:
- Universal API: Chat, multimodal, and function calling across all providers
- Multimodal Content: Easy image and audio content creation with
Content::from_image_file()andContent::from_audio_file() - Unified Streaming: Consistent SSE/JSONL parsing with real-time deltas
- Reliability: Built-in retry, timeout, circuit breaker, and error classification
- Flexible Configuration: Environment variables, builder pattern, or explicit overrides
- Production Ready: Connection pooling, proxy support, observability hooks
Result: Focus on your product logic while ai-lib handles provider integration friction.
Import guidance: In application code, prefer
use ai_lib::prelude::*;for a minimal set of common items. Library authors may use explicit imports by domain. See the module tree and import patterns guide:docs/MODULE_TREE_AND_IMPORTS.md.
⚙️ Quick Start
Installation
Basic installation (core features):
[]
= "0.4.0"
= { = "1", = ["full"] }
= "0.3"
For streaming support, enable the streaming feature:
[]
= { = "0.4.0", = ["streaming"] }
= { = "1", = ["full"] }
= "0.3"
For full functionality (streaming, resilience, routing):
[]
= { = "0.4.0", = ["all"] }
= { = "1", = ["full"] }
= "0.3"
Simple Usage
use *;
async
Standard Usage
// Application code can also use the prelude for minimal imports
use *;
async
Streaming
Note: Streaming requires the
streamingfeature (orallfeature) to be enabled.
use *;
use StreamExt;
async
🧠 Core Concepts
| Concept | Purpose |
|---|---|
| Provider | Enumerates all supported AI providers |
| AiClient | Main entry point with unified interface |
| ChatCompletionRequest | Standardized request payload |
| Message / Content | Text, image, audio content types |
| Streaming Event | Provider-standardized delta streams |
| ConnectionOptions | Runtime configuration overrides |
| Metrics Trait | Custom observability integration |
| Transport | Injectable HTTP + streaming layer |
| Usage / UsageStatus | Response-level usage metadata (tokens + status). Import from ai_lib::Usage or ai_lib::types::response::Usage |
💡 Key Features
Core Capabilities
- Unified Provider Abstraction: Single API across all providers
- Universal Streaming: Consistent SSE/JSONL parsing with real-time deltas
- Multimodal Support: Text, image, and audio content handling
- Function Calling: Consistent tool patterns and OpenAI compatibility
- Batch Processing: Sequential and concurrent processing strategies
Reliability & Production
- Built-in Resilience: Retry with exponential backoff, circuit breakers
- Strategy Builders:
AiClientBuilder::with_round_robin_chain/with_failover_chaincompose routing before runtime - Error Classification: Distinguish transient vs permanent failures
- Connection Management: Pooling, timeouts, proxy support
- Observability: Pluggable metrics and tracing integration
- Security: No sensitive content logging by default
🌍 Supported Providers
17+ providers and growing - We continuously add new AI platforms to support the evolving ecosystem.
| Provider | Streaming | Highlights |
|---|---|---|
| Groq | ✅ | Ultra-low latency inference |
| OpenAI | ✅ | GPT models, function calling |
| Anthropic | ✅ | Claude models, high quality |
| Google Gemini | ✅ | Multimodal capabilities |
| Mistral | ✅ | European models |
| Cohere | ✅ | RAG-optimized |
| HuggingFace | ✅ | Open source models |
| TogetherAI | ✅ | Cost-effective inference |
| OpenRouter | ✅ | Unified gateway, multi-provider model routing |
| Replicate | ✅ | Hosted OSS models gateway |
| DeepSeek | ✅ | Reasoning models |
| Qwen | ✅ | Chinese ecosystem |
| Baidu ERNIE | ✅ | Enterprise China |
| Tencent Hunyuan | ✅ | Cloud integration |
| iFlytek Spark | ✅ | Voice + multimodal |
| Moonshot Kimi | ✅ | Long context |
| Azure OpenAI | ✅ | Enterprise compliance |
| Ollama | ✅ | Local/air-gapped |
| xAI Grok | ✅ | Real-time oriented |
| Perplexity | ✅ | Search-augmented chat |
| AI21 | ✅ | Jurassic models |
| ZhipuAI (GLM) | ✅ | China GLM series |
| MiniMax | ✅ | China multimodal |
See examples/ for provider-specific usage patterns.
Gateway Providers
ai-lib supports gateway providers like OpenRouter and Replicate that provide unified access to multiple AI models. Gateway platforms use provider/model format for model naming (e.g., openai/gpt-4o), while direct providers use original model names (e.g., gpt-4o).
🔑 Configuration
Environment Variables
# API Keys (convention-based)
# Optional: Custom endpoints
# Optional: Proxy and timeouts
# Optional: Connection pooling (enabled by default)
# Optional: Override default models per provider
Model Selection & Fallbacks
- Auto defaults: Pass
"auto"(case-insensitive) or an empty string as the model when constructingChatCompletionRequestand ai-lib will inject the provider’s preferred model (or yourAiClientBuilder::with_default_chat_modeloverride). - Env overrides: Set
*_MODELenvironment variables (e.g.GROQ_MODEL,OPENAI_MODEL) to change defaults without touching code. These overrides feed the newModelResolverand apply across builders, streaming, and batch flows. - Invalid-model recovery: When a provider rejects a request with
invalid_model/model_not_found, ai-lib now retries with documented fallbacks and surfaces actionable hints (including links such as Groq Models) in the returnedAiLibError::ModelNotFound. - Per-provider context: Call
client.default_chat_model()to inspect the effective model string that will be used for the next request—handy when composing multi-provider strategies like failover chains.
Programmatic Configuration
use ;
use Duration;
let client = with_options?;
🔌 Bring Your Own Provider
Use CustomProviderBuilder + AiClientBuilder::with_strategy to plug in
OpenAI-compatible gateways (self-hosted or vendor previews) without editing the
Provider enum. See examples/custom_provider_injection.rs for a full demo.
use ;
let labs_gateway = new
.with_base_url
.with_api_key_env
.with_default_chat_model
.build_provider?;
let client = new // Enum ignored when strategy is provided
.with_strategy
.build?;
let resp = client
.chat_completion
.await?;
println!;
Per-provider Builders
Prefer builder wrappers (e.g., GroqBuilder, OpenAiBuilder) when you want clearer configuration or when composing strategies.
use GroqBuilder;
let client = new
.with_base_url
.with_proxy
.build?; // returns AiClient
Concurrency Control
use ;
let client = new
.with_max_concurrency
.for_production
.build?;
🔁 Routing & Failover (OSS)
Use with_failover_chain or with_round_robin_chain to wire strategies before the client sends requests.
use ;
let client = new
.with_failover_chain?
.build?;
Combine with with_round_robin_chain or RoutingStrategyBuilder for weighted/round-robin routing. Strategy composition now happens during client construction—no sentinel models or runtime branching required.
🛡️ Reliability & Resilience
| Feature | Description |
|---|---|
| Retry Logic | Exponential backoff with intelligent error classification |
| Error Handling | Distinguish transient vs permanent failures |
| Timeouts | Configurable per-request and global timeouts |
| Proxy Support | Global, per-connection, or disabled proxy handling |
| Connection Pooling | Tunable pool size and connection lifecycle |
| Health Checks | Endpoint monitoring and policy-based routing |
| Fallback Strategies | Multi-provider arrays and manual failover |
📊 Observability & Metrics
Custom Metrics Integration
;
let client = new_with_metrics?;
Usage Tracking
match response.usage_status
Migration: Usage/UsageStatus are defined in ai_lib::types::response and re-exported at the root. Old imports from types::common are deprecated and will be removed before 1.0.
Optional Features
By default, ai-lib ships with a minimal feature set. Enable features as needed:
| Feature | Description | Alias |
|---|---|---|
unified_sse |
Common SSE parser for streaming | streaming |
interceptors |
Retry, timeout, circuit breaker pipeline | resilience |
unified_transport |
Shared HTTP client factory | transport |
config_hot_reload |
Config hot-reload traits | hot_reload |
cost_metrics |
Basic cost accounting via environment variables | - |
routing_mvp |
Model selection and routing capabilities | - |
observability |
Tracer and AuditSink interfaces | - |
all |
Enable all features above | - |
Recommended for most applications:
= { = "0.4.0", = ["streaming", "resilience"] }
🗂️ Examples
| Category | Examples |
|---|---|
| Getting Started | quickstart, basic_usage, builder_pattern |
| Configuration | explicit_config, proxy_example, custom_transport_config |
| Streaming | test_streaming, cohere_stream |
| Reliability | custom_transport, resilience_example |
| Multi-Provider | config_driven_example, model_override_demo, custom_provider_injection, routing_modelarray |
| Model Management | model_management, routing_modelarray |
| Batch Processing | batch_processing |
| Function Calling | function_call_openai, function_call_exec |
| Multimodal | multimodal_example |
| Advanced | architecture_progress, reasoning_best_practices |
📄 License
Dual-licensed under MIT or Apache License 2.0 - choose what works best for your project.
🤝 Contributing
- Fork & clone repository
- Create feature branch:
git checkout -b feature/your-feature - Run tests:
cargo test - Add examples for new features
- Follow adapter patterns (prefer config-driven over custom)
- Open PR with rationale + benchmarks (if performance impact)
We value: clarity, test coverage, minimal surface area, incremental composability.
📚 Citation