Behaviour for decision-model backends, the extension point behind
Nous.DecisionModel.
A backend turns a state and an ordered list of Nous.DecisionModel.Question
into a Nous.DecisionModel.Result. Nous ships three:
Nous.DecisionModel.Backend.SystemOne- HTTP/v1/systemone(vLLM, SGLang, llama-server, Jev, LiteLLM) and the Cloudflare Workers AI presetNous.DecisionModel.Backend.OpenAIDecisions- HTTP/v1/decisions(OpenAI, vLLM)Nous.DecisionModel.Backend.LlamaCpp- in-processLlamaCppEx.Decision
Contract
decide/4 receives the Nous.DecisionModel struct (so it can read
model and opts), the state, the validated questions and per-call opts.
By the time it is called, Nous.DecisionModel.decide/4 has already
normalised the questions and defanged every string in the state; it also
emits the telemetry events. A backend must:
- keep question order and option order on the wire
- map answers back through the declared ids and option keys, never by
creating atoms from the response, and return
{:error, %Nous.Errors.DecisionModelError{reason: :unknown_label}}when the response names an id, option or level that was not declared - return every answer in question order (
Result.answers) - return
{:error, %Nous.Errors.DecisionModelError{}}for every failure; never answer a failed request with probabilities
HTTP backends go through Nous.Providers.HTTP.post/4 and validate their
URL with Nous.UrlGuard.
Optional callbacks
max_state_tokens/1- the token budget a request (state plus questions) must fit in. Without it,Nous.DecisionModel.max_state_tokens/1returns a default.count_tokens/2- exact token count for a text. Without it,Nous.DecisionModel.count_tokens/2assumes one token per byte.child_spec/1- for backends that own a process (a loaded model).
Example
defmodule MyApp.KeywordBackend do
@behaviour Nous.DecisionModel.Backend
alias Nous.DecisionModel.{Answer, Result}
@impl true
def decide(dm, state, questions, _opts) do
answers =
Enum.map(questions, fn %{id: id, type: :noul} ->
p = if String.contains?(inspect(state), "ignore previous"), do: 0.99, else: 0.01
{id, %Answer{type: :noul, noul: p, probabilities: [{"true", p}, {"false", 1 - p}]}}
end)
{:ok, %Result{answers: answers, backend: __MODULE__, model: dm.model}}
end
end
Summary
Callbacks
A child spec for backends that own a process, such as a loaded local model.
The exact number of tokens text occupies for this backend's model.
Answers questions about state.
The token budget a single request (state plus questions) must fit in.
Functions
Builds a Nous.Errors.DecisionModelError for backend.
Callbacks
@callback child_spec(keyword()) :: Supervisor.child_spec()
A child spec for backends that own a process, such as a loaded local model.
Optional. Start the child under your own supervision tree; see
Nous.DecisionModel.Backend.LlamaCpp for the options it accepts.
@callback count_tokens(Nous.DecisionModel.t(), String.t()) :: non_neg_integer()
The exact number of tokens text occupies for this backend's model.
Optional. text is already defanged. Without this callback,
Nous.DecisionModel.count_tokens/2 assumes one token per byte, an upper
bound, so chunking never relies on server-side truncation.
@callback decide( Nous.DecisionModel.t(), state :: term(), [Nous.DecisionModel.Question.t()], keyword() ) :: {:ok, Nous.DecisionModel.Result.t()} | {:error, Nous.Errors.DecisionModelError.t()}
Answers questions about state.
state is already defanged and the questions are already validated and in
declared order. opts carries per-call options such as :timeout (ms).
Returns {:ok, %Nous.DecisionModel.Result{}} with one answer per question,
in question order, keyed by the declared ids. Every failure, including a
response that names an undeclared id, option or level, is
{:error, %Nous.Errors.DecisionModelError{}}.
@callback max_state_tokens(Nous.DecisionModel.t()) :: pos_integer()
The token budget a single request (state plus questions) must fit in.
Optional. Nous.DecisionModel.max_state_tokens/1 prefers a
:max_state_tokens option on the struct, then this callback, then a default.
Hooks and guards size their chunks from this value.
Functions
@spec error(module(), Nous.Errors.DecisionModelError.reason(), String.t(), keyword()) :: Nous.Errors.DecisionModelError.t()
Builds a Nous.Errors.DecisionModelError for backend.
fields takes :status, :retryable?, :retry_after_ms and :details.
:retryable? defaults to true for :timeout and :backend_dead, false
otherwise.