EN

Language

Our editions

Written for each audience, not translated word for word.

Prompt engineering basics: nine techniques from OpenAI's and Anthropic's documentation

How-toBy Lucas Matías García TorresUpdated (first published )Data checked 6 min read

Reviewed by Lucas Matías García Torres on . How we work.

Leer en español

A 3D lattice of thin steel lines and nodes with a few pathways lit in orange, like a neural network

Short answer

Prompt engineering is writing effective instructions so a model consistently gives the output you need. Both OpenAI and Anthropic document the same core techniques: be clear and direct, explain why, use roles, structure the prompt (identity, instructions, examples, context, with Markdown or XML), show three to five diverse examples, put long documents first and the question last (up to 30 percent better in Anthropic's tests), say what to do rather than what not to do, specify the format, and test and version prompts like code.

Prices and limits change often: check the vendor's page before you pay. How we make money.

Prompt engineering is the process of writing effective instructions for a model so that it consistently produces the output you need. OpenAI's documentation defines it in almost those words, and adds that because model output is not deterministic, getting what you want is "a mix of art and science". This guide gathers the techniques that OpenAI and Anthropic both document for their current models, checked on October 6, 2026, and shows how to apply them with before-and-after examples of our own. It works with any chat assistant or API, and it is not a collection of magic phrases: the techniques are about being clear, giving context and testing what you write.

What to do before you write the prompt

Anthropic's guide starts with three things you need first: a clear definition of the success criteria for your use case, some way to test against those criteria, and a first draft of the prompt. If you do not have them, it points to defining success criteria and building evaluations before tuning wording. OpenAI says the same in other words: build tests and evaluation suites that measure prompt behavior, so that you can monitor results as you iterate or when you change model versions, and pin production applications to a specific model snapshot so behavior stays consistent.

Anthropic also reminds readers that not every failing evaluation is best solved by editing the prompt: sometimes you can improve latency and cost more easily by choosing a different model. For the prices behind that choice, see LLM API pricing compared.

1. Be clear and direct

Anthropic's first principle is that Claude responds well to clear, explicit instructions, and that if you want "above and beyond" behavior you should ask for it instead of expecting the model to infer it. Its rule of thumb is to treat the model as "a brilliant but new employee who lacks context on your norms and workflows", and its golden rule is to show your prompt to a colleague with minimal context and ask them to follow it: if they would be confused, the model will be too. Be specific about the output you want and, when order matters, give the steps as a numbered list.

Anthropic's own example: "Create an analytics dashboard" is less effective than "Create an analytics dashboard. Include as many relevant features and interactions as possible. Go beyond the basics to create a fully-featured implementation."

2. Give the reason behind an instruction

Explaining why you want something helps the model apply it sensibly. In Anthropic's example, "Never use ellipses" is less effective than "Your response will be read aloud by a text-to-speech engine, so never use ellipses since the text-to-speech engine will not know how to pronounce them." The model can generalize from the reason to cases you did not list.

3. Use roles and levels of authority

OpenAI separates instructions by who gives them: developer messages come from the application developer and are prioritized ahead of user messages, which come from the end user. It suggests thinking of the developer message as a function definition, with the rules and business logic, and the user message as the arguments. Anthropic makes the same point with the system prompt: setting a role there, even in a single sentence such as "You are a helpful coding assistant specializing in Python", focuses the model's behavior and tone. In a chat app without those settings, the equivalent is to open with who the assistant should be and what you want done.

4. Structure the prompt

OpenAI says a developer message usually contains four sections, in this order: identity (purpose and style), instructions (rules, what to do and not to do), examples, and context (extra information such as private data), with the context best placed near the end because it changes between requests. Markdown headers and lists mark sections; XML tags mark where one piece of content, such as a reference document, begins and ends. Anthropic recommends XML tags too, with consistent and descriptive names, because they help the model parse a prompt that mixes instructions, context, examples and inputs without ambiguity.

5. Show examples

Examples are, in Anthropic's words, one of the most reliable ways to steer format, tone and structure. A few well-chosen examples (few-shot or multishot prompting) improve accuracy and consistency. Anthropic says to make them relevant to your real use case, diverse enough to cover edge cases so the model does not pick up unintended patterns, and structured in tags so they cannot be confused with instructions; it suggests three to five. OpenAI describes few-shot learning as steering a model toward a task by including a handful of input and output examples instead of fine-tuning it, and advises showing a diverse range of inputs.

6. Put long documents first and the question last

For large inputs, Anthropic advises placing long documents near the top of the prompt, above the query, instructions and examples, and reports that putting the query at the end can improve response quality by up to 30 percent in its tests, especially with complex, multi-document inputs. For tasks over long documents, it also suggests asking the model to quote the relevant parts first. Google's long-context guide gives the same advice about placing the question after the context. If you reuse the same instructions across many requests, keep the stable part at the beginning: OpenAI notes this also maximizes the savings from prompt caching.

7. Say what to do, not what not to do

Anthropic lists this as an effective way to control format: instead of "Do not use markdown in your response", write "Your response should be composed of smoothly flowing prose paragraphs." Positive instructions give the model a target; prohibitions leave it guessing what to do instead.

8. Specify the output format

State the format you need: length, structure, language, sections. When software will read the answer, use the structured output features of your API so that the response follows a JSON schema; OpenAI notes that models can return structured data in JSON format through its Structured Outputs feature. For long documents, tell the model whether you want a summary, a table or a list, and in what order.

9. Test it, version it and re-test it

Prompts are code. OpenAI recommends storing production prompts in your application code, with typed inputs, review and tests, and adding representative cases and evaluation checks before you change a prompt that is live. Different model types (reasoning models and GPT models) can need different prompting, and even different snapshots in the same family can behave differently, so re-run your evaluations when you change model. Our guides to Claude vs ChatGPT and ChatGPT pricing explain which models each plan gives you.

A worked example

This is our own example of the techniques together, for a customer-support assistant. The weak version is "Answer customer questions about our returns." The stronger version uses the sections OpenAI describes:

  • Identity: "You are the support assistant for a small online shop. You write short, friendly answers in the customer's language."
  • Instructions: "Answer only from the policy in the context. If the policy does not cover the question, say you will pass it to a person. Give the answer first, then the steps, as a numbered list. Do not promise refunds."
  • Examples: two or three sample questions with the answers you would want, wrapped in example tags.
  • Context: the returns policy text, placed last, followed by the customer's message.

Then run it on 30 to 50 real past questions and read the answers before any customer sees it. If the model lacks information, adding the right documents (retrieval) will do more than rewording: see what RAG is.

What prompting cannot fix

A better prompt does not make a model know what it was not given, and it does not guarantee a correct answer. For facts that change or are private, give the model the source text. For decisions that matter, keep a person in the loop and check the result. For a model that is too slow or too expensive, try a different model before a longer prompt. Models can also state false things fluently, and our guide to model hallucinations explains why and which prompt techniques reduce it, such as allowing the model to say it does not know.

What we checked

  • OpenAI: prompt engineering is the process of writing effective instructions for a model such that it consistently generates content that meets your requirements; getting the desired output is a mix of art and science; recommends pinning production applications to specific model snapshots and building tests and evaluation suites; instructions and message roles give differing levels of authority, with developer messages prioritized ahead of user messages, like a function and its arguments; a developer message usually contains identity, instructions, examples and context in that order, with context near the end; Markdown and XML tags mark sections; few-shot learning steers a model with a handful of input and output examples instead of fine-tuning; production prompts should be stored in code with typed inputs, tests and evaluation checks; keeping repeated content at the beginning of the prompt maximizes prompt caching savings; models can return structured JSON with Structured Outputs. (OpenAI, )
  • Anthropic: before prompt engineering you need success criteria, ways to test against them and a first draft; not every failing eval is best solved by prompt engineering and a different model can improve latency and cost more easily. (Anthropic, )
  • Anthropic: be clear and direct (treat the model as a brilliant but new employee who lacks context; golden rule of showing the prompt to a colleague with minimal context); add context or motivation for instructions; use three to five relevant, diverse and structured examples in tags; structure prompts with consistent XML tags; give the model a role in the system prompt; put longform data at the top of the prompt and the query at the end, which can improve response quality by up to 30 percent in tests; ground responses in quotes for long documents; tell the model what to do instead of what not to do. (Anthropic, )
  • Google's long-context guide advises putting the query at the end of the prompt, after all the other context. (Google (Gemini API docs), )

What may change

  • Prompting guidance is model-specific and changes with each release; both vendors keep a living reference per model, and this page is reviewed quarterly.
  • The percentage improvement for putting the query last is from Anthropic's own tests on its own models; yours may differ.

Frequently asked questions

What is prompt engineering?

OpenAI defines it as the process of writing effective instructions for a model so that it consistently generates content that meets your requirements. Because model output is not deterministic, it is a mix of art and science: you apply documented techniques and test the result.

How do I write a good prompt?

Be clear and specific about the task and the output format, explain why you want it, give three to five relevant and varied examples, structure the prompt in labeled sections, and put long documents before the question. Anthropic's rule of thumb: show the prompt to a colleague with no context, and if they would be confused, the model will be too.

What is few-shot prompting?

Including a handful of input and output examples in the prompt so the model picks up the pattern, instead of fine-tuning it. Anthropic advises three to five examples that are relevant, diverse and wrapped in tags so they are not mistaken for instructions.

What is the difference between a system prompt and a user prompt?

In OpenAI's terms, developer messages carry the application's rules and business logic and are prioritized ahead of user messages, which carry the end user's inputs, like a function definition and its arguments. Anthropic's system prompt serves the same purpose, including setting a role for the model.

Where should I put long documents in a prompt?

Near the top, above the question, instructions and examples. Anthropic reports that placing the query at the end can improve response quality by up to 30 percent in its tests, especially with complex multi-document inputs, and Google gives the same advice.

Can a better prompt fix wrong answers?

Only when the model had the information and misunderstood the task. If it lacks the facts, give it the source text with retrieval; if the task is beyond the model, try a different model. Always test on real examples and keep a person in the loop for decisions that matter.

Sources

  1. Prompt engineering (OpenAI API guide): message roles, structure, few-shot learning, context and evals, OpenAI. Accessed October 6, 2026.
  2. Prompt engineering overview (Claude): success criteria, evaluations and when to prompt engineer, Anthropic. Accessed October 6, 2026.
  3. Prompting best practices (Claude): clarity, context, examples, XML tags, roles and long context, Anthropic. Accessed October 6, 2026.
  4. Long context (Gemini API): what a context window is and how to use it, Google (Gemini API docs). Accessed October 6, 2026.

Spotted an error or an outdated price? Tell us and we will fix it.

Change history

  • : Wording revised; no figure, date or source changed.
  • : First published from OpenAI's prompt engineering guide and Anthropic's prompt engineering overview and prompting best practices.

Next review: .

Part of our Language Models Explained guide.

Other topics