Generative AIPrompt engineeringFundamentals of prompt engineering

Crafting effective prompts

8 minutes read

A short request to an LLM will get some answer. Getting the answer you actually need, and getting it consistently, usually takes a more carefully written prompt.

Characteristics of an effective prompt

Weak prompts are usually open-ended and assume the model will guess your intent. A clear, specific prompt narrows down what the model can reasonably answer.

Start with clarity: use direct instructions and skip politeness fillers like "if you don't mind." They are normal in conversation, but in a prompt they add words without adding information. A clear prompt states the task directly:

Explain the concept of gravity to a group of 10-year-olds.

A good prompt is also specific. If you ask for a "short summary," the model has to guess what "short" means: it could be three words or three paragraphs. A specific prompt removes the guesswork:

Explain the concept of gravity to a group of 10-year-olds. Keep it at approximately 50 words.

Here are sample outputs from three models at the time of writing (wording varies between models and versions):

Imagine Earth is a giant, invisible magnet. That's gravity! It pulls everything toward the ground. It is the reason you come back down when you jump and why objects stay on the floor. Gravity holds the oceans, the air, and us onto the planet so we don't float away.

Gravity is a natural force that pulls objects toward each other. Earth's gravity pulls everything toward the ground, which is why you stay on the floor instead of floating away. It also keeps the Moon orbiting Earth and makes dropped objects fall. Without gravity, everyday life would feel very different.

Gravity is an invisible force that pulls objects toward each other. Earth's gravity keeps you on the ground and makes things fall down when you drop them. It's why you can jump but always come back down, and why the Moon orbits around Earth instead of floating away into space!

This applies to every part of the request, including tone, audience, and format. If you want the model to rewrite code, include the code and say "change this snippet to use approach X" instead of asking "Can you suggest changes?" The more concrete the request, the fewer corrections you'll need later.

Also watch for words with more than one meaning. People resolve them from the situation, but the model only has your text. For instance, "bank" could be a financial institution or the side of a river. Name the domain ("financial bank", "river bank") or give enough context to make the meaning clear.

Iterative refinement

Your first prompt will rarely give the result you want. You write a draft, test it against your requirements, look at the output, and change the wording. A poor result shows you which part of the instruction was unclear or missing.

You may also need to split a complex task into smaller subtasks, which is called prompt chaining. If a single prompt asks the model to summarize a document, translate it, and format it as an email, the model might skip one of the steps or do them poorly. If you split the work into three prompts (summarize, then translate the summary, then format it as an email), each prompt has one job, and when something goes wrong, you can see which step failed.

If the content is correct but the style is wrong, you need to refine the persona or tone instructions. If the facts are incorrect, you need to provide more accurate context or source data. To prevent the model from fabricating facts, explicitly allow it to say "I don't know" if it lacks the necessary information. Conversely, if the response is off-topic, you should clarify your end goal and explain why you are asking. For issues with formatting or unnecessary introductory text, you can provide examples of the desired output or explicitly instruct the model to skip the preamble and give only the answer.

Some common mistakes waste time. A longer, more complex prompt isn't automatically better, and an over-engineered prompt often confuses the model. Modern models usually follow clear, explicit instructions without complicated techniques or heavy role-playing. Also, don't apply every technique at once: start with a simple, clear instruction and add complexity only when you need it, testing each change to see if it actually improves the result.

Model playgrounds are useful for this iterative refinement:

Comparing two models in Amazon Bedrock playgrounds.

Changing one thing at a time and checking the result is how you debug a prompt. Over time, you'll collect prompt structures that work for your use cases. A prompt management system can store them, along with prompt templates; you'll use one in a later topic.

Working with data responsibly

Once you use LLMs for work or personal projects, you'll be pasting in real data, and that data goes to someone else's servers. Treat anything you paste into a prompt as if you were posting it publicly, especially on free or consumer services. Many of them keep inputs and outputs and may use them to train future models, so sensitive information could end up in a later model's training data.

The most important rule is to never put personally identifiable information (PII) into a public model. PII includes names, addresses, phone numbers, social security numbers, and email addresses. The same goes for confidential company data, such as internal codebases, financial sheets, or secret project names. Even if a service promises privacy, data breaches or logging errors can happen, so assume that once data leaves your machine, it's no longer fully under your control.

To use LLMs with sensitive tasks, sanitize the data first: replace sensitive details with generic placeholders before you build the prompt. For example, in a customer email, replace "John Doe" with "User_A" and "New York" with "City_X", then ask the model to process the sanitized text. When the response comes back, map the placeholders back to the real data locally, so the sensitive details never reach the external server.

Conclusion

An effective prompt is clear, specific, and unambiguous about what you need. You rarely get it right the first time: test the prompt, see what's wrong with the output, and change one thing at a time. Whatever you send to a hosted model, leave out personal and confidential data, or replace it with placeholders first.

27 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo