Alloy 0.16 adds major-lab wire parity (multimodal PDFs/documents, image URLs, dynamic :api_key callbacks, provider caching, Gemini native tools, portable :reasoning), uniform v1 streaming events, cooperative cancellation (Alloy.Cancel), mid-run :steering and completion :follow_up callbacks, Alloy.Tool.Executor.run_tool/3, and optional Alloy.ModelCatalog capability and pricing metadata — all with zero new mandatory runtime dependencies.

Most v0.14 callers upgrade cleanly; review the contract additions and stricter checks below before bumping.

1. Cooperative cancellation (:cancelled status and error kind)

Runs given a :cancel_token (Alloy.Cancel.new/0) return {:error, %Alloy.Result{status: :cancelled, error: :cancelled}} when cancelled cooperatively.

  • Exhaustive pattern matches & result.error type: Alloy.Agent.State.status/0, Alloy.Result.status, and Alloy.Provider.Error.kind/0 now include :cancelled. Update any exhaustive case matches on run status, :turn_end event status, [:alloy, :run, :stop] telemetry status, or error.kind. Note that result.error on {:error, result} is now the atom :cancelled rather than a String.t() (any underlying %Alloy.Provider.Error{kind: :cancelled} remains in result.metadata.run.provider_error), so callers passing result.error directly to String.* helpers must handle :cancelled.
  • Persistent Codex sessions after cancellation: Callers using Alloy.Provider.Codex with persist_session: true should rotate :session_generation after a :cancelled run before starting new work so an interrupted CLI session is not resumed in a partial state.
  • Fail-closed middleware check: Alloy.Middleware.run_before_tool_call/2 now returns :cancelled when state.config.cancel_token is cancelled (while preserving explicit {:block, reason} verdicts). Callers invoking run_before_tool_call/2 directly must handle :cancelled.
  • Runtime provider config keys: When :cancel_token is set, Alloy injects :alloy_cancel_token into the provider_config map passed to providers (alongside :alloy_request_deadline), and when top-level :reasoning is set, Alloy injects normalized :reasoning (%{effort: ..., display: ...}). Custom providers using strict option validators should tolerate unknown :alloy_* keys and :reasoning.

2. Dynamic :api_key callbacks, static key validation & required :origin_key

Built-in HTTP providers (Anthropic, OpenAI, XAI, Gemini, OpenAICompat) now accept a 0-arity :api_key callback returning String.t() | {:ok, String.t()} | {:error, term()} resolved per HTTP attempt.

  • Required :origin_key (applies to every primary and fallback provider): Alloy.Agent.Config enforces :origin_key validation across all primary and :fallback_providers configs — including custom providers. Using a 0-arity :api_key function OR auth_scheme: :bearer (supported on Anthropic and Gemini among built-ins) now requires a non-empty string :origin_key so rotating credentials preserve account provenance across turns and origin-scoped file attachments (file_id / Gemini File API URIs). Passing either without :origin_key raises ArgumentError at startup.
  • Key validation: Both static string :api_key values and callback results must be printable ASCII (0x20..0x7E, no CR/LF characters). Arbitrary callback failures map to non-retryable %Alloy.Provider.Error{kind: :auth} with sanitized messages (explicit {:error, %Alloy.Provider.Error{}} returns preserve allowlisted kinds, including :cancelled, with diagnostic fields redacted).

3. Portable :reasoning option

Pass :reasoning at the top level (Alloy.run/2, Alloy.stream/3, Alloy.Agent.State.prepare/2) or inside provider config: nil | :off | :low | :medium | :high | :xhigh | %{effort: ..., display: ...}.

  • Global config validation (including custom providers): Alloy.Agent.Config validates and normalizes :reasoning on every primary and fallback provider config at startup (and via State.prepare/2). Custom providers that previously stored a custom non-portable :reasoning value in their provider config will now raise ArgumentError; rename that provider-specific option or accept the normalized %{effort: ..., display: ...} map.
  • Anthropic reasoning: :off: Explicitly sends %{"thinking" => %{"type" => "disabled"}} (whereas reasoning: nil omits "thinking" as in v0.14). Explicit "thinking" inside extra_body still takes precedence.
  • Compaction summary stripping: :reasoning (along with :prompt_cache_key, :prompt_cache_retention, :cached_content, :google_search, :url_context, and Gemini :code_execution) is stripped automatically on compaction summary requests.

4. Uniform v1 streaming events (events.md)

Alloy.Events v1 adds five additive event types:

  • :turn_start (%{turn:}) and :turn_end (%{turn:, status:, stop_reason:, usage:})
  • :thinking_delta across Anthropic, OpenAI/xAI Responses, Gemini, and OpenAICompat
  • :tool_call_delta (%{id:, index:, name:, arguments_delta:})
  • :tool_update (%{id:, name:, chunk:, start_event_seq:}), marshalled to the caller process and wired to Alloy.Tool.Core.Bash
  • Optional :parent_call_id on :tool_start, :tool_update, and :tool_end when emitted for nested calls via Alloy.Tool.Executor.run_tool/3 or execute_all/4

Subscribers matching on v1 envelopes should keep a catch-all clause for unknown event atoms. Note that :thinking_delta, :tool_call_delta, and :tool_update are emitted only when :on_event is subscribed so unsubscribed Alloy.stream/3 callers preserve retry/fallback resilience across non-text deltas.

5. Stricter OpenAI / OpenAIStream tool-call validation & Events.build_correlation_id/1

  • Alloy.Provider.OpenAIStream (Chat Completions streaming via OpenAICompat): Negative/malformed tool-call indices and conflicting tool identities (id / name changes on the same slot) now return non-retryable :protocol_error; non-empty invalid JSON or non-object tool arguments return retryable :malformed_reply (with retries suppressed once any output has been emitted to caller callbacks).
  • Alloy.Provider.OpenAI (OpenAI / xAI Responses streaming): Conflicting streamed tool-call headers (id / name mismatches) and contradictory completed tool arguments return :malformed_reply (retryable before output is emitted; retry/fallback-suppressed once output has been emitted to caller callbacks) instead of silently corrupting tool calls.
  • Alloy.Events.build_correlation_id/1: Now requires non-empty valid UTF-8 strings for context[:request_id] and context[:correlation_id]; empty strings ("") or invalid UTF-8 fall back cleanly to a generated correlation ID instead of propagating invalid IDs to Alloy.Tool.Executor.

6. Mid-run :steering and completion :follow_up

  • :steering ((State.t() -> [Message.t()])) is polled between completed tool batches; non-empty messages skip remaining unstarted batches with synthetic error tool_result blocks, append the steering messages, and advance to the next turn (bypassing :stop_after_tool).
  • :follow_up ((State.t() -> [Message.t()])) is polled right before normal non-refusal :end_turn completion once :until_tool is satisfied, bounded by :max_turns, :timeout_ms, and :cancel_token.
  • Both callbacks strictly validate returned [%Alloy.Message{}] lists (roles, UTF-8, JSON-compatible block fields, and Alloy.Transcript.validate/1 == :ok) and sanitize exceptions/throws/exits with :redact.

7. Reusable single-tool execution & optional Alloy.ModelCatalog metadata