Prompt Engineering Notes for Coders and Hack-
ers
1. Ask the Model to Adopt a Role or Define a Persona
title
When you assign a specific role to the model (e.g., ”Be an ethical
hacker” or ”Act as a Python tutor”), its responses become more tar-
geted and relevant.
How: Start the prompt with Act as or You are a [role/persona].
Example:
You are a cybersecurity expert. Explain how to ethically test the security of a w
title
The model provides ethical and professional advice specific to cyber-
security.
2. Include Details in Your Query to Get More Relevant Answers
title
The more context you provide, the more accurate and tailored the
response.
How: Clearly state the language, framework, tools, or version you are
working with. Example:
I am using Python 3.10 with Flask. \\
\vspace{0.5cm} % Adjust space as needed
How can I create an API endpoint to accept JSON data and store it in an SQLite da
title
A detailed response with Python 3.10 and Flask-specific code.
3. Specify the Steps Required to Complete a Task
1
title
Breaking down tasks into steps helps the model structure the response
in a way that is easy to follow and implement.
How: Use phrases like Step-by-step, List the steps, or Explain the
process. Example:
Provide step-by-step instructions to set up a Node.js server using Express and Mo
title
A step-by-step guide to set up the server, install dependencies, and
connect to MongoDB.
4. Provide Examples to Enhance Understanding
title
Examples give the model context for the output you expect and im-
prove response quality.
How: Include your example and specify the desired format. Example:
Create a Python function to hash a password using bcrypt. Example:
Input: "my_password"
Output: A hashed password
title
A Python function using bcrypt to hash the given password.
5. Request Explanations or Justifications
title
Understanding the reasoning helps validate or refine the solution.
How: Use phrases like Explain why or Provide reasoning for. Ex-
ample:
Write a secure SQL query to prevent injection attacks and explain why it is secur
title
Code using parameterized queries and an explanation of how it pre-
vents SQL injection.
2
6. Ask for Optimization or Alternatives
title
Sometimes you need better performance, simpler code, or different
approaches.
How: Use phrases like Optimize this code or Provide alternative
methods. Example:
Optimize the following Python code for readability and performance:
Followed by your code.
7. Define Specific Constraints or Rules
title
Constraints guide the model to provide solutions that meet your exact
requirements.
How: Mention constraints like Use no external libraries or Must
be under 10 lines. Example:
Create a Python function to sort a list without using the built-in ‘sort()‘ funct
title
A sorting function that does not rely on sort().
8. Combine Multiple Tasks in One Prompt
title
Saves time and gets a comprehensive answer.
How: Use conjunctions like and or while to connect tasks. Example:
Write a Python script to scrape data from a website and save it to a CSV file. Al
title
A complete script that includes web scraping, data saving, and error
handling.
9. Specify Output Format
3
title
Ensures the response matches your expected structure or style.
How: Use phrases like Output as or Provide in [format]. Example:
Generate a JSON schema for a to-do app with fields: id, title, description, and c
title
A JSON schema in the desired format.
10. Iterate and Refine Prompts
title
If the initial response is not accurate, refine the prompt to include
additional context or adjust instructions.
How: Use phrases like Refine this or Based on this code, do the
following. Example:
Based on the Node.js server code you provided, add a route for deleting a user by
title
An updated code snippet with the delete route.
11. Avoid Ambiguity
title
Ambiguity in the prompt can lead to irrelevant or generic responses.
How: Be as specific as possible about your goals. Example:
Bad: Write a function in JavaScript.
Good: Write a JavaScript function to reverse a string without using built-in meth
title
A targeted solution for reversing a string.
12. Ask for Debugging or Explanation of Errors
title
Useful for finding bugs and understanding issues in your code.
4
How: Share the problematic code and describe the issue. Example:
Here’s my Python code:
Followed by the code and the error message.
Help me fix this and explain why the error occurs.
13. Experiment with Context Length
title
Adding background details or omitting irrelevant context affects the
quality of the response.
How: Provide necessary details but avoid overloading the prompt. Ex-
ample:
I am building a REST API in Django. How do I implement token-based authentication
title
Focused guidance on JWT implementation in Django.
14. Test for Edge Cases
title
Verifies that the solution works in all scenarios.
How: Ask the model to include edge case handling in its response. Ex-
ample:
Write a Python function to check if a string is a palindrome. Include edge cases
title
By mastering these techniques, you can maximize the value you derive
from AI tools.
==========Done================