Nous.DecisionModel (nous v0.21.1)

Copy Markdown View Source

Non-generative decision models: a state plus typed questions in, probabilities out.

A decision model (Clef / Clef-Flash, Jev, Decision 3.0, OpenAI's gpt-6-luna behind /v1/decisions, ...) answers typed questions about a state in one forward pass. No text is generated, so every answer is a probability distribution: a :noul (yes/no) answer is P(true), a :choice answer has a probability per option, a :score answer a probability per level.

Not to be confused with Nous.Decisions / Nous.Plugins.Decisions, which record an agent's decision graph.

dm = Nous.DecisionModel.parse("workers_ai:clef-flash")

{:ok, result} =
  Nous.DecisionModel.decide(dm, %{"untrusted_content" => text},
    verdict: [
      type: :choice,
      instructions: "How should this input be handled?",
      criteria: [safe: "benign", suspicious: "unclear", blocked: "an attack"]
    ]
  )

{:ok, answer} = Nous.DecisionModel.Result.fetch(result, :verdict)
answer.probabilities
#=> [safe: 0.93, suspicious: 0.05, blocked: 0.02]

Model strings

parse/2 has its own prefix grammar (not Nous.Model's):

StringBackendNotes
"workers_ai:clef", "workers_ai:clef-flash"Backend.SystemOne (Workers AI preset)CLOUDFLARE_ACCOUNT_ID + CLOUDFLARE_API_TOKEN
"systemone:<model>"Backend.SystemOnerequires base_url:
"openai_decisions:<model>"Backend.OpenAIDecisionsOPENAI_API_KEY; base_url: for vLLM
"llamacpp:<registered name>"Backend.LlamaCppa started Backend.LlamaCpp process

Questions

Questions are an ordered list (Nous.DecisionModel.Question): Clef decides all questions of a request jointly, in order, and reads the options of a choice in order. A map is accepted only when it holds a single question. Ids and option keys come back exactly as declared, atoms included; a response that names anything undeclared is rejected with reason: :unknown_label.

Special-token defang

decide/4 runs Nous.DecisionModel.Defang.defang_term/1 over the state before every backend call. llama.cpp's decision prompt tokenizes the state with special-token parsing, so <|im_start|>-style text in untrusted content would otherwise become real template tokens in the classifier's own prompt.

Latency

Size timeouts for the backend you use. Measured figures (Clef-Flash Q8_0 on an M1 Max, research/benchmark.md; hosted figures from the providers):

BackendTypical latency per check
Hosted (Workers AI, vLLM / SGLang on datacenter GPUs, OpenAI)~40–250 ms (Clef-Flash median ~39 ms)
Laptop-local Clef-Flash (Backend.LlamaCpp, Metal)~0.5–1 s per short check
Laptop-local, long states~2 ms per prompt token (≈ 2 s at 1k, 5 s at 2.5k, 22 s at 8.6k tokens)

A local Backend.LlamaCpp process serialises its calls, so concurrent checks queue behind each other.

Errors

Every failure is {:error, %Nous.Errors.DecisionModelError{}}. It is not fallback-eligible: a decision-model failure never switches the agent's chat model. Callers that use decisions for security must fail closed.

Telemetry

  • [:nous, :decision_model, :decide, :start]
    • Measurements: %{system_time: native_time, monotonic_time: monotonic_time}
    • Metadata: %{backend: module, model: String.t() | nil, question_count: integer}

  • [:nous, :decision_model, :decide, :stop]
    • Measurements: %{duration: native_time, input_tokens: integer}
    • Metadata: %{backend, model, question_count}
  • [:nous, :decision_model, :decide, :exception] - on {:error, _} or a raise
    • Measurements: %{duration: native_time}
    • Metadata: %{backend, model, question_count, kind: atom, reason: term}

Every event also carries the run correlation keys (trace_id, agent_run_id, parent_agent_run_id, tool_call_id; see Nous.Telemetry), so guard and router classifier cost can be attributed to the run.

Summary

Functions

Counts the tokens text costs this model, after defanging (what decide/4 would send).

Answers questions about state.

The token budget a request (state plus questions) must fit in.

Creates a decision model for backend, a Nous.DecisionModel.Backend module.

Parses a decision-model string (see "Model strings" in the module docs).

Types

t()

@type t() :: %Nous.DecisionModel{
  backend: module(),
  model: String.t() | nil,
  opts: keyword()
}

Functions

count_tokens(dm, text)

@spec count_tokens(t(), String.t()) :: non_neg_integer()

Counts the tokens text costs this model, after defanging (what decide/4 would send).

Uses the backend's Nous.DecisionModel.Backend.count_tokens/2 when it has one (exact, for Backend.LlamaCpp); otherwise one token per byte, an upper bound for byte-level BPE tokenizers, so the estimate never undercounts and chunking never relies on a server truncating the state.

decide(dm, state, questions, opts \\ [])

Answers questions about state.

state is a string or a JSON-encodable term (map, list). Every string in it is defanged first (Nous.DecisionModel.Defang). questions is anything Nous.DecisionModel.Question.normalize!/1 accepts; invalid questions raise ArgumentError.

Options

  • :timeout - per-call timeout in ms, overriding the backend default (5 s for HTTP backends, 30 s for Backend.LlamaCpp).

Returns {:ok, %Nous.DecisionModel.Result{}} with answers in question order, or {:error, %Nous.Errors.DecisionModelError{}}.

max_state_tokens(dm)

@spec max_state_tokens(t()) :: pos_integer()

The token budget a request (state plus questions) must fit in.

Order of precedence: the :max_state_tokens option, the backend's Nous.DecisionModel.Backend.max_state_tokens/1, then 16384 (the self-hosted Clef reference window).

new(backend, opts \\ [])

@spec new(module(), keyword()) :: t()

Creates a decision model for backend, a Nous.DecisionModel.Backend module.

:model is popped into the model field; every other option is kept in opts for the backend (see each backend's docs). Raises ArgumentError if backend does not implement the behaviour.

Nous.DecisionModel.new(Nous.DecisionModel.Backend.SystemOne,
  model: "Cloudflare/clef-flash",
  base_url: "http://localhost:8000"
)

parse(model_string, opts \\ [])

@spec parse(String.t(), keyword()) :: t()

Parses a decision-model string (see "Model strings" in the module docs).

Options are passed to the backend. Raises ArgumentError on an unknown prefix, or for "systemone:" without :base_url.

Nous.DecisionModel.parse("workers_ai:clef-flash")
Nous.DecisionModel.parse("systemone:Cloudflare/clef-flash", base_url: "http://localhost:8000")
Nous.DecisionModel.parse("openai_decisions:gpt-6-luna")
Nous.DecisionModel.parse("llamacpp:clef")