ExAgent.Classifier behaviour (ExAgent v3.0.0)

View Source

A bounded choice among host-defined labels. Use a stock Decisions model, a host-owned Nx serving, or a custom adapter.

Stability — Candidate.

adapter = ExAgent.Classifier.ReqLLM.new("openai:gpt-6-luna")

classifier = ExAgent.Classifier.new(
  adapter: adapter,
  instructions: "Which department should handle this request?",
  labels: %{"billing" => "Payments and refunds", "support" => "Other requests"}
)

A call returns the label, optional confidence, and qualified usage. An error after dispatch retains any available usage. Unknown measurements stay unknown. The adapter receives UTF-8 text or normalized JSON objects and arrays, and supplies a bounded result. Only the host configures the policy, provider, credentials and limits.

Summary

Callbacks

Declare the quality that token-limit preflight should expect from this adapter.

Classify the admitted input. The runtime bounds the call to the supplied timeout.

Return a stable provider and model name for admission and accounting.

Functions

Classify one input with the classifier's deadline and rate limiter.

Build an ExAgent.Eval evaluator. Set :input to a function that projects an Eval outcome and :pass to a nonempty list of passing labels. Set :version when comparing reports or observing live runs. Errors fail grading and any available usage enters the grading report.

Build a blocking input, stream, output, tool-input or tool-output guard. Set :allow to a nonempty list of declared labels. Any other label trips the guard. Backend errors fail the check. The guard contributes an admitted request and qualified usage to the run tree.

Build a classifier with an adapter struct and 2–32 string labels. Label identifiers use 1–64 ASCII letters, digits, dots, underscores or hyphens. Descriptions are UTF-8 strings of up to 1,024 bytes. Instructions are UTF-8 and at most 4,096 bytes.

Build a pure Flow selector from a map of labels to declared step IDs. By default it reads "label" from the Flow input. The host runs the classifier before starting the Flow and commits the confirmed label as input. The selected step is recorded by the ordinary Flow checkpoint.

Build a tool that accepts a "text" argument and returns the selected "label" and optional "confidence". Set :name to override "classify" and :requires_approval to require the usual tool approval.

Types

guard_stage()

@type guard_stage() :: ExAgent.Guardrail.stage()

guardrail()

@opaque guardrail()

input()

@type input() :: String.t() | map() | list()

outcome()

@type outcome() ::
  {:ok, ExAgent.Classifier.Result.t()} | {:error, ExAgent.Classifier.Error.t()}

request()

@type request() :: %{
  labels: %{required(String.t()) => String.t()},
  instructions: String.t(),
  timeout: pos_integer()
}

t()

@opaque t()

Callbacks

accounting_quality(struct)

(optional)
@callback accounting_quality(struct()) :: ExAgent.ModelProfile.accounting_quality()

Declare the quality that token-limit preflight should expect from this adapter.

classify(adapter, input, request)

@callback classify(adapter :: struct(), input(), request()) :: outcome()

Classify the admitted input. The runtime bounds the call to the supplied timeout.

identity(struct)

@callback identity(struct()) :: {String.t(), String.t()}

Return a stable provider and model name for admission and accounting.

Functions

classify(classifier, input)

@spec classify(t(), input()) :: outcome()

Classify one input with the classifier's deadline and rate limiter.

evaluator(classifier, name, opts)

@spec evaluator(t(), String.t(), keyword()) :: ExAgent.Eval.Evaluator.t()

Build an ExAgent.Eval evaluator. Set :input to a function that projects an Eval outcome and :pass to a nonempty list of passing labels. Set :version when comparing reports or observing live runs. Errors fail grading and any available usage enters the grading report.

Classifier evaluators refuse ExAgent cassette runs because the cassette format holds chat requests. They use the same classifier for offline adapters, loopback fixtures or live calls made without an ExAgent cassette.

guardrail(classifier, stage, name, opts)

@spec guardrail(t(), guard_stage(), String.t(), keyword()) :: guardrail()

Build a blocking input, stream, output, tool-input or tool-output guard. Set :allow to a nonempty list of declared labels. Any other label trips the guard. Backend errors fail the check. The guard contributes an admitted request and qualified usage to the run tree.

A :stream guard takes the window options of ExAgent.Guardrail.stream/3. Each reviewed window can admit its own request. Supply a one-argument :input projector for :stream, :tool_input and :tool_output. These stages receive a window, a tool call and a tool return respectively. Choose the text or JSON data suitable for the classifier. The projector defaults to the identity function at the :input and :output stages.

Use this guard in non-durable runs with an execution scope. C7 rejects it at admission. Use tool/2 for a classifier call inside a durable run. Output guards gate delivery and use the ordinary guardrail preview policy.

new(opts)

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

Build a classifier with an adapter struct and 2–32 string labels. Label identifiers use 1–64 ASCII letters, digits, dots, underscores or hyphens. Descriptions are UTF-8 strings of up to 1,024 bytes. Instructions are UTF-8 and at most 4,096 bytes.

The options :max_input_bytes and :timeout default to 65,536 bytes and 30,000 ms. Set :rate_limiter to use the configured admission quota. A matching TPM bucket needs an explicit positive :estimated_tokens for the entire request. A reservation supplies quota evidence only.

selector(routes, label_from_input \\ &input_label/1)

@spec selector(%{required(String.t()) => String.t()}, (term() -> term())) ::
  (term() ->
     {:ok, String.t()}
     | {:error, :no_classifier_route})

Build a pure Flow selector from a map of labels to declared step IDs. By default it reads "label" from the Flow input. The host runs the classifier before starting the Flow and commits the confirmed label as input. The selected step is recorded by the ordinary Flow checkpoint.

tool(classifier, opts \\ [])

@spec tool(t(), keyword()) :: ExAgent.Tool.t()

Build a tool that accepts a "text" argument and returns the selected "label" and optional "confidence". Set :name to override "classify" and :requires_approval to require the usual tool approval.

The write effect covers remote inference and its potential charge. C7 journals the decision and qualified usage as a tool outcome. An ambiguous result requires reconciliation. Live runs account for the classifier as a request in the shared execution scope, as they do for classifier guards. The tool never requests a model retry.