Skip to content

Your First Session

In this walkthrough, Codex inspects a CSV and reports which rows fall outside an allowed range. The example gives you a small task with an answer you can check before studying how the harness performs it.

Install Codex CLI and sign in using the official CLI guide. Run codex --version to see your local release. The flags below were checked against CLI 0.172.0.

Use a POSIX shell such as Bash or Zsh with Git installed. Choose a parent directory where you can make a new scratch repository. These commands create the CSV used throughout the guides. If the directory already exists, setup stops before writing any files. Choose a different parent directory for a fresh exercise.

Terminal window
mkdir codex-first-session &&
cd codex-first-session &&
git init &&
cat > scores.csv <<'CSV'
id,score
r1,0.80
r2,1.20
r3,0.40
CSV

Run one non-interactive turn from that directory. The selected sandbox lets the CLI inspect the workspace with read-only access for shell commands. The prompt gives the task and specifies the expected output fields. CLI command reference · Sandbox and approvals

Terminal window
before=$(cksum scores.csv)
prompt='Read scores.csv. List IDs with scores outside the inclusive range [0, 1].
Report the mean of valid scores to two decimal places.
Name the file you inspected and leave all files unchanged.'
codex exec --sandbox read-only "$prompt"
Field Expected value
Input scores.csv
Invalid row IDs r2
Valid scores 0.80 and 0.40
Mean of valid scores 0.60

Compute the mean as (0.80 + 0.40) / 2. Compare the file’s checksum before and after the run.

Terminal window
test "$before" = "$(cksum scores.csv)" && printf 'scores.csv unchanged\n'

A matching checksum checks the sample input. If the answer differs, check which rows were included and ask Codex to show the values used in the calculation.

With Python 3 installed, run a second turn from the same shell with --json to save newline-delimited CLI events outside the scratch repo. The two runs can choose different tools or wording. CLI JSON output

Terminal window
trace_file=$(mktemp) &&
if codex exec --sandbox read-only --json "$prompt" > "$trace_file"; then
python3 - "$trace_file" <<'PYTHON'
import json
import sys
completed = False
with open(sys.argv[1], encoding="utf-8") as trace:
for line in trace:
event = json.loads(line)
kind = event["type"]
if kind == "error":
print(event.get("message", "CLI error"), file=sys.stderr)
elif kind == "turn.failed":
raise SystemExit(f"Incomplete trace ({kind}): {sys.argv[1]}")
if kind == "turn.completed":
completed = True
elif kind == "item.completed":
item = event["item"]
if item.get("type") == "command_execution":
print(item["status"], item["exit_code"], item["command"])
elif item.get("type") == "agent_message":
print(item["text"])
if not completed:
raise SystemExit(f"Missing turn.completed: {sys.argv[1]}")
print("turn.completed")
PYTHON
else
codex_exit_code=$?
printf 'Codex exited %s. Captured stdout: %s\n' "$codex_exit_code" "$trace_file" >&2
(exit "$codex_exit_code")
fi

The block preserves a nonzero Codex exit status and reports the path to the captured stdout. A retry can emit an error diagnostic and still complete. The parser prints that diagnostic, checks for turn.failed, and requires a final turn.completed for this single-turn exercise. CLI error handling · JSON event emission An item.completed event can describe a failed command, so check its status and exit_code. If the model used no shell command, inspect the final message and compare it with the expected answer. These are CLI events for the client. The streaming chapter traces the separate provider stream. Pinned CLI event types · Command item fields

Evidence. The scratch setup, checksum, and expected CSV calculation were checked locally. Bash and Zsh checks used synthetic traces for a recovered error, a missing completion, and a failed turn. A fourth check verified that the block preserves a nonzero CLI exit. The example prompts are for your signed-in Codex CLI session.

Continue with Project Instructions to make the validation rule reusable, or read One Turn, One Tool Call to trace a command result through the Rust implementation.