ChatGPTCode generationLevel 2: Context

How Do You Get ChatGPT to Write Code That Fits Your Project?

State your language, versions and conventions before asking for code, and save them so you don't have to repeat them. In Chat, use project instructions and upload your config files. In Codex, put them in an AGENTS.md file at the root of your repo.

Prompt to copy
For Chat, paste this as project instructions. For Codex, save it as AGENTS.md at the repo root:

# Stack
[Language and version], [framework and version], [database], [package manager].

# Commands
- Test: [command]
- Lint and typecheck: [command]
- Run locally: [command]

# Conventions
- Errors: [how your code handles them, such as raise custom exceptions, never return null]
- Tests: [framework], next to the source as [naming pattern]
- Don't add a new production dependency without asking.
- Follow the patterns in [path to a good example file].

# When you change code
Run the tests and lint, and report the results. If you couldn't run something, say so.
  1. 1Global style~/.codex/AGENTS.md or custom instructions
  2. 2This repoAGENTS.md or project instructions
  3. 3This taskThe prompt
Where to keep each layer. A rule of thumb, based on OpenAI's AGENTS.md and Projects pages.

Try it now

  1. 1In Codex, run /init in your repo to create a starter AGENTS.md, then trim it and edit it against the template above. In Chat, open **Projects** and add the same text as project instructions.
  2. 2Under the project's Sources, upload your dependency file, such as package.json or pyproject.toml, plus one example of good code.
  3. 3Ask for a small function in a new chat. It worked if it uses your libraries and your error style, with no extra prompting.

Use it when

Any time the code has to live alongside existing code. It matters most when your stack is recent or unusual, or when your team has rules that a general answer wouldn't guess, like "use make test-payments, not npm test."

Skip it when

A language-agnostic question like "explain how a hash map works" doesn't need your stack. Also skip long style essays in AGENTS.md. Rules your linter already enforces only take up room and add to every message's context, so run the linter and let it speak.

Why it works

Without your stack, ChatGPT has to pick one, and its pick may not match your framework version, libraries or error handling. In Chat it can't see your repository, since OpenAI says ChatGPT projects have no direct folder access. Project instructions, by contrast, apply to every chat in the project, and items in Sources are available across them.

Codex reads AGENTS.md files before starting work. OpenAI's page says Codex checks your global file in ~/.codex first, then walks from the project root down to your current folder. Files closer to where you're working come later, so they take precedence. OpenAI's customization overview names the things that belong there: build and test commands, review expectations and repo conventions.

That also means the file costs you something. OpenAI's pricing FAQ lists reducing the size of your AGENTS.md among its tips for making usage limits last.

Example

Your app is Next.js with TypeScript in strict mode, Zod for validation and Vitest for tests. Asked plainly for an API route, ChatGPT might give you older patterns and a different validation library. With your package.json in Sources and a line saying "validate inputs with Zod, never trust req.body", you start from code that fits. Pointing at one existing route file usually does more than three paragraphs describing your style.

Going further

To check what Codex actually loaded, OpenAI's page suggests codex --ask-for-approval never "Summarize the current instructions." from the repo root, or codex --cd services/payments with "List the instruction sources you loaded" for a nested folder. Combined files are capped by project_doc_max_bytes, 32 KiB by default, and Codex stops adding files at the limit. So a bloated root file can silently keep your nested rules out. Split guidance by folder. Each directory contributes at most one file, and AGENTS.override.md wins over AGENTS.md in the same directory, which is useful for a temporary rule until you remove it.

Common mistakes

  • Saying "React" without a version, then getting patterns your version doesn't support.
  • Writing conventions in Chat and assuming Codex knows them. Codex reads AGENTS.md and repo docs, so keep the guidance where it will actually run.
  • Letting AGENTS.md go stale. If the test command changed, Codex will keep running the old one.
  • Repeating linter rules in instructions, which spends context on things a tool can check for free.

Sources

Checked 6 November 2026. Products change often, so confirm details with the vendor.

Related

Beyond the course