SPB Git

Repository Public

model-optimization.md 10.4 KB 232 lines markdown

1# Model optimization23> For the complete documentation index, see [llms.txt](/llms.txt). Markdown versions of documentation pages are available by appending `.md` to the page URL.45LLM output is non-deterministic, and model behavior changes between model snapshots and families. Developers must constantly measure and tune the performance of LLM applications to ensure they're getting the best results. In this guide, we explore the techniques and OpenAI platform tools you can use to ensure high quality outputs from the model.67This guide covers evals and fine-tuning workflows that are being moved into8  legacy documentation. See the [deprecations page](https://developers.openai.com/api/docs/deprecations) for9  the current timelines for the affected platform surfaces.10111213  - **[Evals](https://developers.openai.com/api/docs/guides/evals)**: Systematically measure performance.14- **[Prompt engineering](https://developers.openai.com/api/docs/guides/text?api-mode=responses#prompt-engineering)**: Give context, instructions, and goals.15- **[Fine-tuning](https://developers.openai.com/api/docs/guides/supervised-fine-tuning)**: Train models to excel at a task.16171819## Model optimization workflow2021Optimizing model output requires a combination of **evals**, **prompt engineering**, and **fine-tuning**, creating a flywheel of feedback that leads to better prompts and better training data for fine-tuning. The optimization process usually goes something like this.22231. Write [evals](https://developers.openai.com/api/docs/guides/evals) that measure model output, establishing a baseline for performance and accuracy.241. [Prompt the model](https://developers.openai.com/api/docs/guides/text) for output, providing relevant context data and instructions.251. For some use cases, it may be desirable to [fine-tune](#fine-tune-a-model) a model for a specific task.261. Run evals using test data that is representative of real world inputs. Measure the performance of your prompt and fine-tuned model.271. Tweak your prompt or fine-tuning dataset based on eval feedback.281. Repeat the loop continuously to improve your model results.2930Here's an overview of the major steps, and how to do them using the OpenAI platform.3132## Build evals3334In the OpenAI platform, you can [build and run evals](https://developers.openai.com/api/docs/guides/evals) either via API or in the [dashboard](https://platform.openai.com/evaluations). You might even consider writing evals _before_ you start writing prompts, taking an approach akin to behavior-driven development (BDD).3536Run your evals against test inputs like you expect to see in production. Using one of several available [graders](https://developers.openai.com/api/docs/guides/graders), measure the results of a prompt against your test data set.3738[Learn about evals39404142      Run tests on your model outputs to ensure you're getting the right results.](https://developers.openai.com/api/docs/guides/evals)4344## Write effective prompts4546With evals in place, you can effectively iterate on [prompts](https://developers.openai.com/api/docs/guides/text). The prompt engineering process may be all you need in order to get great results for your use case. Different models may require different prompting techniques, but there are several best practices you can apply across the board to get better results.4748- **Include relevant context** - in your instructions, include text or image content that the model will need to generate a response from outside its training data. This could include data from private databases or current, up-to-the-minute information.49- **Provide clear instructions** - your prompt should contain clear goals about what kind of output you want. Start with [`gpt-6-astra`](https://developers.openai.com/api/docs/models/gpt-6-astra) for new work, and use [reasoning model guidance](https://developers.openai.com/api/docs/guides/reasoning) to tune outcome-level instructions, reasoning effort, and verbosity.50- **Provide example outputs** - give the model a few examples of correct output for a given prompt (a process called few-shot learning). The model can extrapolate from these examples how it should respond for other prompts.5152[Learn about prompt engineering53545556      Learn the basics of writing good prompts for the model.](https://developers.openai.com/api/docs/guides/text)5758## Fine-tune a model5960OpenAI is winding down the fine-tuning platform. The platform is no longer61  accessible to new users, but existing users of the fine-tuning platform will62  be able to create training jobs for the coming months.63  6465  All fine-tuned models will remain available for inference until their base66  models are [deprecated](https://developers.openai.com/api/docs/deprecations). The full timeline is67  [here](https://developers.openai.com/api/docs/deprecations).6869OpenAI models are already pre-trained to perform across a broad range of subjects and tasks. Fine-tuning lets you take an OpenAI base model, provide the kinds of inputs and outputs you expect in your application, and get a model that excels in the tasks you'll use it for.7071Fine-tuning can be a time-consuming process, but it can also enable a model to consistently format responses in a certain way or handle novel inputs. You can use fine-tuning with [prompt engineering](https://developers.openai.com/api/docs/guides/text) to realize a few more benefits over prompting alone:7273- You can provide more example inputs and outputs than could fit within the context window of a single request, enabling the model handle a wider variety of prompts.74- You can use shorter prompts with fewer examples and context data, which saves on token costs at scale and can be lower latency.75- You can train on proprietary or sensitive data without having to include it via examples in every request.76- You can train a smaller, cheaper, faster model to excel at a particular task where a larger model is not cost-effective.7778Visit our [pricing page](https://openai.com/api/pricing) to learn more about how fine-tuned model training and usage are billed.7980### Fine-tuning methods8182These are the fine-tuning methods supported in the OpenAI platform today.83848586<table>87<tbody>88<tr>89<th>Method</th>90<th>How it works</th>91<th>Best for</th>92<th>Use with</th>93</tr>9495<tr>96<td>97[Supervised fine-tuning (SFT)](https://developers.openai.com/api/docs/guides/supervised-fine-tuning)98</td>99<td>100Provide examples of correct responses to prompts to guide the model's behavior.101102Often uses human-generated "ground truth" responses to show the model how it should respond.103104</td>105<td>106- Classification107- Nuanced translation108- Generating content in a specific format109- Correcting instruction-following failures110</td>111<td>112`gpt-4.1-2025-04-14`113`gpt-4.1-mini-2025-04-14`114`gpt-4.1-nano-2025-04-14`115</td>116</tr>117118<tr>119  <td>[Vision fine-tuning](https://developers.openai.com/api/docs/guides/vision-fine-tuning)</td>120  <td>121    Provide image inputs for supervised fine-tuning to improve the model's122    understanding of image inputs.123  </td>124  <td>125    - Image classification - Correcting failures in instruction following for126    complex prompts127  </td>128  <td>`gpt-4o-2024-08-06`</td>129</tr>130131<tr>132  <td>133    [Direct preference optimization134    (DPO)](https://developers.openai.com/api/docs/guides/direct-preference-optimization)135  </td>136  <td>137    Provide both a correct and incorrect example response for a prompt. Indicate138    the correct response to help the model perform better.139  </td>140  <td>141    - Summarizing text, focusing on the right things - Generating chat messages142    with the right tone and style143  </td>144  <td>145    `gpt-4.1-2025-04-14` `gpt-4.1-mini-2025-04-14` `gpt-4.1-nano-2025-04-14`146  </td>147</tr>148149<tr>150<td>151[Reinforcement fine-tuning (RFT)](https://developers.openai.com/api/docs/guides/reinforcement-fine-tuning)152</td>153<td>154Generate a response for a prompt, provide an expert grade for the result, and reinforce the model's chain-of-thought for higher-scored responses.155156Requires expert graders to agree on the ideal output from the model.157158**Reasoning models only**.159160</td>161<td>162- Complex domain-specific tasks that require advanced reasoning163- Medical diagnoses based on history and diagnostic guidelines164- Determining relevant passages from legal case law165</td>166<td>167`o4-mini-2025-04-16`168</td>169</tr>170</tbody>171</table>172173174175### How fine-tuning works176177In the OpenAI platform, you can create fine-tuned models either in the [dashboard](https://platform.openai.com/finetune) or [with the API](https://developers.openai.com/api/reference/resources/fine_tuning). This is the general shape of the fine-tuning process:1781791. Collect a dataset of examples to use as training data1801. Upload that dataset to OpenAI, formatted in JSONL1811. Create a fine-tuning job using one of the methods above, depending on your goals—this begins the fine-tuning training process1821. In the case of RFT, you'll also define a grader to score the model's behavior1831. Evaluate the results184185Get started with [supervised fine-tuning](https://developers.openai.com/api/docs/guides/supervised-fine-tuning), [vision fine-tuning](https://developers.openai.com/api/docs/guides/vision-fine-tuning), [direct preference optimization](https://developers.openai.com/api/docs/guides/direct-preference-optimization), or [reinforcement fine-tuning](https://developers.openai.com/api/docs/guides/reinforcement-fine-tuning).186187## Learn from experts188189Model optimization is a complex topic, and sometimes more art than science. Check out the videos below from members of the OpenAI team on model optimization techniques.190191192193Cost/accuracy/latency194195    <iframe196      width="100%"197      height="400"198      src="https://www.youtube.com/embed/Bx6sUDRMx-8?si=i3Tl8qEjlCdOtyiU"199      title="YouTube video player"200      allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"201      allowFullScreen202    ></iframe>203  204205  206207    208Distillation209210    <iframe211      width="100%"212      height="400"213      src="https://www.youtube.com/embed/CqWpJFK-hOo?si=7ztgDp1inte0vnw7"214      title="YouTube video player"215      allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"216      allowFullScreen217    ></iframe>218  219220  221222    223Optimizing LLM Performance224225    <iframe226      width="100%"227      height="400"228      src="https://www.youtube-nocookie.com/embed/ahnGLM-RC1Y?si=cPQngClssVG_R2_q"229      title="YouTube video player"230      allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"231      allowFullScreen232    ></iframe>
Files main
Menu

Theme