Skip to main content

Codex CLI Integration

PraisonAI provides integration with OpenAI’s Codex CLI for non-interactive code execution, file modifications, and structured output.

Installation

Quick Start

Use as Agent Backend

Delegate an Agent’s LLM turns to codex exec instead of the OpenAI API — uses your ChatGPT subscription.
cli_backend= is deprecated (removal in 2.0.0). Prefer runtime="codex-cli". Run praisonai doctor fix --execute to auto-migrate YAML.

Backend Configuration

The codex-cli backend ships with this default configuration:
clear_env=["OPENAI_API_KEY"] explicitly strips the env var so codex uses your subscription session, not any leftover API key.

Backend CLI Flags

The backend builds the codex command with these flags:

Configuration Options

Examples

Basic Execution

Full Auto Mode

Enable file modifications:

Sandbox Modes

JSON Streaming Output

Structured Output with Schema

Streaming Output

As Agent Tool

As a native async agent tool

Preferred when the agent runs on agent.astart(...) (or any async entrypoint) — the tool is awaited directly on the running loop with no thread hop.
as_async_tool() was added in PraisonAI PR #4022; as_tool() still works and is now also safe to call from inside an async agent runtime.

Environment Variables

CLI Flags Used

The integration uses the following Codex CLI flags:

JSON Lines Output Format

When json_output=True, the output is a stream of JSON events:

Error Handling

Robustness (PR #4111)

  • A subprocess TimeoutError is now returned as CliBackendResult(error=...) instead of escaping as an exception.
  • On CalledProcessError, the CLI’s actual stderr diagnostic is surfaced (previously only the exit status was shown).
  • The system-prompt argument is JSON-escaped for TOML basic-string compatibility — prompts containing quotes/newlines no longer break.
  • The resume shape is now codex exec resume <id> --skip-git-repo-check ... (previously resume was misplaced after --skip-git-repo-check, which failed).
  • -m (model) and -C (cwd) are threaded through, so scheduled runs can pin a model and run in a workspace.
  • CliSessionBinding.is_resume is now set on the second turn of a session, so the resume branch runs instead of re-sending the system prompt every turn.

Best Practices

  1. Use full_auto=True only when file modifications are needed
  2. Use structured output for CI/CD pipelines
  3. Set appropriate sandbox mode based on security requirements
  4. Use JSON output for programmatic processing
  5. Set timeouts appropriate for task complexity