Codex CLI Practical Scenarios

📘 Tutorials 2026-07-19 · Updated 2026-11-13 4 min read

Codex CLI's value is the loop: read code, write files, run commands, see errors, fix them. This covers the sandbox and approval switches first, then four copyable scenarios — scaffolding a FastAPI app with JWT auth, root-causing a traceback, a three-round sync-to-async conversion, and read-only review in CI.

📜 Table of Contents

📖 Keep reading

Codex CLI in Practice: Building a FastAPI App with User Auth

Codex CLI is OpenAI's open-source terminal coding agent: you describe a task in a project directory and it reads code, writes files, runs commands, sees errors and fixes them. The difference from completion tools is the loop — you do not copy tracebacks back and forth.

Install and set boundaries first

npm install -g @openai/codex
# or
brew install codex

Two switches matter more than anything else:

Switch Values Meaning
Sandbox read-only / workspace-write / full access Which paths it can write, whether it has network
Approval always ask / ask on failure / never Whether commands need your confirmation

The stable default is "workspace writable, commands require approval". Full access belongs in disposable containers only. Flag names and ~/.codex/config.toml fields have changed across versions — check the official docs.

Hard rule: commit before you let it work. You want a clean git diff and a rollback path.

Scenario 1: generate an authenticated FastAPI project

Do not send a one-liner. Put the acceptance criteria in the prompt:

Create a FastAPI project in an empty directory:
- SQLite + SQLAlchemy 2.x; users table with id/email/hashed_password/created_at
- register, login (JWT), and get-current-user endpoints
- bcrypt hashing, HS256 JWT, secret from env vars
- Pydantic v2 models with email format and minimum password length
- pytest tests: successful register, duplicate email, wrong password
- requirements.txt and .env.example, no real secrets
- run pytest at the end and show the result

Expected layout: app/main.py, models.py, schemas.py, auth.py, deps.py, db.py, plus tests/test_auth.py.

Verify with three commands, not with the agent's summary:

pip install -r requirements.txt
uvicorn app.main:app --reload
pytest -q

Then walk register → login → authenticated request in /docs.

Scenario 2: root-cause a traceback

Here is the production error:
<traceback>

1. Identify the failing path: which line, which input
2. Write a minimal test that reproduces it reliably
3. Fix the root cause, do not just catch the exception
4. Run the tests to confirm

The reproduce-first step is the point. Without it the agent patches symptoms — a try/except that hides the error. With a failing test, the fix is verifiable.

If three rounds fail, question its assumption. Usually it is staring at the last stack frame while the real problem is an upstream data shape.

Scenario 3: sync to async, one step at a time

Split into three rounds: convert I/O boundaries only and list all call sites; add type hints without new dependencies; write pytest-asyncio tests for success and failure paths. Run tests between rounds so a rollback costs one round, not one giant diff.

Scenario 4: read-only review in CI

codex exec --sandbox read-only "Review this diff and output only issues:
1. unhandled exceptions / null paths
2. missing input validation
3. violations of AGENTS.md
Sort by severity. Do not modify code."

Read-only sandbox keeps it from writing into the PR; pipe the output into a comment bot.

AGENTS.md

# Project conventions
- Python 3.11, ruff, line length 100
- New endpoints require pydantic models; no raw dict input
- Migrations via alembic, never edit tables by hand
- Tests in tests/, named test_*.py, no real external services
- Before commit: ruff check . && pytest -q

Skip long product background and anything a linter can enforce. Longer files get ignored more.

Division of labor

Tool Strength Hand it
Codex CLI Bootstrapping projects, command loop, automation New skeletons, environment setup, CI tasks
Claude Code Long-context reading, cross-file refactors Changes in large repos needing wide context
Aider Atomic git commits, batch edits, model choice Controlled refactors, local models

Five traps

  1. No acceptance criteria — "make a login system" versus "bcrypt + JWT + three tests".
  2. Running in a dirty worktree.
  3. Full-access sandbox on a machine with secrets or production credentials.
  4. Ten files in one pass.
  5. Trusting "done" instead of test output.

FAQ

Is an OpenAI account required? The CLI supports account login or an API key, and can be pointed at compatible endpoints (check the docs for config keys). Watch quota with API keys.

Does it install dependencies? It tries. Keep approval on and read what it wants to install.

Can it run unattended on a server? Yes, with read-only or restricted sandbox and structured output. Do not grant write access on machines holding production data.

Is the generated code good enough? Boilerplate usually is; auth, crypto and permission code must be read line by line.

Note: flags, sandbox mode names, config fields and login flow change quickly. Commands here are generic — verify against the official repository and docs.

Related Articles
2026-08-01
Prompt Engineering Guide on GitHub in 2026: 6 Repositories Worth Reading
2026-08-06
Mistral AI vs ChatGPT in 2026: Honest Comparison for Developers
2026-08-01
LLM Jailbreak vs Prompt Injection: The Difference Security Teams Keep Confusing
2026-08-06
Earthly (12,047 Stars) Build Framework 2026: Repeatable Builds with a Familiar Dockerfile Syntax
2026-07-23
LLM Evaluation Metrics Frameworks 2026: 7 Best Tools Compared
2026-08-01
AI Coding Assistants with Free Trials in 2026: 9 Compared

Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.

💬 Comments (0)

No comments yet. Be the first!

Login to comment