SPB Git

Repository Public

prompt-engineering.md 36.5 KB 1,003 lines markdown

1# Prompt engineering23> For the complete documentation index, see [llms.txt](/llms.txt). Markdown versions of documentation pages are available by appending `.md` to the page URL.45With the OpenAI API, you can use a [large language model](https://developers.openai.com/api/docs/models) to generate text from a prompt, as you might using [ChatGPT](https://chatgpt.com). Models can generate almost any kind of text response—like code, mathematical equations, structured JSON data, or human-like prose.6789Here's a simple example using the [Responses API](https://developers.openai.com/api/reference/resources/responses).1011Generate text from a simple prompt1213```javascript14import OpenAI from "openai";15const client = new OpenAI();1617const response = await client.responses.create({18  model: "gpt-6-astra",19  input: "Write a one-sentence bedtime story about a unicorn.",20});2122console.log(response.output_text);23```2425```python26from openai import OpenAI2728client = OpenAI()2930response = client.responses.create(31    model="gpt-6-astra",32    input="Write a one-sentence bedtime story about a unicorn.",33)3435print(response.output_text)36```3738```go39package main4041import (42	"context"43	"fmt"4445	"github.com/openai/openai-go/v3"46	"github.com/openai/openai-go/v3/responses"47)4849func main() {50	client := openai.NewClient()5152	resp, err := client.Responses.New(context.TODO(), responses.ResponseNewParams{53		Model: "gpt-6-astra",54		Input: responses.ResponseNewParamsInputUnion{OfString: openai.String("Say this is a test")},55	})56	if err != nil {57		panic(err.Error())58	}5960	fmt.Println(resp.OutputText())61}62```6364```java65import com.openai.client.OpenAIClient;66import com.openai.client.okhttp.OpenAIOkHttpClient;67import com.openai.models.responses.Response;68import com.openai.models.responses.ResponseCreateParams;6970public class Main {71  public static void main(String[] args) {72    OpenAIClient client = OpenAIOkHttpClient.fromEnv();7374    ResponseCreateParams params =75        ResponseCreateParams.builder().input("Say this is a test").model("gpt-6-astra").build();7677    Response response = client.responses().create(params);78    response.output().stream()79        .flatMap(item -> item.message().stream())80        .flatMap(message -> message.content().stream())81        .flatMap(content -> content.outputText().stream())82        .forEach(outputText -> System.out.println(outputText.text()));83  }84}85```8687```csharp88using OpenAI.Responses;89#pragma warning disable OPENAI0019091string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;92ResponsesClient client = new(key);9394ResponseResult response = await client.CreateResponseAsync(95    "gpt-6-astra",96    "Say 'this is a test.'"97);9899Console.WriteLine($"[ASSISTANT]: {response.GetOutputText()}");100```101102```ruby103require "openai"104105openai = OpenAI::Client.new106107response = openai.responses.create(108  model: "gpt-6-astra",109  input: "Write a one-sentence bedtime story about a unicorn."110)111112puts(response.output_text)113```114115```bash116openai responses create \117  --model "gpt-6-astra" \118  --input "Write a one-sentence bedtime story about a unicorn." \119  --raw-output \120  --transform 'output.#(type=="message").content.0.text'121```122123```bash124curl "https://api.openai.com/v1/responses" \125    -H "Content-Type: application/json" \126    -H "Authorization: Bearer $OPENAI_API_KEY" \127    -d '{128        "model": "gpt-6-astra",129        "input": "Write a one-sentence bedtime story about a unicorn."130    }'131```132133134An array of content generated by the model is in the `output` property of the response. In this simple example, we have just one output which looks like this:135136```json137[138  {139    "id": "msg_67b73f697ba4819183a15cc17d011509",140    "type": "message",141    "role": "assistant",142    "content": [143      {144        "type": "output_text",145        "text": "Under the soft glow of the moon, Luna the unicorn danced through fields of twinkling stardust, leaving trails of dreams for every child asleep.",146        "annotations": []147      }148    ]149  }150]151```152153**The `output` array often has more than one item in it!** It can contain tool calls, data about reasoning tokens generated by [reasoning models](https://developers.openai.com/api/docs/guides/reasoning), and other items. It is not safe to assume that the model's text output is present at `output[0].content[0].text`.154155Some of our [official SDKs](https://developers.openai.com/api/docs/libraries) include an `output_text` property on model responses for convenience, which aggregates all text outputs from the model into a single string. This may be useful as a shortcut to access text output from the model.156157In addition to plain text, you can also have the model return structured data in JSON format - this feature is called [**Structured Outputs**](https://developers.openai.com/api/docs/guides/structured-outputs).158159160161162163## Choosing a model164165A key choice to make when generating content through the API is which model you want to use - the `model` parameter of the code samples above. [You can find a full listing of available models here](https://developers.openai.com/api/docs/models). Here are a few factors to consider when choosing a model for text generation.166167- **[Reasoning models](https://developers.openai.com/api/docs/guides/reasoning)** generate an internal chain of thought to analyze the input prompt, and excel at understanding complex tasks and multi-step planning. They are also generally slower and more expensive to use than GPT models.168- **GPT models** are fast, cost-efficient, and highly intelligent, but benefit from more explicit instructions around how to accomplish tasks.169- **Large and small (mini or nano) models** offer trade-offs for speed, cost, and intelligence. Large models are more effective at understanding prompts and solving problems across domains, while small models are generally faster and cheaper to use.170171When in doubt, [`gpt-6-astra`](https://developers.openai.com/api/docs/models/gpt-6-astra) offers a strong default for general-purpose text generation and prompt iteration.172173## Prompt engineering174175**Prompt engineering** is the process of writing effective instructions for a model, such that it consistently generates content that meets your requirements.176177Because the content generated from a model is non-deterministic, prompting to get your desired output is a mix of art and science. However, you can apply techniques and best practices to get good results consistently.178179Some prompt engineering techniques work with every model, like using message roles. But different model types (like reasoning versus GPT models) might need to be prompted differently to produce the best results. Even different snapshots of models within the same family could produce different results. So as you build more complex applications, we strongly recommend:180181- Pinning your production applications to specific [model snapshots](https://developers.openai.com/api/docs/models) (like `gpt-4.1-2025-04-14` for example) to ensure consistent behavior182- Building tests and evaluation suites that measure prompt behavior so you can monitor performance as you iterate, or when you change and upgrade model versions183184Now, let's examine some tools and techniques available to you to construct prompts.185186## Message roles and instruction following187188189190You can provide instructions to the model with [differing levels of authority](https://model-spec.openai.com/2025-02-12.html#chain_of_command) using the `instructions` API parameter or **message roles**.191192The `instructions` parameter gives the model high-level instructions on how it should behave while generating a response, including tone, goals, and examples of correct responses. Any instructions provided this way will take priority over a prompt in the `input` parameter.193194Generate text with instructions195196```javascript197import OpenAI from "openai";198const client = new OpenAI();199200const response = await client.responses.create({201  model: "gpt-6-astra",202  reasoning: { effort: "low" },203  instructions: "Talk like a pirate.",204  input: "Are semicolons optional in JavaScript?",205});206207console.log(response.output_text);208```209210```python211from openai import OpenAI212213client = OpenAI()214215response = client.responses.create(216    model="gpt-6-astra",217    reasoning={"effort": "low"},218    instructions="Talk like a pirate.",219    input="Are semicolons optional in JavaScript?",220)221222print(response.output_text)223```224225```go226package main227228import (229	"context"230	"fmt"231232	"github.com/openai/openai-go/v3"233	"github.com/openai/openai-go/v3/responses"234)235236func main() {237	client := openai.NewClient()238239	response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{240		Model:        "gpt-6-astra",241		Instructions: openai.String("Talk like a pirate."),242		Reasoning: responses.ReasoningParam{243			Effort: responses.ReasoningEffortLow,244		},245		Input: responses.ResponseNewParamsInputUnion{246			OfString: openai.String("Are semicolons optional in JavaScript?"),247		},248	})249	if err != nil {250		panic(err)251	}252253	fmt.Println(response.OutputText())254}255```256257```java258import com.openai.client.OpenAIClient;259import com.openai.client.okhttp.OpenAIOkHttpClient;260import com.openai.models.Reasoning;261import com.openai.models.ReasoningEffort;262import com.openai.models.responses.ResponseCreateParams;263264String semicolonsDevMsg = "Talk like a pirate.";265266String semicolonsPrompt = "Are semicolons optional in JavaScript?";267268ResponseCreateParams params =269    ResponseCreateParams.builder()270        .model("gpt-6-astra")271        .input(semicolonsPrompt)272        .instructions(semicolonsDevMsg)273        .reasoning(Reasoning.builder().effort(ReasoningEffort.LOW).build())274        .build();275276client.responses().create(params).output().stream()277    .flatMap(item -> item.message().stream())278    .flatMap(message -> message.content().stream())279    .flatMap(content -> content.outputText().stream())280    .forEach(text -> System.out.println(text.text()));281```282283```csharp284using OpenAI.Responses;285#pragma warning disable OPENAI001286287string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;288ResponsesClient client = new(key);289290CreateResponseOptions options = new()291{292    Model = "gpt-6-astra",293    Instructions = "Talk like a pirate.",294    ReasoningOptions = new ResponseReasoningOptions295    {296        ReasoningEffortLevel = ResponseReasoningEffortLevel.Low,297    },298};299options.InputItems.Add(300    ResponseItem.CreateUserMessageItem("Are semicolons optional in JavaScript?")301);302303ResponseResult response = await client.CreateResponseAsync(options);304305Console.WriteLine(response.GetOutputText());306```307308```ruby309require "openai"310311client = OpenAI::Client.new312response = client.responses.create(313  model: "gpt-6-astra",314  instructions: "Talk like a pirate.",315  reasoning: { effort: :low },316  input: "Are semicolons optional in JavaScript?"317)318319puts(response.output_text)320```321322```bash323curl "https://api.openai.com/v1/responses" \324    -H "Content-Type: application/json" \325    -H "Authorization: Bearer $OPENAI_API_KEY" \326    -d '{327        "model": "gpt-6-astra",328        "reasoning": {"effort": "low"},329        "instructions": "Talk like a pirate.",330        "input": "Are semicolons optional in JavaScript?"331    }'332```333334335The example above is roughly equivalent to using the following input messages in the `input` array:336337Generate text with messages using different roles338339```javascript340import OpenAI from "openai";341const client = new OpenAI();342343const response = await client.responses.create({344  model: "gpt-6-astra",345  reasoning: { effort: "low" },346  input: [347    {348      role: "developer",349      content: "Talk like a pirate.",350    },351    {352      role: "user",353      content: "Are semicolons optional in JavaScript?",354    },355  ],356});357358console.log(response.output_text);359```360361```python362from openai import OpenAI363364client = OpenAI()365366response = client.responses.create(367    model="gpt-6-astra",368    reasoning={"effort": "low"},369    input=[370        {"role": "developer", "content": "Talk like a pirate."},371        {"role": "user", "content": "Are semicolons optional in JavaScript?"},372    ],373)374375print(response.output_text)376```377378```go379package main380381import (382	"context"383	"fmt"384385	"github.com/openai/openai-go/v3"386	"github.com/openai/openai-go/v3/responses"387)388389func main() {390	client := openai.NewClient()391392	response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{393		Model: "gpt-6-astra",394		Reasoning: responses.ReasoningParam{395			Effort: responses.ReasoningEffortLow,396		},397		Input: responses.ResponseNewParamsInputUnion{398			OfInputItemList: responses.ResponseInputParam{399				responses.ResponseInputItemParamOfMessage(400					"Talk like a pirate.",401					responses.EasyInputMessageRoleDeveloper,402				),403				responses.ResponseInputItemParamOfMessage(404					"Are semicolons optional in JavaScript?",405					responses.EasyInputMessageRoleUser,406				),407			},408		},409	})410	if err != nil {411		panic(err)412	}413414	fmt.Println(response.OutputText())415}416```417418```java419import com.openai.client.OpenAIClient;420import com.openai.client.okhttp.OpenAIOkHttpClient;421import com.openai.models.Reasoning;422import com.openai.models.ReasoningEffort;423import com.openai.models.responses.EasyInputMessage;424import com.openai.models.responses.ResponseCreateParams;425import com.openai.models.responses.ResponseInputItem;426import java.util.List;427428String semicolonsDevMsg = "Talk like a pirate.";429430String semicolonsPrompt = "Are semicolons optional in JavaScript?";431432ResponseCreateParams params =433    ResponseCreateParams.builder()434        .model("gpt-6-astra")435        .input(436            ResponseCreateParams.Input.ofResponse(437                List.of(438                    ResponseInputItem.ofEasyInputMessage(439                        EasyInputMessage.builder()440                            .role(EasyInputMessage.Role.DEVELOPER)441                            .content(semicolonsDevMsg)442                            .build()),443                    ResponseInputItem.ofEasyInputMessage(444                        EasyInputMessage.builder()445                            .role(EasyInputMessage.Role.USER)446                            .content(semicolonsPrompt)447                            .build()))))448        .reasoning(Reasoning.builder().effort(ReasoningEffort.LOW).build())449        .build();450451client.responses().create(params).output().stream()452    .flatMap(item -> item.message().stream())453    .flatMap(message -> message.content().stream())454    .flatMap(content -> content.outputText().stream())455    .forEach(text -> System.out.println(text.text()));456```457458```csharp459using OpenAI.Responses;460#pragma warning disable OPENAI001461462string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;463ResponsesClient client = new(key);464465CreateResponseOptions options = new()466{467    Model = "gpt-6-astra",468    ReasoningOptions = new ResponseReasoningOptions469    {470        ReasoningEffortLevel = ResponseReasoningEffortLevel.Low,471    },472};473options.InputItems.Add(474    ResponseItem.CreateDeveloperMessageItem("Talk like a pirate.")475);476options.InputItems.Add(477    ResponseItem.CreateUserMessageItem("Are semicolons optional in JavaScript?")478);479480ResponseResult response = await client.CreateResponseAsync(options);481482Console.WriteLine(response.GetOutputText());483```484485```ruby486require "openai"487488client = OpenAI::Client.new489response = client.responses.create(490  model: "gpt-6-astra",491  reasoning: { effort: :low },492  input: [493    {494      role: :developer,495      content: "Talk like a pirate."496    },497    {498      role: :user,499      content: "Are semicolons optional in JavaScript?"500    }501  ]502)503504puts(response.output_text)505```506507```bash508curl "https://api.openai.com/v1/responses" \509    -H "Content-Type: application/json" \510    -H "Authorization: Bearer $OPENAI_API_KEY" \511    -d '{512        "model": "gpt-6-astra",513        "reasoning": {"effort": "low"},514        "input": [515            {516                "role": "developer",517                "content": "Talk like a pirate."518            },519            {520                "role": "user",521                "content": "Are semicolons optional in JavaScript?"522            }523        ]524    }'525```526527528Note that the `instructions` parameter only applies to the current response generation request. If you are [managing conversation state](https://developers.openai.com/api/docs/guides/conversation-state) with the `previous_response_id` parameter, the `instructions` used on previous turns will not be present in the context.529530531532533534The [OpenAI model spec](https://model-spec.openai.com/2025-02-12.html#chain_of_command) describes how our models give different levels of priority to messages with different roles.535536| `developer`                                                                                                        | `user`                                                                                             | `assistant`                                                |537| ------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------- | ---------------------------------------------------------- |538| `developer` messages are instructions provided by the application developer, prioritized ahead of `user` messages. | `user` messages are instructions provided by an end user, prioritized behind `developer` messages. | Messages generated by the model have the `assistant` role. |539540A multi-turn conversation may consist of several messages of these types, along with other content types provided by both you and the model. Learn more about [managing conversation state here](https://developers.openai.com/api/docs/guides/conversation-state).541542You could think about `developer` and `user` messages like a function and its arguments in a programming language.543544- `developer` messages provide the system's rules and business logic, like a function definition.545- `user` messages provide inputs and configuration to which the `developer` message instructions are applied, like arguments to a function.546547## Version prompts in code548549Store production prompts in your application code instead of creating reusable prompt objects. Code-managed prompts let you use typed inputs, code review, tests, and your normal deployment process to change model behavior.550551OpenAI is deprecating reusable prompt objects in the API. Prompt creation will552  be de-emphasized beginning June 3, 2026, and `v1/prompts` is scheduled to shut553  down on November 30, 2026. See the [deprecations554  page](https://developers.openai.com/api/docs/deprecations#2026-06-03-reusable-prompts) for the current555  timeline.556557For new prompt-engineering work:558559- Keep prompt builders in a small module near the feature they support.560- Use typed function arguments or schemas for dynamic values such as customer data, files, or task options.561- Pass the generated `instructions` and `input` directly to the [Responses API](https://developers.openai.com/api/reference/resources/responses/methods/create).562- Add representative fixtures, tests, and evaluation checks before changing production prompts.563- Roll out prompt changes through your deployment system, using feature flags or configuration when you need staged releases.564565If your integration already calls a saved prompt with a prompt ID or version, use the [prompt object migration guide](https://developers.openai.com/api/docs/guides/prompting/migrate-from-prompt-object) to move that prompt into code.566567## Message formatting with Markdown and XML568569When writing `developer` and `user` messages, you can help the model understand logical boundaries of your prompt and context data using a combination of [Markdown](https://commonmark.org/help/) formatting and [XML tags](https://www.w3.org/TR/xml/).570571Markdown headers and lists can be helpful to mark distinct sections of a prompt, and to communicate hierarchy to the model. They can also potentially make your prompts more readable during development. XML tags can help delineate where one piece of content (like a supporting document used for reference) begins and ends. XML attributes can also be used to define metadata about content in the prompt that can be referenced by your instructions.572573In general, a developer message will contain the following sections, usually in this order (though the exact optimal content and order may vary by which model you are using):574575- **Identity:** Describe the purpose, communication style, and high-level goals of the assistant.576- **Instructions:** Provide guidance to the model on how to generate the response you want. What rules should it follow? What should the model do, and what should the model never do? This section could contain many subsections as relevant for your use case, like how the model should [call custom functions](https://developers.openai.com/api/docs/guides/function-calling).577- **Examples:** Provide examples of possible inputs, along with the desired output from the model.578- **Context:** Give the model any additional information it might need to generate a response, like private/proprietary data outside its training data, or any other data you know will be particularly relevant. This content is usually best positioned near the end of your prompt, as you may include different context for different generation requests.579580Below is an example of using Markdown and XML tags to construct a `developer` message with distinct sections and supporting examples.581582583584Example prompt585586    A developer message for code generation587588```text589# Identity590591You are coding assistant that helps enforce the use of snake case592variables in JavaScript code, and writing code that will run in593Internet Explorer version 6.594595# Instructions596597* When defining variables, use snake case names (e.g. my_variable)598  instead of camel case names (e.g. myVariable).599* To support old browsers, declare variables using the older600  "var" keyword.601* Do not give responses with Markdown formatting, just return602  the code as requested.603604# Examples605606<user_query>607How do I declare a string variable for a first name?608</user_query>609610<assistant_response>611var first_name = "Anna";612</assistant_response>613```614615  616617  618619    620API request621622    Send a prompt to generate code through the API623624```javascript625import fs from "fs/promises";626import OpenAI from "openai";627const client = new OpenAI();628629const instructions = await fs.readFile("fixtures/prompt.txt", "utf-8");630631const response = await client.responses.create({632  model: "gpt-6-astra",633  instructions,634  input: "How would I declare a variable for a last name?",635});636637console.log(response.output_text);638```639640```python641from openai import OpenAI642643client = OpenAI()644645with open("prompt.txt", "r", encoding="utf-8") as f:646    instructions = f.read()647648response = client.responses.create(649    model="gpt-6-astra",650    instructions=instructions,651    input="How would I declare a variable for a last name?",652)653654print(response.output_text)655```656657```go658package main659660import (661	"context"662	"fmt"663	"os"664665	"github.com/openai/openai-go/v3"666	"github.com/openai/openai-go/v3/responses"667)668669func main() {670	client := openai.NewClient()671672	instructions, err := os.ReadFile("prompt.txt")673	if err != nil {674		panic(err)675	}676677	response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{678		Model:        "gpt-6-astra",679		Instructions: openai.String(string(instructions)),680		Input: responses.ResponseNewParamsInputUnion{681			OfString: openai.String("How would I declare a variable for a last name?"),682		},683	})684	if err != nil {685		panic(err)686	}687688	fmt.Println(response.OutputText())689}690```691692```java693import com.openai.client.OpenAIClient;694import com.openai.client.okhttp.OpenAIOkHttpClient;695import com.openai.models.responses.ResponseCreateParams;696697ResponseCreateParams params =698    ResponseCreateParams.builder()699        .model("gpt-6-astra")700        .instructions(701            "You are a coding assistant. Answer with concise JavaScript examples and use semicolons.")702        .input("How would I declare a variable for a last name?")703        .build();704705client.responses().create(params).output().stream()706    .flatMap(item -> item.message().stream())707    .flatMap(message -> message.content().stream())708    .flatMap(content -> content.outputText().stream())709    .forEach(text -> System.out.println(text.text()));710```711712```csharp713using OpenAI.Responses;714#pragma warning disable OPENAI001715716string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;717ResponsesClient client = new(key);718719string instructions = await File.ReadAllTextAsync("prompt.txt");720CreateResponseOptions options = new()721{722    Model = "gpt-6-astra",723    Instructions = instructions,724};725options.InputItems.Add(726    ResponseItem.CreateUserMessageItem("How would I declare a variable for a last name?")727);728729ResponseResult response = await client.CreateResponseAsync(options);730Console.WriteLine(response.GetOutputText());731```732733```ruby734require "openai"735736client = OpenAI::Client.new737instructions = File.read(File.join(__dir__, "prompt.txt"))738response = client.responses.create(739  model: "gpt-6-astra",740  instructions: instructions,741  input: "How would I declare a variable for a last name?"742)743744puts(response.output_text)745```746747```bash748curl https://api.openai.com/v1/responses \749  -H "Authorization: Bearer $OPENAI_API_KEY" \750  -H "Content-Type: application/json" \751  -d '{752    "model": "gpt-6-astra",753    "instructions": "'"$(< prompt.txt)"'",754    "input": "How would I declare a variable for a last name?"755  }'756```757758759760#### Save on cost and latency with prompt caching761762When constructing a message, you should try and keep content that you expect to use over and over in your API requests at the beginning of your prompt, **and** among the first API parameters you pass in the JSON request body to [Chat Completions](https://developers.openai.com/api/reference/resources/chat) or [Responses](https://developers.openai.com/api/reference/resources/responses). This enables you to maximize cost and latency savings from [prompt caching](https://developers.openai.com/api/docs/guides/prompt-caching).763764## Few-shot learning765766Few-shot learning lets you steer a large language model toward a new task by including a handful of input/output examples in the prompt, rather than [fine-tuning](https://developers.openai.com/api/docs/guides/model-optimization) the model. The model implicitly "picks up" the pattern from those examples and applies it to a prompt. When providing examples, try to show a diverse range of possible inputs with the desired outputs.767768Typically, you will provide examples as part of a `developer` message in your API request. Here's an example `developer` message containing examples that show a model how to classify positive or negative customer service reviews.769770```771# Identity772773You are a helpful assistant that labels short product reviews as774Positive, Negative, or Neutral.775776# Instructions777778* Only output a single word in your response with no additional formatting779  or commentary.780* Your response should only be one of the words "Positive", "Negative", or781  "Neutral" depending on the sentiment of the product review you are given.782783# Examples784785<product_review id="example-1">786I absolutely love this headphones — sound quality is amazing!787</product_review>788789<assistant_response id="example-1">790Positive791</assistant_response>792793<product_review id="example-2">794Battery life is okay, but the ear pads feel cheap.795</product_review>796797<assistant_response id="example-2">798Neutral799</assistant_response>800801<product_review id="example-3">802Terrible customer service, I'll never buy from them again.803</product_review>804805<assistant_response id="example-3">806Negative807</assistant_response>808```809810## Include relevant context information811812It is often useful to include additional context information the model can use to generate a response within the prompt you give the model. There are a few common reasons why you might do this:813814- To give the model access to proprietary data, or any other data outside the data set the model was trained on.815- To constrain the model's response to a specific set of resources that you have determined will be most beneficial.816817The technique of adding additional relevant context to the model generation request is sometimes called **retrieval-augmented generation (RAG)**. You can add additional context to the prompt in many different ways, from querying a vector database and including the text you get back into a prompt, or by using OpenAI's built-in [file search tool](https://developers.openai.com/api/docs/guides/tools-file-search) to generate content based on uploaded documents.818819#### Planning for the context window820821Models can only handle so much data within the context they consider during a generation request. This memory limit is called a **context window**, which is defined in terms of [tokens](https://blogs.nvidia.com/blog/ai-tokens-explained) (chunks of data you pass in, from text to images).822823Models have different context window sizes from the low 100k range up to one million tokens for newer GPT-4.1 models. [Refer to the model docs](https://developers.openai.com/api/docs/models) for specific context window sizes per model.824825826827828## Prompting current models829830GPT models like [`gpt-6-astra`](https://developers.openai.com/api/docs/models/gpt-6-astra) benefit from precise instructions that explicitly provide the logic and data required to complete the task in the prompt. To get the most out of the latest model, start with the current prompting guide.831832[833834      Get the most out of prompting the latest model with current guidance,835    practical examples, and migration notes.](https://developers.openai.com/api/docs/guides/latest-model)836837838839840### Prompting best practices for the latest model841842For the full current treatment, use the [latest model prompting best practices](https://developers.openai.com/api/docs/guides/latest-model). The practical reminders below still apply.843844845846#### Coding847848849850#### Coding851852Prompting `gpt-6-astra` for coding tasks is most effective when following a few best practices: define the agent's role, enforce structured tool use with examples, require thorough testing for correctness, and set Markdown standards for clean output.853854**Explicit role and workflow guidance**855Frame the model as a software engineering agent with well-defined responsibilities. Provide clear instructions for using tools like `functions.run` for code tasks, and specify when not to use certain modes—for example, avoid interactive execution unless necessary.856857**Testing and validation**858Instruct the model to test changes with unit tests or Python commands, and validate patches carefully since tools like `apply_patch` may return “Done” even on failure.859860**Tool use examples**861Include concrete examples of how to invoke commands with the provided functions, which improves reliability and adherence to expected workflows.862863**Markdown standards**864Guide the model to generate clean, semantically correct markdown using inline code, code fences, lists, and tables where appropriate—and to format file paths, functions, and classes with backticks.865866For detailed guidance and prompt samples specific to coding, see the [latest model prompting best practices](https://developers.openai.com/api/docs/guides/latest-model).867868869870871872873874#### Front-end engineering875876877878[GPT-6 Astra](https://developers.openai.com/api/docs/models/gpt-6-astra)879performs well at building front ends from scratch as well as contributing to880large, established codebases. To get the best results, we recommend using the881following libraries:882883- **Styling / UI:** Tailwind CSS, shadcn/ui, Radix Themes884- **Icons:** Lucide, Material Symbols, Heroicons885- **Animation**: Motion886887**Zero-to-one web apps**888889GPT-5 can generate front-end web apps from a single prompt, no examples needed. Here's a sample prompt:890891```bash892You are a world class web developer, capable of producing stunning, interactive, and innovative websites from scratch in a single prompt. You excel at delivering top-tier one-shot solutions.893Your process is simple and follows these steps:894Step 1: Create an evaluation rubric and refine it until you are fully confident.895Step 2: Consider every element that defines a world-class one-shot web app, then use that insight to create a &lt;ONE_SHOT_RUBRIC&gt; with 5–7 categories. Keep this rubric hidden—it's for internal use only.896Step 3: Apply the rubric to iterate on the optimal solution to the given prompt. If it doesn't meet the highest standard across all categories, refine and try again.897Step 4: Aim for simplicity while fully achieving the goal, and avoid external dependencies such as Next.js or React.898```899900**Integration with large codebases**901902For front-end engineering work in larger codebases, we've found that adding these categories of instruction to your prompts delivers the best results:903904- **Principles:** Set visual quality standards, use modular/reusable components, and keep design consistent.905- **UI/UX:** Specify typography, colors, spacing/layout, interaction states (hover, empty, loading), and accessibility.906- **Structure:** Define file/folder layout for seamless integration.907- **Components:** Give reusable wrapper examples and backend-call separation strategies.908- **Pages:** Provide templates for common layouts.909- **Agent Instructions:** Ask the model to confirm design assumptions, scaffold projects, enforce standards, integrate APIs, test states, and document code.910911For detailed guidance and prompt samples specific to frontend development, see the [latest model prompting best practices](https://developers.openai.com/api/docs/guides/latest-model).912913914915916917918919#### Agentic tasks920921922923For agentic and long-running rollouts with `gpt-6-astra`, focus your prompts on three core practices: plan tasks thoroughly to ensure complete resolution, provide clear preambles for major tool usage decisions, and use a TODO tool to track workflow and progress in an organized manner.924925**Planning and persistence**926Instruct the model to resolve the full query before yielding control, decomposing it into sub-tasks and reflecting after each tool call to confirm completeness.927928```929Remember, you are an agent - please keep going until the user's930query is completely resolved, before ending your turn and yielding931back to the user. Decompose the user's query into all required932sub-requests, and confirm that each is completed. Do not stop933after completing only part of the request. Only terminate your934turn when you are sure that the problem is solved. You must be935prepared to answer multiple queries and only finish the call once936the user has confirmed they're done.937938You must plan extensively in accordance with the workflow939steps before making subsequent function calls, and reflect940extensively on the outcomes each function call made,941ensuring the user's query, and related sub-requests942are completely resolved.943```944945**Preambles for transparency**946947Ask the model to explain why it is calling a tool, but only at notable steps.948949```950Before you call a tool explain why you are calling it951```952953**Progress tracking with rubrics and TODOs**954955Use a TODO list tool or rubric to enforce structured planning and avoid missed steps.956957For detailed guidance and prompt samples specific to building agents, see the [latest model prompting best practices](https://developers.openai.com/api/docs/guides/latest-model).958959960961962963## Prompting reasoning models964965There are some differences to consider when prompting a [reasoning model](https://developers.openai.com/api/docs/guides/reasoning) versus prompting a GPT model. Generally speaking, reasoning models will provide better results on tasks with only high-level guidance. This differs from GPT models, which benefit from very precise instructions.966967You could think about the difference between reasoning and GPT models like this.968969- A reasoning model is like a senior co-worker. You can give them a goal to achieve and trust them to work out the details.970- A GPT model is like a junior coworker. They'll perform best with explicit instructions to create a specific output.971972For more information on best practices when using reasoning models, [refer to this guide](https://developers.openai.com/api/docs/guides/reasoning-best-practices).973974## Next steps975976Now that you know the basics of text inputs and outputs, you might want to check out one of these resources next.977978[Build a prompt in the Playground979980981982      Use the Playground to develop and iterate on prompts.](https://platform.openai.com/chat/edit)983984[Generate JSON data with Structured Outputs985986987988      Ensure JSON data emitted from a model conforms to a JSON schema.](https://developers.openai.com/api/docs/guides/structured-outputs)989990[Full API reference991992993994      Check out all the options for text generation in the API reference.](https://developers.openai.com/api/reference/resources/responses)995996## Other resources997998For more inspiration, visit the [OpenAI Cookbook](https://developers.openai.com/cookbook), which contains example code and also links to third-party resources such as:9991000- [Prompting libraries & tools](https://developers.openai.com/cookbook/articles/related_resources#prompting-libraries--tools)1001- [Prompting guides](https://developers.openai.com/cookbook/articles/related_resources#prompting-guides)1002- [Video courses](https://developers.openai.com/cookbook/articles/related_resources#video-courses)1003- [Papers on advanced prompting to improve reasoning](https://developers.openai.com/cookbook/articles/related_resources#papers-on-advanced-prompting-to-improve-reasoning)
Files main
Menu

Theme