Learn · Codex

How to prompt Codex

Codex is OpenAI's coding agent. It can read your files, run commands in a sandbox and edit code. A useful prompt names the behaviour you want, points to the relevant code or the steps that reproduce a problem, keeps your constraints, and says how to verify the change. Here is how OpenAI's official guide puts it.

How Codex reads a prompt

  • Name the behaviour, then how to check it. OpenAI's guide says a useful Codex prompt "names the behavior you want, points to the relevant code or reproduction steps, preserves important constraints, and says how to verify the change."
  • Goal, context, constraints, done when. A good default, from the official best-practices guide, is four parts: the goal (what you want to change or build), the context (which files, folders, documents or errors matter), the constraints (standards or conventions to follow), and "done when" (what should be true before the task is complete, such as tests passing or a bug no longer reproducing).
  • Plan first for bigger jobs. For a multi-step or unclear task, type /plan so Codex investigates and proposes an approach before it edits anything. When Goal mode is available, you can use /goal after the plan to set a persistent goal.
  • Reproduction steps beat a vague report. When you want a bug fixed, the reproduction steps and the constraints matter more than a high-level description. Ask Codex to reproduce the problem first, then propose a patch and run checks. After the fix, ask it to run lint and the smallest relevant test suite, and to report the commands and the results.
  • Point to the files it should see. The IDE extension already includes the files you have open. In the command-line tool, mention paths explicitly, or attach them with @ or /mention.
  • It works in a sandbox. Codex runs local commands inside a sandbox that limits file and network access. If a task needs to cross that boundary, it follows your approval policy before continuing.
  • Ask for a review before you commit. Use /review for a second look at your local changes before you commit or open a pull request. On GitHub, when code review is enabled, you can comment @codex review on a pull request, and add a focus such as edge cases or security.
  • Standing rules go in AGENTS.md. Codex reads AGENTS.md files before it does any work. Put recurring rules there — how to run tests, your conventions, and what "done" means — instead of repeating them in every prompt. There can be a global file in ~/.codex/ and one per project; files closer to the folder you work in override the ones above them. Keep it short and accurate. The /init command can draft a starter file, which you should then edit to match how you actually work.

A structure that works

You don't need every part every time. For a small, obvious change, one clear sentence plus a test to run can be enough. For work that matters, this shape covers what OpenAI's guide recommends: the goal, the relevant files or reproduction steps, the constraints, and what "done" looks like. If the job is large or unclear, start with /plan before you ask it to edit anything.

Goal: [the behaviour I want, or the bug I want fixed]

Context: [paths, files, or the exact steps that reproduce the problem]

Constraints: [what must stay unchanged]

Done when: [how to verify: tests to run, or the behaviour I should see]

After you finish, report the commands you ran and their results.

Six prompts to copy

Replace the [brackets] with your own details. Each prompt shows one of the ideas above.

Fix a bug you can reproduce

Reproduction steps and constraints, then a verification step.

Bug: The contact form on /contact shows "Message sent" but no email arrives.

Repro:
1) Start the app: [command, e.g. npm run dev]
2) Open /contact
3) Fill in the name, email and message fields
4) Click Send
5) The page shows "Message sent", but nothing arrives at [destination inbox]

Constraints:
- Don't change the form's fields or public URLs.
- Keep the fix small and add a test if you can.

Start by reproducing the bug locally, then propose a patch and run checks. After the fix, run lint and the smallest relevant test suite. Report the commands and the results.

Add a function, with a test

Name the behaviour, point to the files, and say what "done" means.

Goal: Add monthly_totals() to [path, e.g. reports/sales.py] that reads [path, e.g. data/orders.csv] and returns the total per month.

Context: @[path] @[path]
The CSV columns are date, customer, amount.

Constraints: Keep the CSV format. Follow the conventions used in the other functions in that file.

Done when: There is a test with three sample rows, the tests have been run, and you have reported the commands and the results.

Explain how the code works

Point to the files, and ask for something you can check.

Goal: Explain how a request flows through this code.

Context: I have the relevant files open. (Or, in the command-line tool: read @[file] and @[file].)

Output:
- a short summary of what each module involved is responsible for
- what data is validated, and where
- one or two things to watch for when changing this

Then summarise the flow as a numbered list of steps, and list the files involved.

Update the docs, then check the links

A documentation change still needs a way to verify it.

Goal: Update the [section name] documentation in @[path] to include [what is missing, e.g. troubleshooting for failed payments].

Constraints: Match the tone and structure of the existing docs. Don't invent features that aren't in the code.

Done when: You have checked that every link on the page is valid, and you have told me which file changed.

A small, specific change

Iterate in small prompts. If you revert an edit, say so.

Goal: Improve only the header on the landing page.

Context: The page is in @[path]. I want it to look calmer on a phone as well as on a wide screen.

Constraints:
- Change only the header.
- Keep the current layout and brand colours.
- If I revert an edit, don't put it back in the next change.

Done when: The header still reads clearly at a mobile width, and you have told me what you changed.

Draft a short AGENTS.md

Standing rules belong in AGENTS.md, not in every prompt.

Goal: Draft a short AGENTS.md for this project, then stop so I can edit it.

Context: Look at how the repo is laid out, and at the scripts in package.json, the Makefile or pyproject.toml.

Output: A short file that covers:
- how to install and run the project
- the commands for lint, tests and a production build
- any conventions you can see in existing code
- what "done" should mean for a typical change

Constraints: Keep it practical and short. Don't invent commands that aren't in the repo. I'll edit this before we use it as standing rules.

Done when: You have written the draft and listed anything you weren't sure about.

Free PDF cheat sheet

The same task, for ChatGPT, Claude and Codex

Codex is one of three. Our free four-page cheat sheet puts the three side by side, with a template for each and an example AGENTS.md for Codex.

Download the free PDF ↓More free prompts

Guides to practise with

Our written guides use ChatGPT in the titles, because they started as copy-and-paste prompts. When the job involves a script, a spreadsheet or a change in your code, you can hand the same task to Codex with the shape above.

Paid prompt packs

Go further

Found the free material useful? Choose a pack for the task you want to work on next. Open a product to read its contents and samples before you decide.

Browse all categories →