YouTip LogoYouTip

Codex Cli Prompting

Mastering effective prompting techniques is key to making the most of Codex CLI. This section details how to write efficient prompts to achieve the best results. * * * ## Prompting Basics You interact with Codex by sending prompts, describing the tasks you want it to complete. ### Basic Example ## Basic Prompts # Explain code functionality "Explain how the transform module works and how other modules use it." # Add new feature "Add a new command-line option `--json` that outputs JSON." # Fix bug "Fix the error in the login function" ### How Prompts Work When you submit a prompt, Codex works in the following loop: ![Image 1: Codex Prompting Workflow](#) Figure: Codex Prompting Workflow 1. Call the language model 2. Execute the actions indicated by the model's 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, as Codex makes decisions based on context at each 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: ## Prompts with Verification # Not recommended "Write a function to sort a list" # Recommended - Include 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 Down Complex Tasks into Small Steps Codex performs better with smaller, focused tasks. Small tasks are easier to test and review: ## Break Down Tasks # Not recommended - Task is too large "Create a complete user authentication system with login, registration, password reset, and OAuth" # Recommended - Break 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 - Include 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 Output Format Tell Codex the output format you expect: ## Specify Output Format # Specify code style "Write a function to calculate Fibonacci numbers. Use TypeScript, include JSDoc comments, and follow functional programming principles." # Specify 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 prompt plus the subsequent model output and tool calls. A thread can contain multiple prompts. ### Local Threads vs. Cloud Threads | Type | Description | Advantages | | --- | --- | --- | | **Local Thread** | Runs on your machine | Can read and write files, use existing tools | | **Cloud Thread** | Runs in an isolated environment | Parallel execution, delegate tasks from other devices | ### Resume Session ## Resume Previous Session # Open session selector codex resume # Jump directly to the most recent session codex resume --last # Resume a specific session codex resume ### Session Management Running threads can be parallelized, but avoid having two threads modify the same file at the same time. You can also resume a thread later by continuing with another prompt. * * * ## Context Management When you submit a prompt, include context that 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. ### Auto-Compression For longer tasks, Codex may automatically compress the context by summarizing relevant information and discarding less relevant details. Codex can continue processing complex tasks across multiple steps. ### Manual Context Management You can help Codex manage context by: * Focusing on the current task * Avoiding mixing multiple independent tasks in one prompt * Starting new sessions regularly (using `/new`) > When Codex reports high context usage, consider starting a new session or reducing the history messages. * * * ## Advanced Prompting Techniques ### Using Plan Mode For complex tasks, use plan mode to let Codex plan before executing: ## Plan Mode # Use the /plan command /plan Implement user authentication system # Or include "plan" in the prompt "Plan and implement a REST API with CRUD operations for a blog" ### Using Review Mode Ask Codex to review code rather than 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 Modification Scope Explicitly tell Codex what it can modify: ## Limit Modification Scope # Read-only mode "Explain what this function does, don't make any changes" # Restrict file scope "Add logging only to the auth module, don't touch other files" # Step-by-step confirmation "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:- - Include:- Detailed documentation comments- Error handling- Test cases ### Code Review Review the [File/Module] for:- Potential bugs - Security issues - Performance problems - Code style violations Provide:- Issue list with severity - Suggested fixes - Overall quality score (1-10) ### Bug Fix Fix the bug in [File:Line number/Function name].Error: Expected behavior: Steps to reproduce: ### Refactoring Refactor [File/Function] to .Current issues:- - Constraints:- Keep functionality unchanged- Do not change the API interface- Add unit tests > Practice makes perfect. Practice writing prompts more, and you will gradually master how to achieve the best results. * * * ## Frequently Asked Questions ### Q: What should I do if Codex doesn't understand my prompt? Try: 1) Describe it more specifically 2) Provide more context 3) Break it down into smaller steps 4) Use 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 in Chinese, make sure the description is detailed and clear enough. ### Q: How can I make Codex only analyze without modifying code? Explicitly tell it "read-only" or "don't make any changes", and it will only provide analysis without making modifications. ### Q: What should I do when the context window is full? Start a new session (using /new), or let Codex compress the history (it will handle this automatically).
← Codex Cli ModelsCodex Sandbox β†’