AiRuntime

LLM gateway(self built service)

Reading
8 min read
Stack
Go · SQlite
IOverview

Overview#

AI Runtime is an LLM gateway and operations console in a single Go binary. It sits between a product and the model providers. Services send a prompt name and variables over gRPC. The runtime owns the prompt text, the choice of model, the fallback chain, validation of the answer and the cost record. It was built as the AI layer of Vatary Desk, where it is the only process that holds model keys.

Why it exists. When every service calls providers directly, keys spread across the codebase, prompts live in callers' code, and switching a model means a redeploy. Nobody can say what a feature costs or why a call failed. The runtime puts all of that in one place: one key holder, one prompt registry, one cost ledger, and one console that shows what every call did, what it cost and why.

Callers ask for a prompt by name. Which model answers, at what cost, and what happens when it fails is the runtime's job.

Go binary: gRPC service and console
1
model providers behind one API
9
JavaScript build steps in the console
0
Go test files
131
AI Runtime dashboard: requests, failures, p50/p95 latency, cost, schema-invalid and truncated answers, retries, fallbacks and cache-hit share for the last 24 hours, a low-sample failure alert, and requests and latency per hour.
AI Runtime dashboard: requests, failures, p50/p95 latency, cost, schema-invalid and truncated answers, retries, fallbacks and cache-hit share for the last 24 hours, a low-sample failure alert, and requests and latency per hour.
AI Runtime dashboard: requests, failures, p50/p95 latency, cost, schema-invalid and truncated answers, retries, fallbacks and cache-hit share for the last 24 hours, a low-sample failure alert, and requests and latency per hour.
Fig. 1 — Dashboard: requests, failures, latency, cost, schema-invalid answers, retries, fallbacks and cache hits for 24 hours, 7 or 30 days. The alert box flags a model only once it has enough requests to judge. All screenshots come from the public demo, which runs on synthetic data.
IIFeatures

Features#

Every call, logged and filterable

Each model call is a row: status, error class, provider, model, prompt, duration, tokens and cost. Filter by any of them, plus collection, user and metadata, and export the selection as JSON.

Request log: filters by provider, model, status, error class, prompt, collection, user and metadata above a table of calls with status, error, model, prompt, duration, tokens and cost.
Request log: filters by provider, model, status, error class, prompt, collection, user and metadata above a table of calls with status, error, model, prompt, duration, tokens and cost.
Request log: filters by provider, model, status, error class, prompt, collection, user and metadata above a table of calls with status, error, model, prompt, duration, tokens and cost.

One request, end to end

The request page shows which model was asked for and which one served, the experiment it was assigned to, the prompt version, the schema verdict and the cost. It also breaks the input down by source: static prompt text, each variable and the caller's payload. A person can rate the answer good or bad, with a note.

One request: provider, model, experiment assignment, prompt version, schema verdict, tokens, cost, a human quality vote, and a breakdown of where the input tokens went.
One request: provider, model, experiment assignment, prompt version, schema verdict, tokens, cost, a human quality vote, and a breakdown of where the input tokens went.
One request: provider, model, experiment assignment, prompt version, schema verdict, tokens, cost, a human quality vote, and a breakdown of where the input tokens went.

Actions: what one click cost

Every user action carries one correlation id. The console groups its calls into a timeline, a flow and a graph, with the steps around them, the critical path and the total cost. Time spent outside the runtime is shown as a gap, not guessed.

One user action as a timeline: wall time, time inside and outside the runtime, cost and tokens, then four model calls on a shared time axis with the gaps between them.
One user action as a timeline: wall time, time inside and outside the runtime, cost and tokens, then four model calls on a shared time axis with the gaps between them.
One user action as a timeline: wall time, time inside and outside the runtime, cost and tokens, then four model calls on a shared time axis with the gaps between them.

Timeline: four calls on one time axis, with the gaps outside the runtime.

Compare models and prompts

Latency by model, cost per 1k tokens by model, and cost by prompt, for any window. The page shows which product feature is expensive, then which model. Any window can be exported as JSON or sent to an AI review.

Compare models: p50/p95 latency by model, cost per 1k tokens by model coloured by provider, and cost by prompt.
Compare models: p50/p95 latency by model, cost per 1k tokens by model coloured by provider, and cost by prompt.
Compare models: p50/p95 latency by model, cost per 1k tokens by model coloured by provider, and cost by prompt.

Prompt registry

Prompts live in the runtime, not in callers' code. Each one has tags, a base model, a version that is a hash of its text, and a request count. Select several prompts to set a new model for all of them, move them into an experiment, swap one model for another everywhere, or tag them.

Prompt registry: every prompt with tags, status, base model, live experiments, active version hash, version count and 30-day request count, with bulk actions below.
Prompt registry: every prompt with tags, status, base model, live experiments, active version hash, version count and 30-day request count, with bulk actions below.
Prompt registry: every prompt with tags, status, base model, live experiments, active version hash, version count and 30-day request count, with bulk actions below.

A prompt's page holds its text with the variables highlighted, its history, and the strict JSON schema every answer is checked against.

One prompt: its active version, tags, prompt text with highlighted variables and the strict JSON schema its answers are checked against.
One prompt: its active version, tags, prompt text with highlighted variables and the strict JSON schema its answers are checked against.
One prompt: its active version, tags, prompt text with highlighted variables and the strict JSON schema its answers are checked against.

Experiments without a redeploy

A collection overrides the model for a set of prompts and takes a share of traffic, for example "Haiku for triage, 15%". Assignment is sticky per user and never edits the prompts themselves. One switch stops every experiment and sends all traffic back to the base model.

Prompt collections: live and stopped experiments with their traffic share, testers and prompts, a bar of experiment traffic and a stop-all switch.
Prompt collections: live and stopped experiments with their traffic share, testers and prompts, a bar of experiment traffic and a stop-all switch.
Prompt collections: live and stopped experiments with their traffic share, testers and prompts, a bar of experiment traffic and a stop-all switch.

Test iterations

An iteration captures whole user actions for chosen prompts over a window, together with people's thumbs-up and thumbs-down votes. An AI analysis reviews every bad case, writes one report and drafts a prompt fix. A person saves the fix, and the next iteration measures it against the previous one.

A test iteration: cases captured, votes with a confidence range, latency and cost, an AI analysis report, a proposed prompt fix, and the captured cases with their verdicts.
A test iteration: cases captured, votes with a confidence range, latency and cost, an AI analysis report, a proposed prompt fix, and the captured cases with their verdicts.
A test iteration: cases captured, votes with a confidence range, latency and cost, an AI analysis report, a proposed prompt fix, and the captured cases with their verdicts.

Playground and saved cases

Run any registered model by hand, with raw text or with a registry prompt through the real generate path: rendering, schema check and the prompt's routing. Each run is logged as a request, and it can be saved as a test case to replay later.

Model playground: a registry prompt run against Claude Sonnet with JSON input; the result shows latency, tokens, cost, a valid schema verdict and the answer.
Model playground: a registry prompt run against Claude Sonnet with JSON input; the result shows latency, tokens, cost, a valid schema verdict and the answer.
Model playground: a registry prompt run against Claude Sonnet with JSON input; the result shows latency, tokens, cost, a valid schema verdict and the answer.

Model catalog with prices

Every model is registered with its kind (chat, embedding, transcription), input and output price, context window and capabilities. Cost is computed at call time from this table, and the default model is a single click.

Model catalog: provider, kind, input and output price per million tokens, context window and capabilities (JSON, streaming, vision, tools, reasoning) for each model.
Model catalog: provider, kind, input and output price per million tokens, context window and capabilities (JSON, streaming, vision, tools, reasoning) for each model.
Model catalog: provider, kind, input and output price per million tokens, context window and capabilities (JSON, streaming, vision, tools, reasoning) for each model.

Per-user usage

Requests, failures, schema-invalid answers, cost, tokens, p95 latency and top prompts for each end user, with a link to that user's actions.

Users: requests, failures, schema-invalid answers, cost, tokens, p95 latency and top prompts per end user over the window.
Users: requests, failures, schema-invalid answers, cost, tokens, p95 latency and top prompts per end user over the window.
Users: requests, failures, schema-invalid answers, cost, tokens, p95 latency and top prompts per end user over the window.
IIITechnical

The gateway#

Every call follows the same path. The runtime renders the prompt from the registry, picks a model through the router, and falls back to the next one on 429 or 5xx errors. It then checks the answer against the prompt's strict JSON schema and records tokens, cost and latency. A caller never sees a provider key, a price list or a retry loop.

  1. Prompt name + vars
  2. Render
  3. Route
  4. Provider call
  5. Fallback?
  6. Schema check
  7. Record cost
other endings
  • Answer
  • Schema-invalid
  • Failed
  • Providers: OpenAI, Anthropic, Gemini, xAI, Mistral, DeepSeek, Groq, Moonshot and OpenRouter, behind one interface. Models are registered with their prices, so cost is computed at call time.
  • Prompt registry: prompts live in the runtime, not in callers' code. A prompt's version is a hash of its text, so every edit shows up as a step on its charts.
  • Router and experiments: the model for a prompt is a routing decision that can be changed and A/B-tested from the console without redeploying a caller.
  • Storage: calls, traces and the registry are kept in an embedded SQLite store, so the runtime is one container with no extra database.

The console#

The console is server-rendered from the same binary, with no JavaScript build. It covers requests, actions, unresolved failures, comparison between periods and models, the router, users, providers, models, prompts, collections, iterations, a playground and health.

  • Dashboard: requests, failures, p50 and p95 latency, cost, schema-invalid and truncated answers, retries, fallbacks served and prompt-cache hit share, over 24 hours, 7 days or 30 days, optionally compared with the previous period.
  • Alerts that avoid noise: a model's failure rate is judged only once it has enough requests, and a run of three or more consecutive failures is flagged on its own, because a run of that length is almost never chance.
  • Actions: every user action carries one correlation id. The console opens it as a timeline, a flow and a graph, with every model call, the steps around it, the critical path and the total cost. Time spent outside the runtime is drawn as a gap rather than guessed.
  • Analysis: any window can be exported as JSON or sent to an AI review.

The prompt contract#

Callers and the runtime share one vocabulary, prompt names, and nothing at runtime checks that they agree. One pure function compares the prompt names that callers use with the registry, in both directions. A missing prompt is an error, because it would break at request time. An unused prompt is a warning: dead weight or a half-done rename. The same check runs on fixtures, on the registry source in CI, and against the live registry.

Public demo#

The demo gives every visitor a private sandbox with synthetic traffic. Prompts can be edited, the playground run and the router changed, and nobody else sees the changes. Every model answer is canned and no request leaves the server. The sandbox is discarded at sign-out or after two idle hours.

Discussion

Loading comments…