> ## 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.

# Policies

> Choose an eligible model with a local function, selector, or hosted service.

A policy receives the normalized request and the candidates that survived capability filtering. It returns a model ID and may choose a reasoning effort.

## A local policy

```ts theme={null}
import type { RoutingPolicy } from "@mupt-ai/dari-router";

const policy: RoutingPolicy = ({ request, candidates }) => {
  const wantsTools = request.tools.length > 0;
  const preferred = wantsTools ? "anthropic/claude-fable-5-1" : "openai/gpt-6-luna";
  const selected = candidates.find((candidate) => candidate.id === preferred)
    ?? candidates[0]!;

  return {
    model: selected.id,
    reasoningEffort: selected.defaultReasoningEffort,
    reason: wantsTools ? "This candidate supports the requested tools." : "Use the fast candidate.",
  };
};
```

Policies may be synchronous or asynchronous. Router Core validates the returned model and reasoning effort. Omit `reasoningEffort` to use the candidate's default. The TypeScript field is `reasoningEffort`; wire JSON uses `reasoning_effort`.

## Inspect or serve a selection

`router.evaluatePolicy(request)` is stateless: it runs the policy without reading or writing leases and without firing hooks. Use it for previews, logs, or tests.

`router.select(request)` is the authoritative serving primitive. It reads an existing lease, applies a new lease returned by the policy, and fires `onSelection`. `router.fetch()` uses the same semantics.

```ts theme={null}
import { openAIChatRequest } from "@mupt-ai/dari-router/protocols";

const request = openAIChatRequest({
  model: "my-router",
  messages: [{ role: "user", content: "Explain cache locality." }],
});

const preview = await router.evaluatePolicy(request);
const servingSelection = await router.select(request);
console.log(preview.decision, servingSelection.decision);
```

## Self-hosted selector policy

The advanced `createDariRoutingPolicy` uses a selector model (an LLM that answers routing questions) and the deterministic policy engine to assemble evidence, ask the selector, and validate its answer. It is public from `/policy-engine` because it is more opinionated than the core policy interface.

```ts theme={null}
import { createRouter, type RouterExecutor } from "@mupt-ai/dari-router";
import { createDariRoutingPolicy } from "@mupt-ai/dari-router/policy-engine";

declare const executor: RouterExecutor; // your provider call, see Custom Executors
declare function askSelectorModel(request: unknown, signal: AbortSignal): Promise<string>;

const policy = createDariRoutingPolicy({
  selector: askSelectorModel, // calls your selector model and returns its JSON answer
  selectorModel: "openai/gpt-6-luna",
  selectorContextWindowChars: 400_000,
  pricing: (model) => myPricing[model] ?? null,
  averageOutputTokensByModel: myAverageOutputTokensByModel,
});

const router = createRouter({
  models: [
    { id: "openai/gpt-6-luna", executor: "provider" },
    { id: "openai/gpt-6.1-sol", executor: "provider" },
  ],
  policy,
  executors: { provider: executor },
});
```

`selector` is the only place the selector model is called. It is an option of `createDariRoutingPolicy`, not of `createRouter`.

For self-hosted `createDariRoutingPolicy`, provide your own pricing and observed output-token averages. Router Core does not ship or infer them. Cache state and model metadata remain optional; unknown cache state means the engine makes no cache-savings claim.

`evals` also accepts a callback, so a large benchmark pool can be filtered per request. `selectRelevantEvals` asks [TypeSafe's Jev](https://docs.typesafe.ai/introduction) one yes/no question per card (does this benchmark measure the kind of work the agent is doing?) and keeps the cards whose relevance probability exceeds `JEV_EVAL_RELEVANCE_THRESHOLD` (0.3). This is the same filter the Managed Router applies.

```ts theme={null}
import { resolveJevConfig, selectRelevantEvals } from "@mupt-ai/dari-router/policy-engine";

const jev = resolveJevConfig({ apiKey: process.env.TYPESAFE_API_KEY! });
const policy = createDariRoutingPolicy({
  // ...
  evals: async (input) => {
    const conversation = input.request.items.flatMap((item) =>
      item.type === "message"
        ? [{
            role: item.role,
            content: item.content.flatMap((part) =>
              part.type === "text" ? [{ type: "text" as const, text: part.text }] : []
            ),
          }]
        : []
    );
    const { evals } = await selectRelevantEvals(
      allBenchmarkCards,
      { task: conversation.find((message) => message.role === "user") ?? null, conversation },
      jev,
      input.signal,
    );
    return evals;
  },
});
```

## Custom rules

```ts theme={null}
const policy = createDariRoutingPolicy({
  selector: askSelectorModel,
  selectorModel: "openai/gpt-6-luna",
  pricing: (model) => myPricing[model] ?? null,
  averageOutputTokensByModel: myAverageOutputTokensByModel,
  strategy: "custom",
  customConfig: {
    rules: [
      { when: "architecture or difficult debugging", use: "openai/gpt-6.1-sol", thinking_level: "high" },
      { when: "simple factual questions", use: "openai/gpt-6-luna" },
    ],
    default: "openai/gpt-6-luna",
  },
});
```

Custom rules restrict the candidates to the rule and default targets, and the selector request is the same one every router uses with a short rules section appended to its system prompt. The selector must return an eligible model and effort. For a fully local deterministic policy, write a `RoutingPolicy` directly; for full preparation/finalization control, use the [advanced policy engine](https://docs.dari.dev/framework/decision-pipeline.md).


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