LLMProxy.Guardrail behaviour (llm_proxy v0.1.0)

Copy Markdown View Source

Behaviour for host-defined request/response policy hooks.

Guardrails are configured with:

config :llm_proxy, guardrails: [MyApp.LLMPolicy]

Callbacks run in order. They should be deterministic and side-effect-light.

Summary

Types

context()

@type context() :: %{
  optional(:actor) => LLMProxy.Actor.t(),
  optional(:api_key) => map(),
  optional(:route) => atom(),
  optional(:model) => String.t(),
  optional(:provider) => module(),
  optional(:provider_name) => String.t() | nil,
  optional(:trace_id) => String.t(),
  optional(:cache_key) => String.t(),
  optional(:metadata) => map()
}

Callbacks

after_response(t, context)

(optional)
@callback after_response(LLMProxy.Response.t(), context()) ::
  {:ok, LLMProxy.Response.t()} | {:error, term()}

before_request(t, context)

(optional)
@callback before_request(LLMProxy.Protocol.Request.t(), context()) ::
  {:ok, LLMProxy.Protocol.Request.t()} | {:error, term()}

on_stream_event(t, context)

(optional)
@callback on_stream_event(LLMProxy.Stream.Event.t(), context()) ::
  {:ok, LLMProxy.Stream.Event.t() | nil} | {:error, term()}