Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Codex CLI Prompting Tips

Codex CLI Prompting Tips

Mastering effective prompting techniques is key to making the most of Codex CLI. This section explains in detail how to write efficient prompts for the best results.


Prompt fundamentals

You interact with Codex by sending a prompt that describes the task you want it to complete.

Basic Example

Basic prompts

Explain what the code does

"Explain how the transform module works and how other modules use it."

Add a new feature

"Add a new command-line option --json that outputs JSON."

Fix a bug

"Fix the error in the login function"

How prompts work

When you submit a prompt, Codex works in the following loop:

Codex Prompt Workflow
Figure: Codex prompt workflow
  1. Call the language model
  2. Perform the actions indicated by the model output (read files, edit files, run tools, etc.)
  3. Repeat until the task is complete or you cancel

Understanding this loop helps you design better prompts, because Codex makes decisions based on context at every step.


Tips for writing effective prompts

Tip 1: Include verification steps

Codex produces higher-quality output when it can verify its work. Including verification steps helps Codex ensure correctness:

A prompt with verification

Not recommended

"Write a function to sort a list"

Recommended - includes verification

"Write a function to sort a list. Include test cases to verify it handles empty lists, already-sorted lists, reverse-sorted lists, and lists with duplicates. Run the tests to confirm they pass."

Tip 2: Break complex tasks into small steps

Codex performs better on smaller, focused tasks. Small tasks are easier to test and review:

Split the task

Not recommended - task too large

"Create a complete user authentication system with login, registration, password reset, and OAuth"

Recommended - broken into small steps

"Step 1: Create a user model with email and password fields
Step 2: Add a registration endpoint
Step 3: Add a login endpoint with JWT
Step 4: Add password reset functionality
Let me know when Step 1 is done before moving to Step 2"

Tip 3: Provide context

Provide context that Codex can use, such as references to relevant files and images:

Provide context

Not recommended

"Fix this bug"

Recommended - includes detailed information

"Fix the bug in src/auth.py where users can't log in with special characters in their password. I've attached a screenshot of the error message and the relevant code section."

Tip 4: Specify the output format

Tell Codex the output format you expect:

Specify the output format

Specify the code style

"Write a function to calculate Fibonacci numbers. Use TypeScript, include JSDoc comments, and follow functional programming principles."

Specify the output structure

"Explain the codebase structure. Organize your answer with: 1) Overview 2) Key directories 3) Main entry points"

More specific prompts generally produce better results. Codex is good at understanding intent, but the more information you provide, the better it performs.


Threads

A thread is a single session: your prompts plus the subsequent model outputs and tool calls. A thread can contain multiple prompts.

Local threads vs. cloud threads

Type Description Advantage
Local threads Run on your machine Can read and write files and use existing tools
Cloud threads Run in an isolated environment Run in parallel and delegate tasks from other devices

Resume the session

Resume a previous session

Open the session picker

codex resume

Jump directly to the most recent session

codex resume

--last

Resume a specific session

codex resume

<

SESSION_ID

Session management

Running threads can operate in parallel, but avoid having two threads modify the same file at once. You can also resume a thread later by continuing with another prompt.


Context management

When you submit a prompt, include context Codex can use, such as references to relevant files and images. The IDE extension automatically includes the list of open files and selected text ranges as context.

Context window

All information in a thread must fit within the model's context window. Codex monitors and reports the remaining space.

Automatic compaction

For longer tasks, Codex may automatically compact the context by summarizing relevant information and discarding less relevant details. Codex can continue working on complex tasks across multiple steps.

Manage context manually

You can help Codex manage context in the following ways:

When Codex reports high context usage, consider starting a new session or reducing the message history.


Advanced prompting techniques

Use plan mode

For complex tasks, use plan mode to have Codex plan before executing:

Plan mode

Use the /plan command

/

plan Implement a user authentication system

Or include "plan" in the prompt

"Plan and implement a REST API with CRUD operations for a blog"

Use review mode

Ask Codex to review code instead of modifying it directly:

Review mode

Use the /review command

/

review src

/

auth.py

Or describe the review requirements

"Review this code for potential security vulnerabilities and suggest improvements"

Limit the scope of modifications

Tell Codex explicitly what it may modify:

Limit the scope of modifications

Read-only mode

"Explain what this function does, don't make any changes"

Restrict the file scope

"Add logging only to the auth module, don't touch other files"

Confirm step by step

"Let me know the proposed changes before you make them"


Prompt templates

Here are some commonly used prompt templates:

Code generation



Write a [语言] function that [功能描述].

Requirements:

- [需求1]

- [需求2]

Include:

- 详细的文档注释

- 错误处理

- 测试用例

Code review



Review the [文件/模块] for:

- Potential bugs

- Security issues

- Performance problems

- Code style violations



Provide:

- Issue list with severity

- Suggested fixes

- Overall quality score (1-10)

Bug fixes



Fix the bug in [文件:行号/函数名].

Error: [错误信息]

Expected behavior: [期望行为]

Steps to reproduce: [复现步骤]

Refactor



Refactor [文件/函数] to [目标].

Current issues:

- [问题1]

- [问题2]



Constraints:

- 保持功能不变

- 不要改变 API 接口

- 添加单元测试

Practice makes perfect. The more you practice writing prompts, the better you'll get at achieving the best results.


Common questions

Q: What if Codex doesn't understand my prompt?

Try: 1) describing it more specifically 2) providing more context 3) breaking it into smaller steps 4) using examples to illustrate the expected output

Q: Should prompts be in English or Chinese?

Codex supports multiple languages, but English usually works best. If you describe things in Chinese, make sure the description is detailed and clear enough.

Q: How do I make Codex analyze only, without modifying code?

Explicitly tell it "read-only" or "don't make any changes", and it will only provide analysis without modifying.

Q: What if the context window is full?

Start a new session (using /new), or let Codex compact the history (it handles this automatically).