> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dari.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Embed The Runtime

> Compose the gateway runtime with your own storage, accounting, and credentials through GatewayPlatform.

The gateway is a request handler. Run it as the self-hosted server, or compose it into your own service with a `GatewayPlatform` that supplies storage, accounting, and credentials. The reference platform is `src/self_host/` in the [gateway repository](https://github.com/mupt-ai/dari).

## Serve The Handler

The root entry point uses only web-standard `fetch`, `Request`, and `Response`, so any runtime can serve it:

```ts theme={null}
import { createPrioritySelectorBackend, createRequestHandler } from "@mupt-ai/dari-gateway";

const handler = createRequestHandler({ platform, selectorBackend: createPrioritySelectorBackend() });
Bun.serve({ port: 8080, fetch: handler, idleTimeout: 0 });
```

Set `idleTimeout: 0` so long streams stay open while a model thinks. The Bun entry point (`@mupt-ai/dari-gateway/bun`) adds `startServer` for the same setup, and `startSelfHostedGateway` for the full self-hosted server with SQLite.

## The Platform

A `GatewayPlatform` supplies:

* **Router resolution.** Turns a router id and the caller's gateway key into models, thinking levels, and settings.
* **Conversation state.** Reads and writes the prefix state that keeps a conversation on one model and its provider caches warm.
* **Session pins.** Reads and writes pins for routers with `session_pinning: true`, keyed by router and an opaque pin key.
* **Subscription holds.** Usage-limit holds on connected plans, so a held plan is skipped until its reset.
* **Request records.** Usage for each request: input, output, cache-read, and cache-write tokens, and `total_cost_usd`. Failed and aborted requests are recorded too.
* **Error reporting.** Request and operational errors.

## Credentials

Implement `resolveProviderCredential` to supply a per-request credential in place of the router's own key. Return `{ apiKey, baseUrl?, headers?, subscription? }`:

* Pass a tenant's own key for that request.
* Set `subscription` to `"openai_codex"`, `"anthropic_claude_code"`, or `"meta_muse"` to serve from a plan. The `apiKey` is then that plan's access token, and the platform keeps it fresh.

A Claude Code plan serves only Claude Code clients, and a Muse Code plan only Muse Code clients, recognized by their `x-client-id`.

## Strict Routers

A router resolved with `strict: true` serves only the requested `dari/model/<id>`, at the requested thinking level. It uses no selector, lease, or personal subscription, and it refuses anything else instead of substituting a model.

Set `strict_providers: ["openai"]` on the resolved router to allow any bundled-catalog model from that provider without listing each one. Use the full form, such as `dari/model/openai/gpt-6.2-sol`. Bare ids and models outside the catalog are still refused. The grant applies only to strict routers.

## Session Pinning

With `session_pinning: true`, the runtime routes each session once and serves that model and level for the rest of the session. Each subagent is routed once on its own, identified by a Claude Code agent id, a Codex subagent thread, or an OpenCode child session. Background requests and `dari/model/<id>` requests never use pins.

## Cost Attribution

A client can tag requests with `X-Dari-Tags: run=r1,trial=3`. The runtime parses the tags into the request record's `tags` and into the `recordUsage` payload, so you can group spend by run or experiment.

## Details

> [accordion] Request record settings
>
> Each request record also carries the router's non-secret settings, such as enabled models, thinking levels, and the strict, speculative-routing, and session-pinning flags.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.