ExAgent.Model behaviour (ExAgent v3.0.0)

View Source

Behaviour that every provider model implements.

Stability — Stable.

A "model" is any struct whose module @behaviours ExAgent.Model. The struct carries the model's own state (base URL, API key, configured options…). Callers never invoke a provider module directly — they go through the dispatch functions in this module (request/4, request_stream/4, …), which resolve the implementer from the struct's __struct__.

When a run opens its execution scope, it calls system/1 and model_name/1 synchronously. Keep both callbacks local and prompt. If either raises, throws or exits, admission fails with {:model_request_failed, %ExAgent.RequestError{reason: :invalid_model_identity}} before a request is counted.

The dispatch functions return every failure as %ExAgent.RequestError{}. An implementation may return its own RequestError or a bare reason; a bare reason, a result outside the callback contract (:invalid_model_result) and the dispatcher's own stream failures are wrapped with provider: nil. A new response must supply :stop or :tool_calls as its finish_reason to enter the run history. Every other finish (including :length, :content_filter, :cancelled, :error, :incomplete, :unknown and nil) ends the run with {:incomplete_response, finish_reason} and a diagnostic in partial.pending_response. Unrecognized values are reported as :unknown. The historical reader still accepts missing finishes.

This uses Elixir behaviours plus struct dispatch so providers can stay small and explicit.

Summary

Callbacks

Optional static configuration binding for persisted continuation, without IO.

Declares capabilities (optional, with conservative defaults). The core rejects function tools and tool-based structured output when supports_tools is false. Explicit native output requires supports_json_schema_output: true before request admission. Native JSON/thinking flags do not describe tool-output mode and are not used to silently downgrade it. Supported provider file reference pairs belong in the :file_references field of ExAgent.ModelProfile. Streaming requires its own callback.

Make a non-streaming request and return a full Message.Response.

Make a lazy streaming request. Events are {:text_delta, binary} for answer text, optional {:commentary_delta, binary} for assistant narration, optional {:thinking_delta, binary} for reasoning, optional {:usage, cumulative_request_usage}, and exactly one terminal {:response, response, final_model} or {:error, reason}. Usage snapshots replace earlier snapshots for this request; they are not additive deltas. When params.tool_call_deltas is true, implementations may also emit {:tool_call_delta, %{index: index, name: name_or_nil, fragment: binary}} as tool-call arguments are generated: the first event for an index carries the tool name, later ones carry raw argument JSON fragments. They are a preview only; the terminal response stays authoritative. Structured output consumers request these fragments independently of raw preview delivery. The stock ReqLLM adapter marks its named delta with arguments_codec: :arguments_envelope; other adapters emit logical JSON. Implementations must release resources when enumeration halts. A stream ending without a terminal is a protocol error. Implementations may return {:error, _} directly when streaming is unsupported.

Optional no-IO preflight required by persisted continuation. Validate restored logical history, provider/endpoint binding and codec/settings using the trusted current model and tool inventory. Never dispatch a request from this callback. Ordinary runs may use models without this callback. ExAgent.Models.Fallback honors the result when a candidate implements it.

Functions

Returns the bounded, portable static continuation binding; no Model IO.

Resolve a custom Model struct, Test label, or stock ReqLLM model spec.

Types

messages()

@type messages() :: [ExAgent.Message.t()]

model()

@type model() :: struct()

Callbacks

continuation_binding(model)

(optional)
@callback continuation_binding(model()) :: {:ok, term()} | {:error, term()}

Optional static configuration binding for persisted continuation, without IO.

Return {:ok, json_data} (at most 4096 encoded JSON bytes) or {:error, reason}. Exclude credentials, transient state and runtime counters. The normalized value must be deterministic across equivalent configurations. Absent callbacks return {:ok, nil}. Frame restore checks both the trusted template and app-codec result; a changed binding rejects before resuming effects, including a first uncertain Model request with no response history. Callback errors are normalized, not echoed.

model_name(model)

@callback model_name(model()) :: String.t()

profile(model)

(optional)
@callback profile(model()) :: ExAgent.ModelProfile.t()

Declares capabilities (optional, with conservative defaults). The core rejects function tools and tool-based structured output when supports_tools is false. Explicit native output requires supports_json_schema_output: true before request admission. Native JSON/thinking flags do not describe tool-output mode and are not used to silently downgrade it. Supported provider file reference pairs belong in the :file_references field of ExAgent.ModelProfile. Streaming requires its own callback.

request(model, messages, arg3, t)

@callback request(
  model :: model(),
  messages(),
  ExAgent.ModelSettings.t() | nil,
  ExAgent.ModelRequestParameters.t()
) :: {:ok, ExAgent.Message.Response.t(), model()} | {:error, term()}

Make a non-streaming request and return a full Message.Response.

Implementations return the (possibly updated) model struct alongside the response. Real providers return the model unchanged; stateful models (e.g. the script-driven Test) thread their internal state through the run this way, avoiding globals.

In a response, supplied tool call IDs must be distinct UTF-8 strings of 1 to 512 bytes. ExAgent generates an ID when the adapter supplies nil. End a completed text response with :stop and a tool batch with :tool_calls. These rules also apply to the terminal streaming response.

A persisted continuation requires a JSON-portable response, including any changes made by after-model hooks.

request_stream(model, messages, arg3, t)

(optional)
@callback request_stream(
  model :: model(),
  messages(),
  ExAgent.ModelSettings.t() | nil,
  ExAgent.ModelRequestParameters.t()
) :: Enumerable.t() | {:error, term()}

Make a lazy streaming request. Events are {:text_delta, binary} for answer text, optional {:commentary_delta, binary} for assistant narration, optional {:thinking_delta, binary} for reasoning, optional {:usage, cumulative_request_usage}, and exactly one terminal {:response, response, final_model} or {:error, reason}. Usage snapshots replace earlier snapshots for this request; they are not additive deltas. When params.tool_call_deltas is true, implementations may also emit {:tool_call_delta, %{index: index, name: name_or_nil, fragment: binary}} as tool-call arguments are generated: the first event for an index carries the tool name, later ones carry raw argument JSON fragments. They are a preview only; the terminal response stays authoritative. Structured output consumers request these fragments independently of raw preview delivery. The stock ReqLLM adapter marks its named delta with arguments_codec: :arguments_envelope; other adapters emit logical JSON. Implementations must release resources when enumeration halts. A stream ending without a terminal is a protocol error. Implementations may return {:error, _} directly when streaming is unsupported.

For buffered and streaming terminals, dispatch discards invalid optional citations and keeps the response's recognized finish reason and usage.

system(model)

@callback system(model()) :: String.t()

validate_resume(model, messages, t, t)

(optional)
@callback validate_resume(
  model(),
  messages(),
  ExAgent.ModelSettings.t(),
  ExAgent.ModelRequestParameters.t()
) :: :ok | {:error, term()}

Optional no-IO preflight required by persisted continuation. Validate restored logical history, provider/endpoint binding and codec/settings using the trusted current model and tool inventory. Never dispatch a request from this callback. Ordinary runs may use models without this callback. ExAgent.Models.Fallback honors the result when a candidate implements it.

Functions

continuation_binding(model)

Returns the bounded, portable static continuation binding; no Model IO.

model_name(model)

@spec model_name(model()) :: String.t()

profile(model)

@spec profile(model()) :: ExAgent.ModelProfile.t()

request(model, messages, settings, params)

request_stream(model, messages, settings, params)

resolve(spec, opts \\ [])

@spec resolve(model() | String.t() | map() | tuple(), keyword()) ::
  {:ok, model()} | {:error, term()}

Resolve a custom Model struct, Test label, or stock ReqLLM model spec.

Custom structs are returned unchanged. Catalogue strings, explicit maps, stock tuples and LLMDB.Model specs resolve through public ReqLLM.model/1 and use ExAgent.Models.ReqLLM. An Ollama, LM Studio or vLLM ID missing from the catalogue resolves as a stock inline model. The model profile governs tools and streaming. Pass instance credentials and an explicitly qualified profile with resolve/2, or construct ExAgent.Models.ReqLLM directly. No credentials are read from the environment by this resolver. Provider prefixes are those of the ReqLLM catalogue; there are no ExAgent-specific aliases.

system(model)

@spec system(model()) :: String.t()