Generative AIPrompt engineeringFundamentals of prompt engineering

Introduction to prompt engineering

8 minutes read

The way you write a prompt largely decides what a large language model gives back. Prompt engineering is the practice of writing and testing prompts so that the output is useful and consistent.

What is a prompt?

A prompt is a set of instructions or inputs in natural language that directs a model to perform a specific task, such as answering a question, summarizing information, or generating images.

LLMs operate by predicting the most contextually appropriate continuation of the text they receive. When the input is clear and well-structured, the model can infer intent more reliably and generate responses that better align with your expectations. It does not "know" facts in the human sense; instead, it calculates which words are most likely to follow your specific sequence of words based on what it learned during training.

Because LLMs are flexible and can perform many tasks, even a single word will get a response from them. However, not every input generates helpful content. If you just type "write," the model might write a poem, code, or a grocery list. You need to provide detailed information and context so that the LLM's response is useful. This often requires you to be specific about the format, length, and style you want.

Getting a prompt right usually takes several tries. You write a prompt, look at the result, notice a constraint you forgot, and rewrite the prompt to be more precise. This loop of writing, testing, and rewriting is most of a prompt engineer's work.

Single vs chat prompts

In the early days of LLMs, interactions were based on single prompts, also known as text completion. In this format, the input is just a single block of text, and the model simply adds words to the end of it. It works like a smart autocomplete, but for whole paragraphs. If you feed it the text "The capital of France is", the model predicts "Paris". This method is easy to use, but it makes it hard to manage complex instructions or long back-and-forth conversations.

Modern applications primarily use chat prompts, which structure the input as a list of messages. This format mimics a real conversation and assigns specific roles to each message. The most distinct role is the system message. This message is usually hidden from the end user and acts as the "inner rules" for the model. It defines who the model is (e.g., "You are a helpful coding assistant") and sets boundaries that apply to the whole conversation. Some providers name this role differently: in OpenAI's newer models, it is the developer message, while Anthropic's API takes it as a separate system prompt.

The user role is the human's input: the question or command you type. In a chat app, every message you send is labeled as a "user" message. The assistant role holds the model's previous answers. Including these past messages in the prompt gives the model "memory": it can look back at what it said earlier and keep the context.

Modern systems also use the tool role (sometimes called the function role) when the model works with external software, like a database or a search engine. If the model needs to do math, it can generate a structured request, and the system runs the code. The system then sends the result back to the conversation as a "tool" message, so the model can use the actual result in its answer.

Simple completion prompts are fine for quick one-off tasks, but applications are built on chat prompts.

Model settings

The text of your prompt guides the content, but the model's behavior also depends on configuration settings. The most common setting is temperature, which controls the randomness of the output. A low temperature (near 0) makes the model consistent and focused, selecting only the most likely words. This suits tasks like math or data extraction, where you want the same answer every time. A high temperature (for example, 1 or above, depending on the provider's range) lets the model choose less common words, which is useful for creative writing or brainstorming.

Two other settings, Top P and Top K, work alongside temperature to filter the word choices. Top P (or nucleus sampling) limits the model's choices to the best group of words that add up to a certain percentage of probability. This cuts off completely nonsensical words while keeping some variety. Top K is simpler; it tells the model to only pick from the top K most likely words (e.g., the top 50).

To prevent the model from repeating itself, we use frequency and presence penalties. The frequency penalty lowers the chance of a word appearing if the model has already used it many times in the text. This stops the model from getting stuck in a loop, repeating the same phrase over and over. The presence penalty is slightly different; it applies a penalty if a word has appeared even once. This encourages the model to move on to new topics and use a wider vocabulary, rather than staying focused on one subject for too long.

The max output tokens setting puts a hard limit on the output length. This is different from the context window, which is the total amount of text (your prompt plus the model's answer) the model can process at one time. A max token limit keeps costs down and responses fast, but if the limit is too low, the model stops mid-sentence, so leave enough room for a complete answer.

Finally, stop sequences are words or symbols (such as "User:" or "End") that make the model stop writing as soon as it generates them. This is useful in chatbots. For example, if the model writes a dialogue between two characters, you can set a stop sequence for the other character's name, so the model stops after its own part and waits for your input instead of writing the whole conversation.

Support for these settings varies by model and API. Many current reasoning models don't accept sampling settings like temperature, Top P and Top K: the current Claude API rejects them, and OpenAI's reasoning models accept temperature and Top P only when the reasoning effort is set to none. On these models, you steer the output with the prompt itself and with settings like reasoning effort (how much the model thinks before answering) and max output tokens. Penalties and stop sequences aren't available in every API either, so always check the parameter reference of the model you use.

Many of these settings are only available when you call a model through its API. In chat apps, they are usually hidden or replaced by simple controls.

Conclusion

A prompt is the instruction you give a model. In modern applications, it's a list of messages with roles: system (or developer), user, assistant, and tool. Settings such as temperature, Top P, penalties, max output tokens, and stop sequences change how the model generates its answer, but which of them you can use depends on the model and the API. Good results usually come from adjusting both the prompt and the settings, and testing what you get.

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