ChatGPT · Coding

Code generation

Get functions from exact signatures and examples, code that follows your conventions, tests alongside it, and builds in reviewable pieces.

Generating code with ChatGPT

Pin down what you want before it writes

ChatGPT will always produce something, so the risk isn't an empty answer. The risk is reasonable-looking code built on a choice you never made, like a different library or error style. Each card here fixes one of those gaps: your stack, your function's contract, your size of step and your tests. OpenAI's Prompting guide says a useful Codex prompt names the behavior you want, points to the relevant code, keeps your constraints, and says how to verify the change.

Save the context you'd otherwise repeat

If you code in Chat, put your stack in project instructions and upload your dependency file to the project's Sources. In Codex, /init creates an AGENTS.md file, which Codex reads before starting work. Keep it short, because OpenAI's usage tips include reducing its size, and combined instruction files are capped at 32 KiB by default. Before a larger change, use /plan so Codex investigates and proposes an approach first, then review the diff with /review.

Frequently asked
How do I get ChatGPT to write code that fits my project?

State your language, versions, libraries and conventions, and point to one example of good code. Save this as project instructions in Chat or as an AGENTS.md file in Codex, so that you don't have to repeat it.

What is AGENTS.md?

It's a file of instructions that Codex reads before it starts work. It usually holds build and test commands, review expectations and repo conventions. You can have a global one in ~/.codex and others in your repository, with files closer to your working folder taking precedence.

Should I generate a whole app with ChatGPT at once?

Not for code you plan to keep. Ask for a plan first, then build one piece you can test before starting the next. OpenAI advises starting with a manageable assignment and expanding gradually. A throwaway prototype is the main exception.

Can ChatGPT write unit tests?

Yes. Name the edge cases yourself, and ask for the tests alongside the code. In Chat, you'll need to run them yourself. In Codex, ask it to run them and report the commands and results, then check that the tests fail when the code is wrong.