Orbyt

A self-hosted LLM API gateway that centralizes access, handles rate limiting via Key Pools, and absorbs provider instability.

Infrastructure · 2024 · Backend Engineer
02

The Problem

Integrating multiple AI models directly into an application creates a maintenance nightmare. Each provider has different SDKs, different streaming chunk formats, and fragile uptime.

More critically, a single API key quickly hits rate limits under load. The application needed a structural layer to pool keys, handle timeouts, and abstract routing away from the client entirely.

03

How It Works

Rendering architecture...
04

System Architecture

Rendering architecture...
05

Engineering Problems

API Key Pooling & Rate Limits

Problem

A single provider key has a strict request quota. Under load, routing all traffic through one key becomes the primary bottleneck, causing cascading request failures.

Solution

Implemented a Redis-backed key pool. When a request arrives, the gateway acquires a distributed lock on an available key for the target provider, executes the request, and releases the key back to the pool.

Why this approach: Requires managing distributed state via Redis, but guarantees we never exceed the concurrency limit of a single API key.

Provider Failure Handling

Problem

If an AI provider experiences downtime or a latency spike, the client application traditionally hangs or crashes.

Solution

Built a structural fallback router. If the primary model times out or returns a 5xx error, the gateway intercepts the failure and immediately reroutes the exact same payload to a secondary model.

06

Deep Dive: Unified Streaming Lifecycle

Rendering architecture...
01

The gateway establishes an SSE connection with the client immediately.

02

It opens a downstream connection to the target provider.

03

As chunks arrive in arbitrary provider formats, the gateway normalizes them in-memory.

04

The client only ever receives a single, predictable streaming format, regardless of the underlying model.

07

Key Management Strategy

ApproachDrawbackResult
Single API KeyHits rate limits quickly.Unreliable under load.
Random Key SelectionRisk of selecting an exhausted key.Inconsistent latency.
Redis Lock PoolingRequires external infrastructure.Maximum throughput & safe concurrency.
Chosen: Redis Lock PoolingCentralized allocation ensures we never violate strict provider rate limits while maximizing overall system throughput.
08

Results

  • Centralized provider routing completely removes complex SDK integrations from the client codebase.
  • Redis pooling prevents artificial bottlenecks and safely scales parallel requests.
  • Fallback mechanics mask upstream provider outages from end users entirely.
09

Tech Stack

Node.jsExpressTypeScriptRedisPostgreSQLPrisma
10

Learnings

Abstract complexity at the edge.

Pushing logic like fallbacks, retries, and formatting into a gateway makes the consuming client dramatically simpler and more resilient.

Distributed locks are critical for shared resources.

Without a centralized state manager like Redis, parallel requests will inevitably overlap and violate third-party API quotas.

Explore the source code.