Skip to content

Codex Step

CodexStepDescription hands a whole subtask to a local Codex agent. CARL drives it through the optional openai-codex Python SDK, which starts a local Codex app-server process. The step collects the final response, the thread id and token usage.

The interface mirrors the Claude Code step: the same task + input_mapping template, output_schema, output_memory_key and session resume. The authority model is stricter: the chain can only pick a read-only or workspace-write sandbox, and every approval request is denied.

Terminal window
pip install 'mmar-carl[codex]' # openai-codex>=0.147.0, also in mmar-carl[all]
codex login # authentication stays in the host's Codex install

The host must also allow local agents for the run: ReasoningContext(..., allow_local_agent_execution=True). The flag defaults to False and is never serialized; without it the step fails before Codex starts. This gate is new in 0.5.0.

Check the host before running:

from mmar_carl import check_codex_runtime
status = check_codex_runtime()
print(status.available, status.sdk_version, status.cli_path, status.error)

available is True when the SDK imports. cli_path (the codex binary on PATH) is informational. Neither check verifies login. chain.preflight(context) runs the same SDK check and reports openai-codex in missing_codex_runtimes when it is not installed.

from mmar_carl import CodexStepConfig, CodexStepDescription
CodexStepDescription(
number=2,
title="Draft the fix",
dependencies=[1],
config=CodexStepConfig(
task="Fix the failing test described here:\n{report}",
input_mapping={"report": "$history[-1]"},
cwd="/work/repo",
sandbox="workspace-write", # opt in to file edits
reasoning_effort="medium",
timeout=600,
output_memory_key="fix_summary", # → $memory.codex.fix_summary
),
)

Unknown fields are rejected (extra="forbid").

FieldTypeDefaultPurpose
taskstrrequired1–16 000 chars, non-blank. With a non-empty input_mapping it is a str.format template. Wire alias: instruction.
input_mappingdict[str, str]{}Up to 32 entries. Names must be identifiers; sources are non-empty, ≤ 2048 chars. $… is a reference, a quoted string is a literal.
cwdstr | NoneNoneWorking directory for the Codex thread. Must exist.
sandbox"read-only" | "workspace-write""read-only"Filesystem sandbox. Full access is not supported.
modelstr | NoneNoneModel override. None = the local Codex default.
reasoning_effortnone, minimal, low, medium, high, xhigh or NoneNoneReasoning-effort override.
developer_instructionsstr | NoneNoneHost-authored developer instructions, ≤ 16 000 chars.
resume_sessionstr | NoneNoneThread id to resume; a $ reference is resolved at run time. Wire alias: thread_id_source.
ephemeralboolTrueKeep a newly started thread out of durable Codex history.
timeoutfloat300.0Whole-step budget in seconds, including runtime start-up. 0 < timeout ≤ 3600. Wire alias: timeout_seconds.
max_input_bytesint131072Limit for the rendered task, UTF-8 bytes (1 KiB–1 MiB).
output_memory_keystr | NoneNoneMemory key for the final answer (the parsed object when output_schema is set).
output_namespacestr"codex"Namespace for output_memory_key.
output_schemadict | NoneNoneJSON Schema. The final response must contain a matching JSON payload, or the step fails.
store_session_keystr | NoneNoneMemory key for the thread id in namespace codex. None = step_<number>. Wire alias: store_thread_key.
  • sandbox="read-only" is the default; editing files needs an explicit workspace-write.
  • Headless runs use ApprovalMode.deny_all: the agent cannot pause the chain to ask for more authority.
  • Credentials, environment overrides, arbitrary launch arguments and the approval policy are not part of the serialized config.
  • Beyond allow_local_agent_execution there is no host-side policy that limits which cwd a chain may choose or whether workspace-write is allowed. If you run chains you did not write, check these two fields before granting the flag.
  • cache is rejected on this step type.

On success:

WhereWhat
resultThe final response. Also added to history with the tag [CODEX].
result_data["session_id"] / ["thread_id"]Thread id (same value). Also written to $memory.codex.step_<n> (or store_session_key).
result_data["turn_id"], ["status"]The completed turn.
result_data["sandbox"], ["cwd"], ["configured_model"]What the step asked for.
result_data["resumed"], ["ephemeral"]Whether the thread was resumed (ephemeral is None then).
result_data["usage"]Raw SDK usage.
result_data["structured_output"]Parsed JSON when output_schema is set, else None.
token_usage{prompt, completion, total} from the turn’s total usage.
modelThe configured override, if any (the SDK does not report the resolved model).
CodexStepConfig(
task="Now add a regression test for that fix.",
resume_session="$memory.codex.step_2",
sandbox="workspace-write",
)

The step returns success=False with error_message when:

  • the host did not set allow_local_agent_execution;
  • the SDK is not installed (Optional dependency 'openai_codex' not found…);
  • cwd is not an existing directory;
  • a {placeholder} has no input_mapping entry, or the rendered task exceeds max_input_bytes;
  • resume_session is a reference that resolves to an empty value;
  • the turn ends with a status other than completed, or reports an error;
  • timeout expires (Codex turn timed out after …s);
  • output_schema is set and the response has no matching JSON payload.

Error text is cut to 500 characters.

  • No streaming: the SDK returns one completed turn, so on_llm_chunk is not fed.
  • estimate_chain_cost does not model this step. Read result_data["usage"] after the run.
  • Each step starts its own local app-server, which adds start-up latency.