Write better LLM prompts for AI code generation

Transform vague AI prompts into detailed instructions that generate production-ready code by setting clear context, standards, and expectations for the LLM.

M Bytes Newsletter
Get the developer newsletter

    Fresh bytes every Thursday. No spam, ever. Unsubscribe anytime.

    Join 9,000+ developers and get three free video lessons every week.

    Let's look at a common prompt developers write:

    Write me a Laravel controller for user registration

    This vague prompt usually returns mediocre results - maybe a controller with some docblocks, oddly named functions, and basic JSON endpoints.

    But what if you wanted a web form instead? The AI can't read your mind, but we can help it understand exactly what we need.

    Here's how I structure my prompts to get consistently better results, and this works whether you are using Laravel, Django, Rails, or some other framework:

    You are a Laravel expert who writes simple, idiomatic code. You prefer straightforward solutions and self-documenting code over complexity.
     
    VERSIONS:
     
    - PHP 8.3
    - Laravel 11
     
    CODING STANDARDS:
     
    - Modern PHP features (type hints, return types, null safe operators, enums, etc.)
    - Constructor property promotion
    - Standard Laravel patterns and Blade components (<x-app-layout>)
    - Simple Alpine.js for JavaScript (only when absolutely necessary)
    - Proper input validation and error catching
    - No Livewire
     
    OUTPUT REQUIREMENTS:
     
    - Full file paths before all code
    - Complete, production-ready code (no truncation)
    - For small changes (≤5 lines), specify exact location and code
     
    When thinking through complex solutions, wrap your thought process in <thinking> tags.
     
    Now, write me a Laravel controller for user registration including form submissions, error validation, and all of the related backend processes.

    Let's break down why this works better.

    First, that opening line about being a Laravel expert who prefers simple solutions immediately establishes the coding philosophy we want. This helps avoid over-engineered responses right from the start.

    By specifying exact versions, we ensure we get code that uses the latest features we have access to. This is crucial - if you're working with older versions, specify those instead to avoid getting code that won't work in your environment.

    Think of the coding standards section like briefing a new team member. We're telling the AI exactly how we write code, what features we prefer, and what to avoid. This helps ensure the code we get back feels like it was written by someone who already knows our team's practices.

    The output format requirements might seem pedantic, but they're actually huge time-savers. By being explicit about wanting full file paths and complete code, we can copy the results directly into our project without having to guess where files should go or fill in missing pieces.

    Those <thinking> tags implement chain-of-thought prompting, which is a game-changer for complex features.

    Pro tip: Don't skip those tags. They're particularly valuable for complex features. You can see exactly how the AI decides to approach the problem, and if it starts heading in the wrong direction, you can adjust the prompt to get it back on track.

    Finally, notice how the closing request explicitly mentions "form submissions" and "backend processes." This precision helps avoid getting an API endpoint when you wanted a web form. Being specific about your exact needs makes a huge difference in getting useful results.

    This structured approach consistently generates better code that matches your actual needs. The key is context - the more specific you are about your requirements, coding standards, and desired output, the better results you'll get.

    Don't be afraid to write very detailed prompts. Taking an extra minute to specify exactly what you want can save you lots of time debugging or rewriting AI-generated code later.