Skip to content
VineshF1Public

About

FastAPI gateway in front of NVIDIA models: routes each call to a cheap or strong model, retries failures, caches duplicates, logs every request with simulated cost, and proves the routing with a 30-task agent exam.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Splice - LLM Gateway

FastAPI gateway in front of NVIDIA's OpenAI-compatible API: routes each call to a cheap or strong model, survives failures, logs every request, and proves the routing with a 30-task agent exam.

Cost is simulated (simulated_cost_usd); NVIDIA's free tier bills $0. Prices are in app/config.py.

The idea in one example

Imagine a homework/helpdesk chatbot. It has to answer easy factual questions and hard coding questions without paying strong-model prices for everything.

  • Easy question — "What is the capital of France?" The router sees a short, plain question, so it picks the cheap model. The gateway checks the cache, waits for a free slot in a 30-requests-per-minute line, calls NVIDIA, gets 29 input and 34 output tokens, works out $0.0000262 simulated cost, and writes one log row.
  • Hard question — "Debug this Python function, step by step" Code and "step by step" send it to the strong model. prove, analyze, compare and debug do the same.
  • NVIDIA has a bad moment A timeout or 503 is retried up to three times; a Retry-After header is obeyed; then a backup model is tried. If that fails too, the caller gets a clear 502 — and still exactly one log row.
  • Same question again The cache recognises it, skips the provider, costs 0, and marks cache_hit: true. X-Bypass-Cache: 1 forces a fresh call.
  • How do we know the router was right? The agent is a student taking a 30-question test with a calculator and a fact sheet. It calls the same gateway every step, so the test measures the router. Every answer is graded on three things — correct answer, right tools, finished in time — and all three must pass.

The awkward words are simple ideas: token bucket = a waiting line, fallback = a backup model, simulated cost = price tags for comparison, eval = the student's exam.

Interview version: "It's a smart switchboard for an AI chatbot. Simple questions go to a cheap model, hard ones to a strong model, failures are retried and redirected, repeated questions are cached, every call is logged with a simulated price, and a 30-task agent exam proves the switchboard isn't sacrificing quality for cost."

What went wrong, and what fixed it

Imagine a support bot built on this gateway. "What is the capital of France?" goes to the cheap model. "Debug this Python function, step by step" goes to the strong one. If NVIDIA times out, the gateway retries and then falls back. If someone asks the exact same question twice, the second answer comes from the cache. Every call lands in request_logs with its simulated cost.

What went wrong, in plain terms:

  • Provider hiccups — retries, a fallback model, and a rate limiter.
  • Wrong cost — NVIDIA replied with an internal alias, so we price the model we asked for, not the name it echoed back.
  • Wasted exam requests — traces are saved before scoring, so a crash resumes instead of calling the provider again.
  • The strong model answered from memory instead of using the required tool. The exam caught it; we left it as a known limit rather than hiding the result.
  • The cheap model repeated itself when it could not find a fact. The step budget stops the loop and the tool scorer marks it.
  • Keyword routing is a shortcut — a short but hard question may still go to the cheap model. A classifier is the obvious next step.

Setup and run

Python 3.11.

python -m venv .venv
.venv/Scripts/activate            # Windows
pip install -e ".[dev]"
cp .env.example .env

Fill .env:

  1. NVIDIA_API_KEY ΓÇö your NVIDIA Build key.
  2. Pick three model ids available to your key.
  3. Put them in CHEAP_MODEL, STRONG_MODEL, FALLBACK_MODEL.
  4. Add the same three ids to PRICE_TABLE in app/config.py with your simulated USD per 1M input/output tokens.

No model name is hardcoded anywhere in the code.

pip install -e ".[dev]"
cp .env.example .env       # add your key and the three model ids
python -m uvicorn app.main:app --port 8000

Architecture

flowchart TB
    app[Client app] -->|POST /v1/chat| gw[Gateway]
    gw --> route[Router: cheap or strong]:::choice
    route --> rel[Retry + fallback]:::core
    rel --> prov[ProviderClient]:::core
    prov --> nvidia[NVIDIA Build]:::outside
    gw --> cache[(Cache)]:::store
    gw --> log[(request_logs)]:::store
    exam[Agent exam: 30 tasks]:::agent --> gw

    classDef choice fill:#fff3cd,stroke:#b8860b,color:#4a3b00
    classDef core fill:#d4edda,stroke:#155724,color:#155724
    classDef store fill:#d1ecf1,stroke:#0c5460,color:#0c5460
    classDef outside fill:#f8d7da,stroke:#721c24,color:#721c24
    classDef agent fill:#e2d9f3,stroke:#4b2c83,color:#4b2c83
Loading

Results

30 questions, three setups, real NVIDIA calls, cache off:

Setup Score Simulated cost Cost per request Speed (p50)
always_cheap 66.7% $0.009391 $0.00014228 1616 ms
always_strong 60.0% $0.104970 $0.00166619 903 ms
router 63.3% $0.022773 $0.00035035 1593 ms

Router vs strong: 78% cheaper, 3.3 points better.

What we learned

  • The router saved money without giving up quality.
  • The strong model knew the answers but skipped the tools, so it lost the tool check. "Jupiter" without a lookup is a fail, even though it is right.
  • The cheap model sometimes got stuck looking for a fact it did not have, or did the maths in its head instead of using the calculator.

So the exam mostly checks whether the model follows the tool instructions.

How it picks a model

The router asks four quick questions. The first "yes" picks the strong model:

  1. Is there code?
  2. Is the question long (roughly over 1600 characters)?
  3. Does it say prove, analyze, step by step, debug or compare?
  4. Have two tool results already appeared?

Otherwise it uses the cheap model.

A task passes only when three things are right: the answer, the tools used, and the number of steps. 6 and 6.0 count as the same answer.

Why it is built this way

  • One door for the provider, so tests can use a fake instead of the real one.
  • Every request is logged, even failures, so the numbers do not lie.
  • Retry only errors that can succeed; a 400 will fail the same way every time.
  • The exam calls the gateway, so it tests the router, not the agent.
  • Save results before scoring, so a crash does not spend more requests.
  • Cost is simulated because the free tier is free.

Where it falls short

  • A right answer without the required tool still fails. That is on purpose: the exam is testing tool use.
  • Two tool calls in one message confuse the agent.
  • Keyword routing is a guess, not understanding.
  • The cache and SQLite mean one server only, and the fallback model is the same as the strong model, so there is no real backup.
  • 30 tasks is a smoke test, and all costs are simulated.

About

FastAPI gateway in front of NVIDIA models: routes each call to a cheap or strong model, retries failures, caches duplicates, logs every request with simulated cost, and proves the routing with a 30-task agent exam.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages