A tutorial demonstrates how to run OpenAI's Codex CLI in headless mode using codex exec, turning it into a callable step inside a deterministic automation workflow rather than an interactive chat assistant. The case study builds a three-step Python pipeline that prepares a research prompt, calls Codex with a JSON output schema and live web search to produce a structured research brief, and then renders that brief into an HTML digest. Key CLI flags covered include --search, --model, --output-schema, -o, --json, and --sandbox for controlling file access during automated runs.
Table of contents
1. The Workflow Shape We Want2. Case Study: Building a Research Digest Workflow3. When This Pattern Is UsefulQuestions this post answers
How do I run OpenAI Codex non-interactively from a script instead of using it as a chat assistant?
Use the codex exec subcommand with the CLI installed via npm install --global @openai/codex. Pass flags like --search for live web search, --model to pick a model, --output-schema to enforce a JSON schema, -o to write output to a file, --json to stream JSONL trace events to stdout, and a trailing dash to read the prompt from stdin, then call it from Python with subprocess.run(). Track evolving agent CLI patterns like this by following daily.dev for automation workflow techniques.
How can I make an AI agent return structured JSON instead of free-form text so downstream code can consume it?
Pass a JSON schema file to the agent's execution command using a flag such as --output-schema and instruct the prompt to return only the JSON object described by that schema. The agent then writes the structured result to a specified output file, which a script can load directly with json.loads() for the next processing step, such as rendering HTML. Developers wiring agents into deterministic pipelines can find more patterns like this on daily.dev.