Skip to content
AI & MLbeginner

What is prompt engineering?

Updated Nov 30, 20263 min readBased on official documentation
What is prompt engineering?

In short

Prompt engineering is the practice of writing clear instructions for a language model so it reliably produces the output you need. Because model output varies from run to run, it mixes craft with testing. Good prompts use clear roles, explicit rules, examples and context.

Prompt engineering is the work of writing the text you send to a large language model (LLM), which is a program trained on huge amounts of text to produce new text. The prompt is the input. The model's reply depends heavily on how that input is written, so small wording changes can lead to very different results.

How it works

With the OpenAI API, you send a prompt to a model and read back its output. The reply is not a single string. It is a list of items, and that list can include tool calls, reasoning data and text. Do not assume the text sits in one fixed position. Some SDKs offer a convenience property that joins all text output into one string.

Models are non-deterministic, which means the same prompt can produce different answers on different runs. That is why prompt engineering is part art and part science. You apply known techniques, then check the results over and over.

Roles and instructions

A prompt is split into messages with roles. A developer message holds the rules for how the assistant should behave. A user message holds the input from the person using the app. The assistant role is used for replies the model writes. Developer messages carry more weight than user messages, so they are the right place for rules that must hold.

You can also pass turn-specific guidance with the instructions parameter. For guidance that should last across a conversation, place it in the first developer message instead. A useful comparison is a function and its arguments. The developer message is like the function body, and the user message is like the arguments passed in.

Structuring a prompt

Long prompts are easier for the model to follow when they have clear sections. Markdown headings and lists can mark parts of the prompt, and XML-style tags can wrap reference material so it is obvious where it begins and ends. A developer message often covers, in roughly this order: who the assistant is, the rules it must follow, a few examples of input and desired output, and finally any context such as private data. Put the context near the end, since it may change between requests.

Examples and reuse

Few-shot prompting means including a handful of input and output examples in the prompt. The model picks up the pattern from them and applies it to new input. This is often faster and cheaper than training a custom version of the model.

Reusable guidance should go first in the message list. The shared start of a prompt can then be cached by the platform, which can lower cost and latency.

Common mistakes

A common mistake is treating the first attempt as final. Test prompts against a set of realistic inputs, and keep those tests in your codebase. Another mistake is changing the model version without checking results. Pinning production apps to a specific model snapshot helps keep behavior steady, and an evaluation suite shows when a change makes things worse.

Keeping prompts in application code, rather than in saved prompt objects, makes them easier to review and test. The official guidance notes that saved prompt objects are being deprecated, so new work should avoid them.

Choosing a model

The model you call affects how you should write prompts. Reasoning models work through an internal chain of thought, which helps with complex, multi-step tasks, but they are usually slower and more expensive. GPT models are fast and cost-efficient, but they benefit from more explicit instructions. Smaller models are generally faster and cheaper, while larger ones handle a wider range of problems more reliably. Start with a general-purpose model, then adjust your prompt as you learn how it behaves.

FAQ

Do I need to be a programmer to write prompts?

Anyone can write prompts in a chat tool. Developers need extra skills when they build prompts into an app, such as typed inputs, tests and safe rollouts.

Why does the same prompt give different answers?

Models generate text with some randomness, so the output is not fixed. Testing many inputs helps you judge whether a prompt works reliably.

What is the difference between a developer message and a user message?

Developer messages hold the rules from the app builder and take priority. User messages carry the input from the person using the app.

When should I use few-shot examples?

Use them when the model needs to copy a specific format or style. A few clear input and output pairs often work better than long descriptions.

Sources

This page is based on the official documentation below. Read it for full detail.

Prompt engineering — official documentation ↗
blozhikDeveloper news from across the web, explained. Every story links to its original sources.