All projects

Project case study

LLMProxy

Route model requests and account for every attempt

A self-hosted LLM gateway in C# and .NET 10, exposing OpenAI-compatible APIs across ten provider adapters with key pools, quotas, usage accounting, and an embedded admin console.

  • C#
  • .NET 10
  • PostgreSQL
  • Redis
Project notesTests exercise key failover, stream replay boundaries, and concurrent quota reservations.

The problem

I built LLMProxy to put provider credentials, routing, quotas, and usage accounting behind one API. Clients send a gateway key and a public model alias. The gateway chooses a compatible provider, checks the limits, and records each attempt.

How it fits together

  1. 01

    Authenticate

    Validate the gateway key, permitted model alias, and request limits.

  2. 02

    Plan the route

    Filter targets by capabilities and order the eligible providers.

  3. 03

    Reserve capacity

    Acquire concurrency slots and reserve token and spending allowances.

  4. 04

    Invoke and settle

    Translate the provider response, record attempts, and settle usage.

Provider adapters handle wire protocols while the application layer owns routing and accounting. PostgreSQL and Redis support multiple gateway replicas; standalone deployments use SQLite and local admission controls. The embedded admin console manages keys, policies, and usage.

Decisions & tradeoffs

Keep provider differences behind adapters

I put provider protocols behind adapters and expose public model aliases to clients. The router checks the requested features before choosing by priority, round-robin, cost, or latency. Tools, media inputs, and response formats still depend on what the chosen adapter and model support.

Stop fallback once streaming output begins

Provider accounts can rotate keys, cool down rejected credentials, and fall back after eligible failures. I stop fallback after the first streaming event reaches the client: a later failure ends the stream rather than replaying it. Attempts before that boundary can still incur separate provider charges.

Reserve allowances before dispatch

I reserve tokens and spending with a conditional database update before dispatch. Completion settles usage and the reservation in one transaction, and repeating finalization does not charge the request twice. Interrupted requests or missing usage need conservative estimates; provider invoice records can later correct spending.

Tests and limitations

The tests cover provider fallback, key-pool failover, and stream failures on either side of the first event. API checks verify that failover records both credential fingerprints but settles one gateway request, and that an error after streaming starts never retries another key.

Persistence scenarios contend for shared allowances and verify both counters and stored reservations. SQLite and PostgreSQL scenarios check that duplicate finalizers commit usage once; Redis tests exercise shared admission across gateway instances.

Repository CI passed for the pinned revision. Provider calls in the tests use deterministic fakes or mock endpoints; the PostgreSQL and Redis scenarios require configured services. This is not a live-provider benchmark or a verified production workload.

Exercise routing and accounting

With the .NET 10 SDK installed, run these commands from a repository checkout for unit, provider, and SQLite integration checks. Set LLMPROXY_TEST_POSTGRES and LLMPROXY_TEST_REDIS to include the PostgreSQL and Redis scenarios.

dotnet restore --locked-mode
dotnet build --configuration Release --no-restore -m:1
dotnet test --configuration Release --no-build --no-restore