ReqLLM.Router behaviour (ReqLLM v1.26.0)

View Source

A behaviour for application-defined model routers.

A router is any struct whose module implements resolve/2. ReqLLM gives the callback a normalized ReqLLM.Router.Request. The callback must return a concrete %LLMDB.Model{}.

The request contains only the normalized conversation context and an opaque application routing_context map. ReqLLM does not infer routing policy from the generation function or its options.

defmodule MyApp.ModelRouter do
  @behaviour ReqLLM.Router

  defstruct fast_model: "openai:gpt-4o-mini",
            deep_model: "anthropic:claude-sonnet-4-5"

  @impl true
  def resolve(router, %ReqLLM.Router.Request{} = request) do
    model_spec =
      case request.routing_context do
        %{mode: :deep} -> router.deep_model
        _other -> router.fast_model
      end

    ReqLLM.model(model_spec)
  end
end

router = %MyApp.ModelRouter{}
ReqLLM.generate_text(router, "Hello", routing_context: %{mode: :fast})

ReqLLM does not provide a routing policy. The callback can use local rules, an evaluation model, or another service.

Summary

Functions

Returns true when a struct implements the router behaviour.

Resolves an application router to a concrete LLMDB model.

Types

t()

@type t() :: struct()

Callbacks

resolve(t, t)

@callback resolve(t(), ReqLLM.Router.Request.t()) ::
  {:ok, LLMDB.Model.t()} | {:error, term()}

Functions

implementation?(arg1)

@spec implementation?(term()) :: boolean()

Returns true when a struct implements the router behaviour.

resolve(router, request)

@spec resolve(t(), ReqLLM.Router.Request.t()) ::
  {:ok, LLMDB.Model.t()} | {:error, term()}

Resolves an application router to a concrete LLMDB model.