Skip to main content

AI Gateway - API Specification

Version: v1.0.0 | Status: Active | Last Updated: March 2026

1. Overview​

The ai_gateway module provides load balancing, automatic failover, and circuit breaking for LLM provider endpoints. Routes requests across multiple providers with configurable strategies and per-provider failure isolation.

2. Core Components​

2.1 Classes​

ClassDescription
AIGatewayMulti-provider gateway with routing, failover, and metrics tracking
ProviderDataclass configuring a single LLM provider endpoint (name, endpoint, weight, timeout)
CircuitBreakerPer-provider circuit breaker with closed/open/half-open state machine
GatewayConfigConfiguration dataclass for strategy, thresholds, and retry behaviour

2.2 Key Methods​

MethodSignatureDescription
AIGateway.complete(prompt: str) -> dictRoute a completion request to the best available provider
AIGateway.health_check() -> dictReturn health status and metrics for all providers
CircuitBreaker.call(fn: Callable, *args, **kwargs) -> AnyExecute function through circuit breaker
CircuitBreaker.is_available@property -> boolWhether the breaker allows requests

3. MCP Tools​

ToolDescription
gateway_completeRoute a completion request through the AI Gateway with load balancing
gateway_healthCheck health status of all configured providers

4. Usage Example​

from codomyrmex.ai_gateway import AIGateway, GatewayConfig, Provider

providers = [
Provider(name="openai", endpoint="https://api.openai.com/v1", weight=2.0),
Provider(name="anthropic", endpoint="https://api.anthropic.com", weight=1.0),
]
gateway = AIGateway(providers, GatewayConfig(strategy="weighted"))

result = gateway.complete("Explain circuit breakers")
print(result["provider"], result["latency_ms"])

5. Navigation​