How to Write Effective AI Prompts
Complete guide to getting the best results from artificial intelligence
What is a Prompt?
A prompt is the instruction you give to an AI to perform a task. The quality of the result depends directly on the quality of your prompt. A good prompt is like a good recipe: it needs precise ingredients and clear steps.
The 4 Elements of a Perfect Prompt
1. Context
Provide information about your situation, experience level, and end goal.
Good: "I'm a junior developer working on an e-commerce app with React and I need..."
2. Specific Task
Be as specific as possible about what you want the AI to do.
Good: "Create a TypeScript function that filters an array of products by minimum and maximum price, category, and stock availability"
3. Output Format
Specify how you want to receive the response.
4. Constraints
Set clear boundaries.
Ready-to-Use Prompt Templates
For generating code
For debugging code
For learning a concept
Advanced Techniques
Chain of Thought
Ask the AI to think step by step:
Few-Shot Learning
Provide examples before asking for the task:
Role Playing
Assign an expert role to the AI:
Common Mistakes When Writing Prompts
- Being too vague: "Do something with data" doesn't work
- Too long without structure: Use paragraphs and lists
- Not providing context: The AI doesn't know your level or goal
- Asking everything in one prompt: Break complex tasks into steps
- Not iterating: If the result isn't good, refine the prompt
Perfect Prompt Checklist
Prompt Examples by Language
Each programming language has its own conventions, patterns, and best practices. Below are specific prompts tailored to the most popular languages, so you can get much more precise and useful results.
Python
Python is characterized by its readability and scientific ecosystem. Effective Python prompts should mention PEP 8, type hints, and the specific libraries you use in your project.
JavaScript / TypeScript
For JavaScript and TypeScript, it's essential to specify the runtime environment (browser or Node.js), the module system, and whether you prefer a functional or object-oriented style.
SQL
SQL prompts should include the specific database engine (PostgreSQL, MySQL, SQL Server), the schema of the tables involved, and the expected result with sample data.
CSS / Tailwind
CSS prompts should describe the desired visual design, responsive behavior, and accessibility constraints you need to meet.
How to Iterate and Refine Prompts
You rarely get the perfect result on the first try. The key is to treat prompt writing as an iterative process where each version improves upon the previous one. Below we show you how to transform mediocre prompts into precise instructions.
Iteration 1: From vague to structured prompt
The first attempt is usually too generic. The AI needs context and specificity to produce professional-quality code.
Iteration 2: Refining the result
Once you have a working first version, you can request specific improvements to the generated code.
Iteration 3: Adapting to your codebase
The final step is adapting the generated code to the conventions and patterns of your existing project.
Tips for iterating effectively
Follow these principles to get the most out of each iteration with the AI. It's not about rewriting the entire prompt, but adding layers of precision progressively.
- Don't start from scratch: Request modifications to the previous result instead of regenerating everything
- Be specific about what to change: "Change line 5 to use map instead of forEach" is better than "improve the code"
- Provide negative feedback: "This doesn't work because X, I need it to do Y instead"
- Save your best prompts: Build a personal library of prompts that have worked well for you
- Use conversation context: The AI remembers previous messages, so you can reference earlier results
Frequently Asked Questions
How long should an ideal prompt be?
There's no fixed length, but the most effective prompts typically have between 100 and 300 words. What matters isn't the quantity but the density of useful information. A very specific 50-word prompt can outperform a 500-word prompt full of irrelevant information. The general rule is: include everything necessary and nothing more. If your prompt exceeds 500 words, you're probably trying to cover too many tasks in a single request and should break it into steps.
Do prompts work the same across all AIs?
The fundamental principles are the same (context, task, format, constraints), but each model has its particularities. ChatGPT responds well to detailed and conversational instructions, Claude excels with long prompts and extensive documents, and Gemini works well with prompts that include structured data. The recommendation is to learn each model's strengths and adapt your prompting style accordingly. A well-written prompt will work reasonably well on any model, but optimal results require specific adjustments.
Should I write prompts in English or another language?
Most AI models have been trained predominantly in English, so prompts in this language tend to produce slightly better results, especially for code and technical documentation. However, current models handle other languages quite well. A good strategy is to use technical terms in English (for example, "implement a middleware with error handling"). For code, variable and function names should always be in English.
How do I prevent the AI from hallucinating or inventing libraries?
To minimize hallucinations, always include the explicit constraint "use only libraries that exist and are actively maintained" and provide your project's dependency list (your package.json, requirements.txt, etc.). You can also ask the AI to verify its answer: "Before giving me the code, confirm that all libraries you use exist on npm/PyPI and are in their latest version." Another effective technique is to ask it to cite the official documentation of the APIs it uses.