Codex Local SDK Usage
Route Quickly
Load only the reference file needed for the task:
- •Select an execution path:
references/task-routing.md - •Check methods, request flags, and result fields:
references/sdk-api-cheatsheet.md - •Debug failures, retries, and session issues:
references/guardrails-and-failure-modes.md
Execute Workflow
- •Run
scripts/preflight_check.pyto verify Python, package import, andcodexCLI presence. - •Run
scripts/scaffold_usage_template.py --listand choose the closest template. - •Run the scaffold command with
--templateand--output. - •Replace placeholders (
prompt, schema, paths, session names, retries) and keep signatures aligned with the API cheat sheet. - •Run the generated script and inspect
CodexExecResult(return_code,turn_status,stderr,final_message). - •If execution fails, apply the guardrails reference and retry.
Use Bundled Scripts
- •
scripts/preflight_check.py: print readiness checks with optional strict mode. - •
scripts/scaffold_usage_template.py: copy a starter script fromassets/.
Use Bundled Templates
- •
assets/template_sync_run.py - •
assets/template_live_stream.py - •
assets/template_thread_session.py - •
assets/template_resume_named_session.py - •
assets/template_async_run.py - •
assets/template_schema_output.py - •
assets/template_telemetry_and_retry.py
Keep Consumer Guardrails
- •Set
timeout_secondsforrun*andresume*unless intentionally unbounded. - •Set
json_output=Truewhenever you need structured events,thread_id, orturn_status. - •Catch
CodexExecFailedErrorwhenraise_on_error=True; inspectexc.result. - •Pass
session_idorsession_name, never both. - •Treat live methods (
run_live*,resume_live*) as startup-retry only; once a handle is returned, caller code owns interruption/retry logic.