AI Coding Prompts Explained in 10 Minutes
When you're coding with AI, the way you word your prompts can be the difference between building a full-stack application in under an hour and troubleshooting for multiple hours with no progress. After spending extensive time coding with AI, I've gathered numerous lessons and specific prompts that can significantly improve your workflow.
Crafting Prompts for New Code
When it comes to writing new code, a few key strategies can ensure you get high-quality, iterative results.
1. Specify Your Tech Stack
Always provide a specific list of technologies or approaches you want the AI to use. This ensures that iterating on the code is straightforward and that all the tools and technologies make sense together. Using specific technologies also signals a level of expertise to the AI, which often results in higher-quality output.
Lean heavily towards popular languages and frameworks like JavaScript and Python, and common databases like PostgreSQL. AI models are more familiar with these, leading to better code. Additionally, you'll benefit from a larger community and more online examples.
2. Design Before You Prompt
Instead of giving a completely open-ended prompt, design a solution first and then ask the AI to build it. For instance, if you want to add a search feature to a product website, don't just ask the AI to "implement a search feature." This is too vague.
Consider the details: - Should the search query the product title, the description, or both? - Should there be filters for product types? - Do you need sorting capabilities?
Thinking through these details beforehand and spelling them out for the AI makes it much more likely to produce the exact feature you want, avoiding frustrating loops of "it doesn't work."
3. Break Down Complex Tasks
A great way to organize your prompt is to break down the feature into a list of specific tasks. Instead of a single, open-ended request, structure it like this:
"Build a search feature with the following capabilities: 1. It must search both product titles and descriptions. 2. It must include the ability to filter by different product types. 3. It must have a sorting capability."
This allows the AI to focus on and integrate each requirement, which is far more effective than iterating on a vague request step-by-step.
4. Find the Right Scope
You need to find the right balance for the scope of work you assign to the AI. If you provide tasks that are too small (e.g., "move an icon" or "add padding"), you'll spend too much time iterating on minor details instead of building entire features.
On the other hand, asking the AI to build an entire application in one go is unrealistic. Finding the correct scope comes with experience, so it's crucial to practice building applications with AI to learn where that balance lies.
5. Provide Concrete Examples
Using examples in your prompts is highly effective. If you're building a function to format dates, it's much easier to provide examples of the desired output format than to describe every possible edge case.
// Desired date format examples:
// '2025-08-09'
// 'August 9, 2025'
You can also provide sample code that already works, especially when integrating with a new library or tool. Finding working code online and having the AI start from there can dramatically accelerate development and increase the likelihood of success on the first try.
6. Leverage Documentation and Web Search
Many AI coding tools can reference official documentation or search the web directly. Using these features can speed up your work, as you don't have to manually find the information yourself. It also tends to improve the quality of the AI-generated solution.
7. Ensure Security and Performance
Make sure the code the AI writes is both secure and performant. You can either include this requirement in your original prompt or ask about it as an immediate follow-up.
Simply ask: - "Are there any security vulnerabilities in this code?" - "Is this the most efficient approach?"
It's also helpful to be proactive and consult the AI about best practices for a particular technology or feature before development begins.
8. Integrate Testing
You have two main options for testing: ask the AI to write tests as a follow-up, or adopt a test-driven development (TDD) approach. With TDD, the AI writes the tests first and then implements the feature to satisfy those tests. This ensures all edge cases are handled before any implementation code is written. This works particularly well with AI agents, which can iteratively write tests, build the feature, run the tests, and fix any failures until the code passes.
9. The Importance of Naming Conventions
The old adage, "There are only two hard things in Computer Science: cache invalidation and naming things," is more relevant than ever. AI uses the names of your files, functions, and classes to understand your code's context and will follow your naming patterns.
If a function is meant to process CSVs, name it CSVParser
, not DataParser
. Otherwise, when you want to add parsing for other data formats, the AI might try to cram all the logic into the existing function instead of creating a new one. Keeping your naming clear and up-to-date is crucial.
Modifying Existing Code: The Art of Context
Working with existing code is all about managing context.
The Golden Rule: Manage Your Context
This is the most critical takeaway for coding with AI: you must properly manage and balance the context you provide.
- Too Little Context: If you don't give the AI enough context, it will make incorrect assumptions about your code. The new code it writes won't work with your existing codebase, leading to a messy and frustrating loop of fixes. Always provide relevant files to give the AI the necessary context.
- Too Much Context: Conversely, if you provide a single 5,000-line file containing your entire application, the AI will get confused. It might start deleting code, creating duplicate functions, or failing to find the relevant pieces it needs to modify.
Continuously refactoring and maintaining clear naming conventions allows you to tag and pass only the most relevant files as context for your AI requests.
Know When to Start a New Conversation
Every message in your conversation with an AI expands its context. A long conversation (e.g., 30 minutes) can confuse the AI, causing it to reference old, non-existent code or fail to follow instructions.
A powerful technique is to start a new conversation. If you're building a new feature that isn't directly related to what you've just been working on, start a new chat and provide the relevant files as context. Experience will guide you on when to continue a conversation versus starting a new one.
Iterate Effectively: What Worked, What Didn't
When the AI implements a feature, it often makes good progress but isn't perfect. In your follow-up prompt, tell it what worked correctly and what still needs to be changed. This focuses its attention on the remaining issues without breaking what's already working.
Troubleshooting AI-Generated Code
Inevitably, things will go wrong. Here are several techniques to accelerate troubleshooting.
1. Be Specific About Errors
Be very specific about what is going wrong and what is going right. Detail the exact cases where things fail. If you're describing a UI issue, use precise language. Instead of "it's not centered," describe its position relative to other elements.
2. Share Error Logs
Copy and paste the entire error message you're receiving. The AI can often figure out the problem immediately from the full error stack.
3. Use the "Beaver Method" for Logging
Take this one step further with what I call the "Beaver Method." Ask the AI to insert logging statements throughout your code to help with troubleshooting. Run the program, then copy and paste the entire log output back to the AI. This gives it a detailed view of the execution flow and helps pinpoint exactly where things broke. This method is incredibly effective and often resolves issues in just a couple of iterations.
4. The Secret Weapon: Ask for a "Radical" Approach
Sometimes, you get stuck in a troubleshooting loop. When that happens, use this one-word secret weapon: radical.
Ask the AI to "try a radically different approach." This prompt forces the AI to abandon its current path of minor adjustments and re-evaluate the problem from a completely new angle. It's surprisingly effective at breaking through stubborn issues.
5. Know When to Code Manually
Ultimately, you need to know how to code. If you've tried everything and are still stuck, it's time to pause and look at the code yourself. There might be a fundamental conceptual issue with your approach or a limitation in the technology that the AI can't see. Knowing when to stop prompting and start debugging manually is a crucial skill.
Using AI to Accelerate Your Learning
You don't need to be an expert to use AI, but knowing how to code is essential for overcoming complex issues. Here’s how you can use AI to learn.
Tell the AI You're a Beginner
When asking for explanations, tell the AI that you're a new developer and ask it to keep things simple. This helps it tailor the response to your level of understanding.
Request Line-by-Line Explanations
Ask the AI to explain code line by line, either in the chat or by adding comments directly to the code. As you do this repeatedly, you'll start to recognize patterns, which is the foundation of learning to code.
Learn High-Level Concepts
Use the AI to learn about specific technologies and concepts. Ask it to explain how a full-stack application works or to describe different data types. These conversations will raise your overall awareness of how software is built.
Ask "How Would You Build...?"
To understand architecture, ask the AI how it would build something, like a chat application. It will introduce you to different architectural patterns (e.g., central server vs. peer-to-peer) and design considerations you might not have thought of.
Focus on the Big Picture, Not Just Syntax
Finally, focus less on the specific syntax of a language and more on the bigger picture of how software components fit together. Understanding the high-level structure of an application is one of the most valuable skills you can develop.
Join the 10xdev Community
Subscribe and get 8+ free PDFs that contain detailed roadmaps with recommended learning periods for each programming language or field, along with links to free resources such as books, YouTube tutorials, and courses with certificates.