How to create better prompts for AI
Introduction
Currently, it is very important to know how to create good prompts, because the command sent is directly related to the response received.
If you send a prompt with little information, the AI will do its best to return information that is useful to you.
However, the less information passed to the AI, the more possibilities it has to analyze to return to you.
And with that, the AI might "hallucinate" a bit, and bring undesired responses for what you might need.
That is why it is fundamental to be more specific in the requests to the AI.
Furthermore, there are some extra commands that can be passed, so that the AI behaves in a specific way, or performs actions in a certain way that helps to bring better responses.
Below I will show some practices that I use in my daily life to create good prompts and obtain better results, whether for the code part or for general daily items.
Prompt structure
I have been structuring my prompts the same way for a while; I have already analyzed and tested some structures, and the one I adapted to was this one, for the simplicity of being straight to the point, and with the information the AI needs to generate good responses.
The structure is:
Persona
Context
Rules / Additional information
For some time, inside the prompt, I would put the markdown command in the way shown above, filling in the information below each topic.
But lately, I haven't been using markdown titles; I put the information directly in the prompt and it has been working well too.
Persona
Usually, the first line of my prompt is to ask the AI to act as an expert in what I want to work on; if it's with PHP and Laravel, I ask "Act as an expert in PHP and Laravel".
This way, the AI changes its behavior to perform better research, deeply analyze more information, and give you a response with more advanced knowledge.
Context
This is where I invest most of my time, because it is at this moment that I describe what I want from the AI, being as specific as possible.
It is common for me to create prompts of 20, 30 lines, always seeking to ensure that the AI will understand what I am needing, to generate a better response.
If it is a prompt for some code implementation, I usually analyze the current codebase of the project, see if there is already some part implemented, or something that can be used as a base, and I link it within the prompt.
Also, in code implementation, if the implementation requires any external library, I research the libraries beforehand, understand which one is the best to be used, and already pass it to the AI to do the implementation directly with that specific library and in what way I would like the implementation, usually creating an abstraction since it is an external lib.
This part of researching external libraries beforehand also helps the AI consume fewer tokens and reduces response time, because it will have fewer things to process.
Researching also helps to identify more accurately what needs to be done or what I expect as a response from the AI; whether for a daily prompt, I can identify more easily if the AI answered what I needed or not.
I recommend investing as much time as possible in this context stage, because it directly interferes with the response you will receive.
And in this case, I always finish the context by asking more accurately what I expect as a response, such as: Implement this, Plan the development, Analyze the information and bring me an answer.
Rules / Additional information
I leave this section exclusive for some response rules that I want or extra information that I can pass, and usually here I use bullet points; I will leave some examples that I use in my daily life:
- Answer me in the format
X: text, Y: text .... - Do not search for information on other sites.
- Create the image in PNG format and 100x100 size.
- Use the official documentation of the library
xyz, link:xyz.com. - If you have any questions, ask me before starting.
The ones above are some examples, and I would like to highlight the last one: If you have any questions, ask me before starting., as I always put this in all prompts.
This instruction is very useful, because if the AI has any doubt about the context, or about exactly what it needs to do, the AI will ask you before continuing thinking, and after it continues, it will bring something more assertive.
Using Claude Code, inside VsCode and using this question instruction, Claude opens a popup with the questions it wants to ask you, and usually even with pre-determined options of what it intends to do; it is possible to choose the option to answer the question and Claude continues thinking now with this information it had doubts about, to give you a better answer.
Conclusion
The subject of prompts is extensive, but this standard structure can help to create better prompts and have better results.
The ideal is to keep testing different prompt models, based on what you need to do in your daily life, and making changes until you reach a model that gives more precise answers and increases productivity.
If you have any suggestions for improvement in this article, please contact me on LinkedIn :)