All Agent Engineering

Wire a Codex batch job into CI and accept its structured result

Keep stderr and stdout apart for codex exec in CI, request a schema-shaped final JSON, and validate it independently.

AINavWritten / Updated
On this page

At a glance

In CI, codex exec streams progress to stderr and prints only the final message to stdout, so the workflow should store the two streams separately and validate the final JSON on its own. This article gives an acceptance sheet: the input is the CI task description and the agreed JSON fields, and the deliverable is a batch acceptance record you can tick off item by item. The only basis is the official non-interactive documentation, and none of the commands below was executed.

Runtime and applicability

Article type
Engineering practice
Verified
Applies to
  • Codex Wire a Codex batch job into CI and accept its structured result

Execution locations and responsibilities

  • Codex host and calling client Use the workflow below to distinguish responsibilities of the caller, runtime, and external actions.
Engineering workflow order
Engineering workflow order1. Fix permissions and credential scope → 2. Write the schema file → 3. Invoke and store the outputs separately → 4. Define failure rules → 5. Validate the final JSON independently → 6. Archive evidence and check the event stream. Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.1. Fix permissions and credential scope2. Write the schema file3. Invoke and store the outputs separately4. Define failure rules5. Validate the final JSON independently6. Archive evidence and check the event stream

Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.

When to use

  • While codex exec runs it streams progress to stderr and prints only the final agent message to stdout. With --json enabled, stdout becomes a JSONL event stream rather than a single final JSON, so decide first whether you consume the final message or the event stream.

When not to use

  • With --json enabled, stdout is an event stream and not the final JSON; do not hand the event stream to downstream steps as the result file.
  • The documentation does not describe what happens when the model cannot satisfy the schema, and gives no exit code beyond the required-MCP-server failure, so validation and failure rules have to be owned by CI.
  • This sheet does not cover the GitHub Actions patch and pull request flow or session resumption; the latter is a separate task.

Prerequisites and input

  • The CI runner has a callable codex command and the task runs inside a Git repository; otherwise assess whether the environment is safe before deciding on --skip-git-repo-check.
  • You have decided which JSON fields downstream steps consume, and your team has a JSON Schema validation tool available.
  • The credential plan is settled: CODEX_API_KEY is set only for the one Codex invocation that needs it; for GitHub Actions, read the official Codex GitHub Action guidance instead.

Engineering checks

Output stream separation — While codex exec runs it streams progress to stderr and prints only the final agent message to stdout. With --json enabled, stdout becomes a JSONL event stream rather than a single final JSON, so decide first whether you consume the final message or the event stream.

Result contract — --output-schema requests a final response that conforms to a JSON Schema, and -o writes the final message to a file while still printing it to stdout. The official sample schema uses required and sets additionalProperties to false. The text says 'request', so CI must still validate the result itself.

Failures and permissions — The default sandbox is read-only and --full-auto is no longer supported; if an enabled MCP server with required = true fails to initialize, codex exec exits with an error. Exit codes for other failures are not enumerated in the documentation, so the acceptance sheet cannot assume them.

1. Fix permissions and credential scope

Where: the CI job definition and runner environment. Input: the task prompt and a decision on whether files must be edited. Action: the default sandbox is read-only; add --sandbox workspace-write only when writes are truly needed, and use danger-full-access only in an isolated runner or container. Set CODEX_API_KEY for this one invocation only, not as a job-level variable in a job that runs repository code. Completion check: the sheet records the chosen sandbox and the credential scope. Stop condition: if untrusted code shares the same process environment, or you cannot confirm the working directory is a Git repository, stop and do not call Codex.

2. Write the schema file

Where: a schema file in the repository (the official sample names it schema.json). Input: the list of fields downstream steps actually read. Action: write an object schema, give each field a type, put the mandatory fields in required, and set additionalProperties to false so unagreed fields are rejected. Completion check: the file is itself valid JSON and every downstream field appears in it. Stop condition: if a field cannot be expressed in the schema, change the downstream agreement first rather than continue with a gap.

3. Invoke and store the outputs separately

Where: the CI script step that calls codex exec. Input: the prompt, the schema file given to --output-schema, and the output path given to -o. Action: call with both options, write stderr to its own log file and stdout to another file, and do not merge the two streams. The documentation shows no example combining --json with --output-schema, so run a small separate trial if you need the event stream. Completion check: the log file, the stdout file and the -o file each exist. Stop condition: if the command never starts (command not found, authentication failure), record the cause, skip validation and downstream consumption, and still enter archival closeout.

4. Define failure rules

Where: the script, after codex exec returns. Input: the exit status, the stdout file, the -o file and the stderr log. Action: treat a nonzero exit status, a missing output file and an empty output file all as failures, and never pull a result out of the stderr log. Completion check: each failure maps to a clear CI status that the sheet lists. Stop condition: when any failure occurs, mark the task failed and stop downstream consumption while still entering archival closeout; whether to retry is your CI policy and is not silently done here.

5. Validate the final JSON independently

Where: a separate validation step after the Codex call. Input: the -o output file and the schema file. Action: first confirm the file parses as JSON, then use your team's validator to check required fields, types and extra fields. Completion check: the pass or fail result and its reason are written to the sheet. Stop condition: stop on a parse failure or a validation failure and pass nothing downstream. A parse or validation failure stops downstream consumption but still goes to archival.

6. Archive evidence and check the event stream

Where: CI closeout executed on success and failure. Input: existing stderr, stdout, result file, exit status, validation outcome and stop reason. Use a local finally/always-run mechanism to retain what exists, explicitly marking missing files, failure to start and unexecuted validation without fabricating JSON. Keep any --json event log separate from the final result. Completion: successful tasks have required outputs; failed tasks are traceable through available evidence, exit status, missing items and stop reason. Stop downstream consumption if archival fails, retain a concrete follow-up and never treat failed closeout as business success.

Task example

This is an original manual walkthrough and no command was run. Imagine a CI task that extracts project metadata, with a schema whose only required fields are project_name and programming_languages. After the call in step 3, the expected observations are: the stderr log holds progress messages, the stdout file holds only the final message, the -o file matches it, and after validation passes the sheet records the task as complete. Now imagine a failure: the final JSON contains one extra, unagreed field, so the validation step fails because additionalProperties is false, the task stops, and nothing goes downstream. All of this is hypothetical expectation, not output from an actual run.

Completion checks

  • stderr and stdout are two separate files, and no progress text leaked into the result file.
  • Every task has three records: the schema file, the output file and the validation result.
  • A nonzero exit, missing output, empty output and validation failure each map to a defined failure status and each blocks downstream steps.
  • The sandbox choice and the scope of CODEX_API_KEY are recorded in writing on the sheet.

Verification scope

Only the official non-interactive documentation was read. Installation, login, API execution and output were not tested, and every observation in the example is a hypothetical expectation rather than a run result. Exit codes and schema-mismatch behavior that the documentation does not cover are marked as to be confirmed in the boundaries.

Sources

All links are official pages; verification dates are listed per source, and links point to the version checked.

  1. Codex · Codex documentation: codex-batch

Found an error? Tell us →