← All Projects

AI Gateway in Rust

A Rust gateway for applications that use several AI providers through one API. The application sends a request in the OpenAI or Anthropic format; the gateway translates it for the target provider and selects an account with available quota.

What the application sends

The application keeps an OpenAI- or Anthropic-compatible interface instead of integrating every provider separately.

Where the gateway runs it

The requested model, account access, live quota, and assigned network route are checked together. An unavailable model is reported, not silently replaced.

When conditions change

Quota loss, network-route failure, and service-version change are handled separately. The session keeps its context, and an open stream finishes on the version that started it.

How a request crosses the gateway

No prompts, accounts, credentials, private traffic, or proprietary source are shown.

  1. Application APIThe application uses an OpenAI- or Anthropic-compatible request instead of a provider-specific integration.
  2. Provider adapterModels, messages, tools, and streaming events are mapped to the selected provider's protocol.
  3. Where it can runModel support, account access, quota, and the assigned network path are checked together.
  4. Session continuationSession context stays intact while quota, network, and service-version failures follow separate recovery paths.
01

One API instead of several integrations

The application uses it like an ordinary API: send a familiar request and receive the response through the same interface. Inside, the gateway chooses the provider and account, translates the request, and keeps the context when conditions change.

Provider-specific formats, account access, and live quota stay inside the Rust gateway instead of spreading across every application integration.

02

The path of one request

  • One application entrypoint: The application sends a familiar OpenAI or Anthropic request without rebuilding its integration for every provider.
  • Request translation: Messages, tools, thinking fields, and streaming events are mapped to the provider's actual protocol instead of pretending every API behaves the same way.
  • Where the request can run: The requested model, account entitlement, live quota, and assigned network path are checked before work starts. An unavailable model is reported instead of silently replaced.
  • The session continues: Session context stays attached across requests and restarts. Tool history stays with the same continuation instead of becoming a new conversation.
  • Long response and version change: Elapsed time alone cannot end an active generation. A new service version must complete a real model response before new traffic moves to it, while existing connections finish where they started.
03

What the application gets

OpenAI and Anthropic APIs
One entrypoint
model, access, and live quota
Execution choice
persistent session affinity
Continuation
04

Why a reverse proxy is not enough for this job

Problem
Blind forwarding or round-robin selection can send a request to an account that cannot run the model, discard a bound session, and hide whether quota, account state, the provider protocol, or the network actually failed.
Solution
The gateway checks where the request can run, translates it for that provider, and keeps session context separate from the failure. Recovery changes only the failed part instead of breaking the whole integration.
Why not that option
A separate thin proxy for every provider is simpler at first, but duplicates client integration and leaves account, quota, and continuation state split across services.
05

What remains visible for each request

Selected route
The requested and effective model, provider adapter, account capacity, and the reason a route may be unavailable.
Request history
First output, duration, final completion, and recovery reason in the sanitized view, without exposing private prompt content.
Failure class
Quota, account, network, provider rejection, and a long model wait remain different events with different next actions.
Working version
A candidate must finish a real response on the requested model before new work moves to it; older streams keep running.
06

Implementation

Gateway and adapters
Rust, Axum, Tokio, OpenAI and Anthropic APIs
State and diagnostics
PostgreSQL, SQLx, Prometheus, Grafana
Interface and delivery
Leptos, NixOS, systemd, nginx