Back to Prompts
🟢OpenAIBeginner

OpenAI Prompt Engineering Guide

Official best practices for writing effective prompts for GPT models, including GPT-4 and GPT-5 series

View Official Documentation

TL;DR

  • Write clear, specific, and detailed instructions
  • Provide reference text to reduce hallucinations
  • Split complex tasks into simpler subtasks
  • Give the model time to think (chain-of-thought)
  • Use external tools for specific tasks
  • Test changes systematically

Key Principles

1Be specific about desired output format, length, and style
2Use delimiters (### or """) to separate instructions from context
3Provide examples (few-shot prompting) for complex tasks
4Specify steps required to complete a task
5Ask for structured output like JSON when needed
6Instruct the model to explain its reasoning before answering

Write Clear Instructions

GPT models cannot read your mind. If outputs are too long, ask for brief replies. If outputs are too simple, ask for expert-level writing. If you dislike the format, demonstrate the format you want to see. The less the model has to guess, the better your results.

Examples

Include details in your query
Bad:
How do I add numbers in Excel?
Good:
How do I add up a row of dollar amounts in Excel? I want to do this automatically for a whole sheet of rows with all the totals ending up on the right in a column called "Total".

The detailed prompt specifies the exact context (dollar amounts, whole sheet, column name) helping the model deliver precisely what you need.

Specify the desired length of output
Bad:
Summarize this meeting transcript.
Good:
Summarize the meeting transcript in a single paragraph. Then write a markdown list of the speakers and each of their key points. Finally, list the next steps.

Specifying the structure and format ensures consistent, usable output.

Provide Reference Text

GPT models can confidently invent fake answers, especially for obscure topics or citations. Providing reference text can help the model answer with fewer fabrications.

Examples

Answer using only reference text
Good:
Use the provided articles delimited by triple quotes to answer questions. If the answer cannot be found in the articles, write "I could not find an answer."

"""<insert articles>"""

This constrains the model to use only provided information, preventing hallucination.

Split Complex Tasks

Complex tasks have higher error rates than simpler tasks. Break down complex tasks into a workflow of simpler subtasks, where the output of earlier tasks feeds into later ones.

Examples

Use intent classification for customer queries
Good:
First, classify the customer query into one of these categories: [Billing, Technical Support, Account Management, General Inquiry]. Then provide the appropriate response based on the category.

Breaking the task into classification then response improves accuracy for each step.

Give the Model Time to Think

Models make more reasoning errors when trying to answer immediately. Asking for a chain of reasoning before the final answer helps the model reason its way toward correct answers more reliably.

Examples

Work out the solution before evaluating
Good:
First, work out your own solution to the problem step by step. Then compare your solution to the student's solution and evaluate if the student's solution is correct or not. Do not decide if the student's solution is correct until you have done the problem yourself.

Having the model solve independently first leads to more accurate evaluations than jumping straight to judgment.

Use External Tools

Compensate for model weaknesses by using external tools. For example, use code execution for math, or retrieval systems for current information.

Examples

Use code execution for accurate calculations
Good:
Write Python code to calculate this, then run it: What is the 123rd Fibonacci number?

For precise calculations, having the model write and execute code is more reliable than mental math.

Test Changes Systematically

Improving performance is easier when you can measure it. Create a comprehensive test suite (an "eval") that covers the variety of inputs you expect. Use these evals to measure whether changes improve or degrade performance.
#prompt-engineering#gpt#gpt-4#gpt-5#best-practices#few-shot#chain-of-thought#openai
Last updated: December 1, 2024