Codex: Codex Prompt Best Practices

Last updated: 2026-08-31

Prompts are your bridge to Codex. Good Prompts get Codex right the first time; bad ones require repeated corrections.

📋 Prerequisites: Understanding Codex basic operations

1. What You Will Learn


2. Core Principles

(1) The CLEAR Principle

Principle Description Example
Context Provide context "Follow the style of auth.py"
Limit Define scope "Only modify src/api/ directory"
Example Give examples "Output format following types.ts"
Assert Include verification "Ensure npm test passes"
Reason Explain why "Because Unicode support is needed"

(2) Good vs Bad Comparison

TEXT 📖 Display only
# Bad Prompt
"Help me write a function"

# Good Prompt
"In src/utils/date.ts, create a formatDate function,
input: Date object and locale string,
output: localized date string,
follow the style of formatNumber in the same file,
include null handling and unit tests."

3. Structured Description

(1) Standard Structure

TEXT 📖 Display only
[Goal]: What to accomplish
[Scope]: Which files/directories to modify
[Reference]: Which existing code to reference
[Constraints]: What rules to follow
[Verification]: How to verify completion

▶ Example 1: Alice's Structured Prompt

TEXT 📖 Display only
[Goal] Add user password reset feature
[Scope] src/auth/reset.ts, src/api/reset.ts
[Reference] Code style of src/auth/login.ts
[Constraints] Use bcrypt hashing, token expires in 1 hour
[Verification] npm test -- --grep "reset" all pass

(2) Step-by-step Instructions

For complex tasks, describe in steps:

TEXT 📖 Display only
Step 1: Create PasswordReset model with token, expires_at, user_id fields
Tell me when done, I'll confirm before continuing.

Step 2: Implement POST /api/auth/reset-request endpoint
Step 3: Implement POST /api/auth/reset-confirm endpoint
Step 4: Write integration tests for all endpoints

4. Context Provision Techniques

(1) Precise References

TEXT 📖 Display only
# Good: reference specific files
> Follow the User type definition in src/models/user.ts, add Profile type

# Bad: vague description
> Follow existing models, add new ones

(2) Attach Supplementary Info

TEXT 📖 Display only
# Attach images in the App
> Implement this page following the UI layout in the screenshot

# Reference error logs
> Output from cat error.log:
> TypeError: Cannot read property 'id' of undefined
> Fix this error

(3) Environment Description

TEXT 📖 Display only
> Project uses Next.js 14 + App Router + TypeScript
> Database uses Prisma ORM + PostgreSQL
> Test framework is Vitest
> Add CRUD functionality for the new page

5. Verification Instructions

(1) Test Verification

TEXT 📖 Display only
# Require running tests
> Ensure npm test passes
> Run pytest, fix all failing cases
> Test coverage no less than 80%

(2) Type Checking

TEXT 📖 Display only
> Ensure tsc --noEmit has no errors
> Add complete TypeScript type annotations

(3) Lint Checking

TEXT 📖 Display only
> Ensure eslint has no errors
> Fix all lint warnings

▶ Example 2: Bob's Complete Verification

TEXT 📖 Display only
Add pagination to the orders API:
1. Modify GET /api/orders endpoint
2. Support page and pageSize query parameters
3. Return pagination metadata
4. Ensure tsc --noEmit has no errors
5. Ensure eslint has no errors
6. npm test all pass
7. New tests covering pagination logic

6. Common Prompt Templates

(1) Fix Bug

TEXT 📖 Display only
Fix <Bug-description> in <file>.
Error message: <paste-error-log>
Expected behavior: <correct-behavior>
Ensure <verify-command> passes.

(2) Add Feature

TEXT 📖 Display only
Add <feature> to <module>.
Follow the code style of <existing-file>.
Include input validation and error handling.
Write unit tests, ensure <verify-command> passes.

(3) Refactor Code

TEXT 📖 Display only
Refactor <old-impl> to <new-impl>.
Maintain the external interface.
Ensure all existing tests pass.
Add new tests covering the new implementation.

(4) Code Review

TEXT 📖 Display only
Review <file/PR>, focusing on:
1. Security vulnerabilities (SQL injection, XSS, etc.)
2. Performance issues (N+1 queries, memory leaks, etc.)
3. Code style consistency
4. Error handling completeness
5. Test coverage

(5) Documentation Generation

TEXT 📖 Display only
Generate documentation for <module/API>:
- API interface docs (Markdown format)
- Usage examples (including curl commands)
- Parameter description table
- Error code list

7. Advanced Techniques

(1) Role Setting

TEXT 📖 Display only
You are a senior security engineer specializing in Web security.
Review the following code for security vulnerabilities, categorized by OWASP Top 10.

(2) Output Format Control

TEXT 📖 Display only
Output in the following format:
1. One issue per line
2. Format: [Severity] file:line - Issue description
3. Severity: 🔴Critical 🟡Medium 🟢Low
4. Finally, provide a fix priority ranking

(3) Constraint Conditions

TEXT 📖 Display only
Only use dependencies already in the project, do not install new packages.
Do not modify .env files.
Do not delete existing tests.
All new functions must have TypeScript type annotations.

❓ FAQ

Q Should Prompts be in English or another language?
A Codex supports multiple languages, but English usually works better. Complex tasks should use English; simple tasks can use your preferred language.
Q Do long Prompts affect results?
A No. Detailed descriptions work better than brief ones. Just be careful not to exceed the context window.
Q Can I give multiple tasks at once?
A Yes, but order by priority or clearly specify step-by-step execution. Too many tasks at once may cause Codex to miss some.
Q How do I make Codex follow specific code styles?
A Define style rules in AGENTS.md, or reference style files in the Prompt. You can also reference ESLint/Prettier configs.
Q Can I reference URLs in Prompts?
A Codex can't directly access URLs, but you can copy key content from URLs into your Prompt.

📖 Summary


📝 Exercises

  1. Basic (⭐): Write 3 Prompts for different scenarios using the CLEAR principle.
  2. Intermediate (⭐⭐): Create your own Prompt template library covering 5 common scenarios.
  3. Advanced (⭐⭐⭐): Compare the same task using a "good Prompt" vs "bad Prompt," write an analysis report on the execution differences.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏