YouTip LogoYouTip

Codex Prompting

Master prompt engineering techniques to help Codex understand your intent more accurately and complete tasks efficiently. * * * ## Basic Prompt Structure A good prompt contains the following elements: ### Basic Elements | Element | Description | Example | | --- | --- | --- | | **Task Description** | What you want to do | "Implement user login functionality" | | **Context** | Relevant background information | "Use existing auth module" | | **Constraints** | Limitations and requirements | "Must be compatible with existing API" | | **Expected Result** | Specific output | "Return JWT token" | ### Structured Prompt ## Complete Prompt Example # Task Description Implement a user login API endpoint # Context - Project uses FastAPI framework - Existing auth module handles authentication logic - Database uses PostgreSQL # Constraints - Use existing JWT utility to generate tokens - Password verification using bcrypt - Request logging required # Expected Result - Path: POST /api/auth/login - Request body: {"email": "...", "password": "..."} - Success response: {"token": "...", "user": {...}} - Failure response: {"error": "..."} * * * ## Task Decomposition Principles Complex tasks should be broken down into small steps and completed gradually. ### Decomposition Principles * Each step has a clear objective * Dependencies between steps are clear * Verify results after each step * Can rollback when issues occur ### Decomposition Example ## Task Decomposition # Large Task "Implement a complete user authentication system" # Break down into small tasks Step 1: "Design data structures for authentication system, including user table and token table" Step 2: "Implement user registration functionality, including password encryption and verification" Step 3: "Implement user login functionality, generate JWT token" Step 4: "Implement token verification middleware" Step 5: "Implement user logout functionality" Step 6: "Write tests for authentication system" Step 7: "Update API documentation" > Use /plan mode to help Codex automatically decompose complex tasks. * * * ## Providing Context Information Sufficient context helps Codex understand project status more accurately. ### Key Context | Type | Description | When to Provide | | --- | --- | --- | | **Tech Stack** | Framework, language, library versions | First task or new module | | **Project Structure** | Module locations, naming conventions | Involves multi-file modifications | | **Existing Code** | Related functions, classes, modules | Need compatibility or extension | | **Constraints** | Compatibility, performance requirements | Special limitations exist | ### Context Provision Methods ## Providing Context # Direct Description "Project uses Django 4.2, database is PostgreSQL 15" # Reference Existing Code "Refer to validation logic in src/utils/validator.py" # Specify Files "Modify src/api/users.py, keep consistent style with other APIs" # AGENTS.md Auto-Provision Create AGENTS.md file, Codex will automatically read project specifications * * * ## Error Information Handling Codex can quickly locate issues based on error information. ### Provide Complete Error Information ## Error Handling # Provide Complete Error "Error occurred when running tests: AssertionError: Expected status 200 but got 500 at test_login.py:45 in test_successful_login Full traceback: ... Analyze cause and fix" # Screenshot Assistance Attach error screenshot, Codex can view directly ### Error Information Elements * Error type and message * Complete stack trace * Trigger conditions (what operation was performed) * Expected result vs actual result * * * ## Image Input Codex supports image input, suitable for visual information transfer. ### Applicable Scenarios | Scenario | Description | | --- | --- | | **Error Screenshot** | Screenshot showing error interface or logs | | **Design Mockups** | Convert UI design mockups to code | | **Architecture Diagrams** | Explain architecture diagrams and implement | | **Data Charts** | Analyze chart data | ### Image Input Methods ## Using Images # CLI codex -i screenshot.png codex --image design.png "Implement page based on design mockup" # App/IDE Paste or drag and drop images directly into chat area # Multiple Images codex -i img1.png -i img2.png "Compare differences between these two design mockups" * * * ## Iterative Optimization Gradually improve code quality through iteration. ### Iteration Process 1. Codex completes initial implementation 2. Check results, propose improvement points 3. Codex adjusts based on feedback 4. Verify improvement effects 5. Repeat until satisfied ## Iterative Optimization # Initial Implementation "Implement user list API with pagination support" # Propose Improvements After Checking "Add sorting functionality, support sorting by creation time" # Continue Optimizing "Add search functionality, support searching by username" # Performance Optimization "Optimize query performance, add indexes" * * * ## Avoiding Ambiguity Clear prompts reduce misunderstandings and rework. ### Ambiguity Examples ## Avoid Ambiguity # Vague Prompt (Bad) "Modify that function" # Clear Prompt (Good) "Modify the format_date function in src/utils/helper.py" # Vague Prompt (Bad) "Make it faster" # Clear Prompt (Good) "Optimize query_users function, reduce query time from 500ms to within 100ms" # Vague Prompt (Bad) "Add a feature" # Clear Prompt (Good) "Add batch delete functionality to user API, receiving list of user IDs" * * * ## Using Reference Examples Provide reference code or documentation to help Codex understand expected style. ## Providing References # Reference Existing Code Style "Refer to style in src/api/products.py, implement user API" # Reference Documentation "Implement according to OpenAPI specification document docs/api-spec.yaml" # Reference Example "Return response in this format: { 'success': true, 'data': {...}, 'message': '...' }" * * * ## Common Prompt Templates ### Feature Development Template ## Feature Development Add functionality in Requirements: - Use [technology/library] - Compatible with - Include - Return Reference: ### Bug Fix Template ## Bug Fix Problem Description: Trigger Conditions: Error Information: Expected Behavior: Please analyze cause and fix, without affecting other functionality. ### Code Review Template ## Code Review /review for # Example /review src/auth/for security issues /review HEAD~5..HEAD for performance regressions * * * ## Best Practices Summary ### Core Principles * Be clear and specific, avoid vague expressions * Provide sufficient context * Decompose complex tasks for execution * Iterate and optimize, gradually improve * Verify results, ensure correctness ### Efficiency Tips * Use AGENTS.md to reduce repetitive explanations * Utilize Skills to encapsulate common prompts * Use images to convey visual information * Provide complete error information when reporting errors * * * ## Frequently Asked Questions ### Q: What if Codex misunderstands? Provide more explicit descriptions, supplement context, or use reference examples. ### Q: How to make Codex follow project specifications? Create AGENTS.md file to define specifications, Codex will automatically follow them. ### Q: How to handle complex tasks? Use /plan mode to create a plan, execute step by step and verify. ### Q: What are the limitations of image input? Supports PNG, JPG, WebP formats, recommended moderate resolution (not too small or too large).
← Codex ModelsCodex Automation β†’