Unified AI Gateway - Multi-provider LLM routing, observability, and evaluation platform
Project description
Ai_Gateway
A unified AI Gateway SDK for LLM routing, observability, and evaluation.
Features
- Multi-Provider Gateway: Unified API for OpenAI, Anthropic, Google Gemini with automatic fallbacks
- Full Observability: Distributed tracing, logging, cost tracking, and analytics
- Evaluation Metrics: Built-in metrics for relevance, coherence, toxicity detection
- Smart Caching: Response caching with semantic similarity matching
- Self-Hosted or Cloud: Store telemetry locally (SQLite) or in the cloud
Installation
pip install Ai_Gateway
# With all providers
pip install Ai_Gateway[all-providers]
# With semantic caching
pip install Ai_Gateway[semantic-cache]
# Everything
pip install Ai_Gateway[full]
Quick Start
Basic Usage
import aigateway
# Initialize with your API keys
aigateway.init(
openai_api_key="sk-...",
anthropic_api_key="sk-ant-...",
google_api_key="...",
)
# Use the unified gateway
response = aigateway.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)
Monitor Existing Clients
from openai import OpenAI
import aigateway
# Initialize AIGateway
aigateway.init()
# Wrap your existing client
client = OpenAI()
aigateway.monitor(client)
# All calls are now automatically traced
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello!"}]
)
Automatic Fallbacks
import aigateway
aigateway.init(
openai_api_key="sk-...",
anthropic_api_key="sk-ant-...",
fallback_providers=["anthropic"], # Fallback to Anthropic if OpenAI fails
)
# Will automatically retry with Anthropic if OpenAI fails
response = aigateway.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello!"}]
)
Tracing
import aigateway
from aigateway import trace, span
@trace(name="my_workflow")
async def process_request(query: str):
# Create spans for sub-operations
with span("retrieve_context"):
context = await retrieve_documents(query)
with span("generate_response"):
response = await generate_answer(query, context)
return response
Evaluation
import aigateway
# Evaluate LLM outputs
result = await aigateway.evaluate(
input_text="What is Python?",
output_text="Python is a programming language...",
metrics=["relevance", "coherence", "toxicity"]
)
print(f"Overall Score: {result.overall_score}")
print(f"Passed: {result.passed}")
for metric in result.metrics:
print(f" {metric.name}: {metric.score:.2f}")
Track Feedback
import aigateway
# Track user feedback on responses
aigateway.track_feedback(
trace_id="...",
value="thumbs_up",
comment="Very helpful response!"
)
Configuration
Environment Variables
AIGATEWAY_API_KEY=your-cloud-api-key
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
GOOGLE_API_KEY=...
AIGATEWAY_DEFAULT_PROVIDER=openai
AIGATEWAY_STORAGE_BACKEND=sqlite
AIGATEWAY_TRACING_ENABLED=true
AIGATEWAY_CACHE_ENABLED=true
Programmatic Configuration
import aigateway
aigateway.init(
# Provider keys
openai_api_key="sk-...",
anthropic_api_key="sk-ant-...",
# Gateway settings
default_provider="openai",
fallback_providers=["anthropic", "google"],
# Tracing
tracing_enabled=True,
sample_rate=1.0,
# Storage (sqlite, postgresql, cloud, memory)
storage_backend="sqlite",
storage_path="./aigateway.db",
# Caching
cache_enabled=True,
cache_backend="memory",
# Debug
debug=False,
)
API Reference
Gateway Client
from aigateway import AIGateway
gateway = AIGateway()
# Async completion
response = await gateway.complete(
model="gpt-4",
messages=[{"role": "user", "content": "Hello"}],
temperature=0.7,
max_tokens=1000,
)
# Streaming
async for chunk in gateway.complete_stream(
model="gpt-4",
messages=[{"role": "user", "content": "Tell me a story"}],
):
print(chunk.choices[0].delta.content, end="")
# Embeddings
embeddings = await gateway.embed(
model="text-embedding-3-small",
input="Hello, world!"
)
Tracing
from aigateway import Trace, Span, SpanKind
# Manual tracing
with Trace(name="my_request", user_id="user123") as trace:
with trace.span("step1", kind=SpanKind.LLM) as span:
span.set_input({"query": "Hello"})
result = do_llm_call()
span.set_output({"response": result})
span.set_llm_info(model="gpt-4", provider="openai")
Evaluation Metrics
from aigateway.evaluation import (
Evaluator,
RelevanceMetric,
CoherenceMetric,
ToxicityMetric,
CustomMetric,
)
# Custom evaluator
evaluator = Evaluator(metrics=[
RelevanceMetric(threshold=0.8),
CoherenceMetric(threshold=0.6),
ToxicityMetric(threshold=0.2),
])
result = await evaluator.evaluate(
input_text="...",
output_text="...",
context="...", # Optional
)
Storage Backends
from aigateway.storage import SQLiteStorage, CloudStorage, MemoryStorage
# SQLite (local)
storage = SQLiteStorage(db_path="./data.db")
await storage.initialize()
# Cloud (remote)
storage = CloudStorage(
endpoint="https://api.aigateway.io",
api_key="your-api-key"
)
await storage.initialize()
Development
# Install development dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Format code
black src tests
ruff check src tests
# Type checking
mypy src
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Links
License
MIT License - see LICENSE for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aigateway_sdk-0.1.0.tar.gz.
File metadata
- Download URL: aigateway_sdk-0.1.0.tar.gz
- Upload date:
- Size: 169.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5cf628d574cc936ceb15d14e921b77a48a136a67609a745643af0b6f7dfca4e8
|
|
| MD5 |
1c099d003ccd44aebc86fb55180b1be6
|
|
| BLAKE2b-256 |
c87ee6768f3251c6b222e2b2991905101f1856c32b7221280898eded546723ca
|
Provenance
The following attestation bundles were made for aigateway_sdk-0.1.0.tar.gz:
Publisher:
publish.yml on UditMishra-Webby/Aigate
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aigateway_sdk-0.1.0.tar.gz -
Subject digest:
5cf628d574cc936ceb15d14e921b77a48a136a67609a745643af0b6f7dfca4e8 - Sigstore transparency entry: 1225722699
- Sigstore integration time:
-
Permalink:
UditMishra-Webby/Aigate@f40e99c350bb8ed565438d42222445e069611a57 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/UditMishra-Webby
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f40e99c350bb8ed565438d42222445e069611a57 -
Trigger Event:
release
-
Statement type:
File details
Details for the file aigateway_sdk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: aigateway_sdk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 200.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa6d3240d84402fc8438d88f565937787f9ee16bf2f1237cf2109e9a6ef9e7f7
|
|
| MD5 |
f27b3b93096b5b272dc31d18346add97
|
|
| BLAKE2b-256 |
e7d619c34e174eafc02810e28e0dec670fed677099c4b8095131f0990fca450e
|
Provenance
The following attestation bundles were made for aigateway_sdk-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on UditMishra-Webby/Aigate
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aigateway_sdk-0.1.0-py3-none-any.whl -
Subject digest:
aa6d3240d84402fc8438d88f565937787f9ee16bf2f1237cf2109e9a6ef9e7f7 - Sigstore transparency entry: 1225722788
- Sigstore integration time:
-
Permalink:
UditMishra-Webby/Aigate@f40e99c350bb8ed565438d42222445e069611a57 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/UditMishra-Webby
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f40e99c350bb8ed565438d42222445e069611a57 -
Trigger Event:
release
-
Statement type: