AI Prompt Writing Guide

2024-03-20By Cursor Team

Prompt Writing Basics

Writing effective prompts is key to getting the best results from Cursor's AI features. A good prompt should be:

  • Clear and specific
  • Contextually relevant
  • Properly formatted
  • Focused on a single task

Prompt Structure

An effective prompt typically includes these components:

1. Task Description
   - What you want to accomplish
   - Specific requirements or constraints

2. Context
   - Relevant code or background information
   - Programming language or framework

3. Expected Output
   - Format of the desired result
   - Any specific requirements

4. Additional Parameters
   - Performance considerations
   - Style preferences
   - Error handling requirements

Providing Context

Help the AI understand your codebase by:

  • Including relevant code:
    • Function definitions
    • Related classes or modules
    • Important dependencies
  • Specifying technical details:
    • Programming language version
    • Framework constraints
    • Platform requirements

Example Prompts

Here are some effective prompt examples:

Code Explanation Request:

"Explain this React useEffect hook:
[code block]
Focus on:
1. Dependencies array usage
2. Cleanup function
3. Potential side effects"

Code Generation Request:

"Create a TypeScript function that:
- Validates an email address
- Returns a strongly typed result
- Includes error messages for invalid cases
- Uses regular expressions
- Has JSDoc documentation"

Refactoring Request:

"Refactor this function for better performance:
[code block]
Requirements:
- Reduce time complexity
- Maintain readability
- Add error handling
- Keep existing functionality"

Common Tasks

Effective prompts for common development tasks:

  • Debugging:
    • Include error messages
    • Describe expected behavior
    • List attempted solutions
  • Testing:
    • Specify test framework
    • Define test cases
    • Include edge cases
  • Documentation:
    • Specify documentation style
    • List required sections
    • Include example usage

Best Practices

Follow these guidelines for better results:

  • Be specific and clear in your requests
  • Break down complex tasks into smaller parts
  • Provide relevant context and constraints
  • Use appropriate technical terminology
  • Specify output format preferences
  • Review and iterate on responses

Troubleshooting

If you're not getting the desired results:

  • Unclear Responses:
    • Add more specific requirements
    • Break down the request into steps
    • Provide examples of desired output
  • Incorrect Solutions:
    • Check if all context is provided
    • Verify technical requirements
    • Clarify constraints and limitations
  • Performance Issues:
    • Reduce prompt complexity
    • Focus on specific aspects
    • Use step-by-step approach