Alloy.Provider.Codex (alloy v0.16.0)

Copy Markdown View Source

Provider for ChatGPT-plan-backed Codex execution via codex exec.

This provider treats Codex as a structured completion backend rather than a full agent runtime. Alloy remains responsible for the tool loop, while Codex receives the current transcript and available tool definitions, then returns JSON describing either a final assistant response or one or more tool calls.

Config

Required:

  • :model - Codex model name (for example "gpt-5.4")

Optional:

  • :codex_bin - Executable path (default: "codex")
  • :workdir - Directory passed to codex exec (defaults to a temp dir)
  • :codex_home - CODEX_HOME for the subprocess. Defaults to the inherited CODEX_HOME, or ~/.codex.
  • :profile - Codex profile: layers $CODEX_HOME/<name>.config.toml on the user config (Codex CLI 0.134.0 and later). Selecting a profile loads the user config, including its MCP servers and plugins.
  • :config_overrides - key=value strings passed to codex exec as -c flags, for example [~s(model_reasoning_effort="high")]
  • :reasoning - Portable reasoning config (nil | :off | :low | :medium | :high | :xhigh | %{effort: ..., display: :summarized | :omitted}). Maps :effort to a -c model_reasoning_effort="..." override (:off maps to "none"). An explicit model_reasoning_effort entry in :config_overrides takes precedence.

  • :tmp_dir - Parent for the provider's temp working directory (default: System.tmp_dir!/0)
  • :timeout_ms - Timeout for a single codex exec invocation (default: 120_000)
  • :receive_timeout - Optional cap on :timeout_ms. Alloy's retry loop passes the remaining turn deadline as req_options: [receive_timeout: ms] (the shape HTTP providers use); the smallest of the three wins.
  • :command_runner - Test hook matching System.cmd/3
  • :system_prompt - System prompt string
  • :persist_session - Opt in to persistent Codex transcripts and continuation (default: false). Requires a Codex CLI with exec resume --output-schema. Supply an existing :workdir so the recorded working directory survives per-call cleanup. Profiles remain ephemeral. History, model, home, workdir, system prompt, or tool changes start a fresh session. The caller owns retention and isolation of the persistent CODEX_HOME.
  • :session_generation - Host-owned continuation generation (default: nil). Change it to a new value to start fresh while preserving Alloy's history.

Authentication

Codex reads and refreshes its login in CODEX_HOME, so every call shares the same credentials as the codex CLI and refreshed tokens are kept. Requires Codex CLI 0.122.0 or later.

Unless :profile is set, calls run with --ignore-user-config and --ignore-rules: config.toml (MCP servers, plugins, hooks, model settings) and execpolicy rules are not loaded. Codex still reads $CODEX_HOME/AGENTS.md and skills from CODEX_HOME. For full isolation, log in to a dedicated home once (CODEX_HOME=~/.codex-alloy codex login) and pass it as :codex_home. A dedicated home also keeps Alloy from sharing a refresh token with your interactive Codex sessions.

If your credentials are in the OS keyring (cli_auth_credentials_store = "keyring"), pass config_overrides: [~s(cli_auth_credentials_store="keyring")], since that setting lives in the ignored config.toml.

Errors

Failures of the codex exec process return %Alloy.Provider.Error{} with the same message text as before: :timeout (which the loop retries within the turn deadline), :context_overflow (the loop compacts and retries), or :unknown, which covers a missing executable and other failed runs. A tool_use response without tool calls returns :malformed_reply and is retried within the configured retry budget; accompanying text is not treated as evidence of task completion.

A failed resumed call returns non-retryable :session_uncertain — or :shutdown if interrupted by supervisor shutdown; in both cases the saved Codex transcript may already include the attempted turn. After any failed, cancelled, or crashed turn in a persistent session (including cancellation during Alloy tool execution), change :session_generation before continuing. This also recovers from a deleted or corrupt saved thread. Do not share continuation state between concurrent callers.

Hosts retaining Alloy.Agent.State can use Alloy.Agent.State.prepare/2 with the same provider configuration and a new :session_generation. This preserves conversation history and clears the old continuation state. For direct provider calls, either change the generation or remove :provider_state before the next call. Recovery is an explicit host action, never an automatic resend after failure. Leave :fallback_providers empty when using persistence: a successful fallback can hide a resume failure from the host and leave stale continuation state. Continuation metadata is opaque, uses string keys, and must be preserved as returned rather than converted to atoms. Repeated instruction/tool schemas also grow the CLI transcript; a new generation recovers from context exhaustion.

Notes

  • Usage comes from the turn.completed event of codex exec --json. As with every built-in provider, :input_tokens is uncached input; cache reads and writes are reported separately. :reasoning_output_tokens is part of :output_tokens. Counts include Codex's own instructions and tool definitions, not just the transcript.
  • Each call runs codex exec under Alloy.TaskSupervisor. A timeout, or the calling process exiting (a cancelled turn), stops codex and every process it started, and removes the call's temp files.
  • Streaming emits the last validated structured reply at turn.completed before the process exits, not token-by-token JSON fragments. Commands and commentary remain in response metadata, never in answer chunks. If no such event is available, the validated final response is replayed once.
  • :command_runner is a test/injection hook that runs directly in the calling process without port timeout or cancellation wrapping.

Summary

Types

Configuration for the Codex provider. See the module doc for field semantics.

Types

config()

@type config() :: %{
  :model => String.t(),
  optional(:codex_bin) => String.t(),
  optional(:workdir) => String.t(),
  optional(:profile) => String.t(),
  optional(:codex_home) => String.t(),
  optional(:config_overrides) => [String.t()],
  optional(:reasoning) => Alloy.Provider.reasoning(),
  optional(:tmp_dir) => String.t(),
  optional(:timeout_ms) => pos_integer(),
  optional(:receive_timeout) => pos_integer(),
  optional(:req_options) => keyword(),
  optional(:system_prompt) => String.t(),
  optional(:persist_session) => boolean(),
  optional(:session_generation) => term(),
  optional(:provider_state) => map(),
  optional(:command_runner) => (String.t(), [String.t()], keyword() ->
                                  {String.t(), integer()})
}

Configuration for the Codex provider. See the module doc for field semantics.