Optimizing prompts with promptfoo optimize

promptfoo optimize improves one configured prompt against one configured provider. This page explains when teams use it, how to configure candidate search, validation and target metrics, and how to review results before adopting a new prompt.

On this page

When teams use promptfoo optimize to improve a prompt

A team usually reaches for promptfoo optimize when a prompt that once worked has started to slip, or has stopped improving no matter how much manual editing goes into it. The outputs look reasonable in spot checks, but the eval suite shows scores flattening or drifting after a model or data change. In that situation the team wants a measured way to search for a better prompt, using its own eval data rather than intuition. promptfoo optimize answers that need directly: it improves one configured prompt against one configured provider, scored by the team's own tests.

The scope is narrow, and the boundaries matter for planning. The command does not change the model, the application code or the test suite. The provider stays fixed, so the result applies to that model and its settings only, and a change of model means the work should be repeated. The tests are also an input, not an output: if the assertions are weak or the dataset is unrepresentative, the optimizer will tune toward the wrong target. Teams should review their assertions and cases before they start, not after the first run looks encouraging.

promptfoo optimize also does not decide whether a candidate prompt goes to production. That decision stays with the team, which must compare the new prompt against the current one on data the search did not see, check failure cases by hand, and confirm that the gains are not coming from a narrow set of test inputs. Before starting, confirm with your own engineering and evaluation leads who owns that sign-off and what evidence they will require. The command's documentation in the promptfoo docs: Prompt optimization page is the place to confirm how its search and validation steps behave in the version you run.

Configuration options and when each fits

This table compares the main choices in a promptfoo optimize run: the starting prompt, the provider, the data split, the target metric and the search limits. Read each row for when the option fits and what it costs you, then confirm exact config keys in the command line and prompt optimization docs before you write a config.

OptionWhen it fitsTrade-offWatch for
Starting prompt and variablesA prompt that already works partly and has clear variable slots the tests fill inA weak starting prompt limits what the search can reach, and a strong one may leave little room to improveVariables that appear in the test data but not in the prompt, which silently change what is graded
Single provider under optimizationOne target model you plan to ship, so results reflect the model you will runGains rarely transfer cleanly to a different model, so a later model switch may need a new runProvider settings such as temperature that differ between the optimize run and production
Tuning and validation splitA dataset large enough that some cases can be held back for checking the chosen promptA small set invites overfitting, while a large validation split leaves fewer cases to learn fromValidation cases that resemble tuning cases too closely, which hides overfitting
Assertion-based pass rate as targetOutputs with objective checks such as required fields, exact labels or banned phrasesStrict checks can reject acceptable phrasing and push the prompt toward rigid wordingAssertions that encode one correct phrasing when several are acceptable
Graded score as targetOpen-ended outputs where quality is a matter of degree, judged by a model grader or a scored rubricGraders add cost and variance, and the score can reward length or style over correctnessA grader that shares a model family with the target, and a score scale nobody has reviewed
Iteration and candidate limitsA first run to check the setup, then larger runs once the metric and data are trustedMore candidates and iterations can find better prompts but add model calls and wall-clock timeRuns that stop early on a metric that is easy to game, so review the best candidates by hand
Review before adoptionEvery run, so you see which candidate won and why before replacing the live promptManual review takes reviewer time that a fully automatic swap would skipAccepting the top score without reading outputs for edge cases and policy wording

Setting up a first optimization run

This sequence takes one prompt and one provider from a working eval to a reviewed optimization result, with a check at each stage so you know the baseline is sound before any candidate is generated. The output is a set of candidate prompts scored on held-out data that your team can accept or reject.

  1. Install promptfoo and confirm Node.js

    Install the promptfoo package from npm and confirm that your Node.js version meets the requirement for the release you are installing. The npm release 0.124.1, published on 8 October 2026, requires Node.js 22.22.0 or newer, so check your runtime with node --version before installing. This step is done when the promptfoo command runs locally and reports the version you pinned for the team.

  2. Define one prompt and one provider

    Create a configuration file with a single prompt and a single provider, since promptfoo optimize improves one configured prompt against one configured provider. Keep the prompt file and provider settings in version control so that each run can be traced back to the exact inputs. This step is done when the config loads without errors and the provider credentials are read from your environment rather than written into the file.

  3. Write a representative test set

    Write test cases that reflect the inputs the application will actually receive, and attach assertions that express what a good output looks like, such as required fields, forbidden content or a rubric for tone and accuracy. Involve the people who own the task in choosing the cases, because the optimizer can only improve against what the tests measure. This step is done when the test set covers the main input types and every case has at least one assertion.

  4. Confirm the baseline eval passes

    Run a standard eval against the unmodified prompt before running optimize. Record the pass rate and review the failing cases, since a baseline that already fails for reasons unrelated to the prompt, such as a broken provider connection or a faulty assertion, will make any optimization result hard to interpret. This step is done when you have a recorded baseline score and understand why each failure occurs.

  5. Split tuning and validation data

    Divide the test cases into a tuning set, which the optimizer uses to generate and compare candidates, and a validation set, which it does not use for selection. Keep the split fixed across runs so that scores remain comparable. Confirm the split against the prompt documentation before you start, because the exact configuration keys for splitting are the place where setups most often diverge. This step is done when both sets are saved as separate, versioned inputs.

  6. Run promptfoo optimize

    Run the optimize command against the configuration, pointing it at the tuning data and the target metric you chose in advance. The run generates candidate prompts and scores them, and its duration and provider usage depend on the number of candidates, test cases and the model used, so confirm those settings and expected provider cost before starting. This step is done when the run completes and writes its results without provider errors.

  7. Inspect validation scores separately

    Review the results and confirm that validation scores are reported separately from tuning scores. A candidate that scores well on tuning data but drops on validation data is overfitting the tests, and should not be adopted on the strength of its tuning result. This step is done when you can name the candidate, its tuning score and its validation score, and compare each against the baseline from the earlier eval.

  8. Record the decision before adopting a prompt

    Before replacing the production prompt, rerun the chosen candidate through the standard eval and review its failing cases alongside the baseline. Write down the decision, the scores and any cases that regressed, so that the team can reproduce the result later. This step is done when the candidate passes the same gates as the baseline and the decision record is stored with the configuration.

Why validation splits and the target metric decide the result

A prompt that is tuned and scored on the same cases will look better than it really is. The optimizer searches for wording that performs well on the cases it can see, so a high score on those cases says less about the prompt than it appears to. Hold out a validation set of cases that the optimizer never uses during its search, and judge the final prompt only on that set. Keep the held-out cases representative of production traffic, including the awkward inputs, because a validation set made of easy examples will reward a prompt that fails on hard ones. Before you start, confirm in the promptfoo prompt optimization documentation how the optimizer's split between search and validation data is configured and reported, so you know which numbers are safe to act on.

The target metric decides what the optimizer is actually improving, so it must match the quality your users and reviewers care about. An assertion pass rate is objective and repeatable: each case either meets a deterministic check or it does not. It works well when the requirement can be written as a rule, such as valid JSON with required fields. A model-graded score can capture qualities that rules miss, such as tone, completeness or whether an answer is grounded in the supplied context, but the grader is itself a model with its own errors and biases. Choose the metric first, then confirm which assertions and grading options the optimizer reads, and check that a higher score corresponds to an output a reviewer would accept.

The risk is optimizing for a metric that rewards the wrong behavior. If the rubric favors long, confident answers, the optimizer may produce verbose prompts that pass the grader while hedging or inventing detail. If a rule only checks for a keyword, the prompt may learn to echo the keyword. Before adopting a result, read the outputs for the top candidates on the validation set, not only their scores, and look for regressions on the cases that matter most to the business. Plan for that review as part of the work, and record which metric you chose and why, so a later reader can tell whether a score gain reflects better behavior or a better fit to the grader.

How an optimization run moves from config to adoption decision

The run starts in a config file that names one prompt, one provider and a test set divided into tuning and validation splits. promptfoo optimize runs on the team's own machine and generates candidate prompts, but each model call still goes from the machine to the provider endpoint, which is the trust boundary to review. Candidates are scored on both splits and compared against the current prompt as the baseline. A person then checks whether gains on the validation split hold up before anything is adopted. Keep the current prompt if they do not.

Optimization runs locally, while model calls cross to the provider endpoint.

What to review before adopting a new prompt

An optimized prompt can score higher on its validation split and still be worse for your product. These checks look at the score, the failures, the edges of the test set, the cost, the output quality and the next model version, so the decision to adopt rests on more than one number.

Validation score against baseline

Compare the optimized prompt's score on the validation split with the original prompt's score on the same cases, using the same provider and assertions. Confirm the gain is large enough to matter for your product and that it is not produced by a handful of easy cases. Check that the metric you chose is the one your team actually cares about, since promptfoo optimize will push hardest on whatever it measures.

Prompt optimization docs

Failures on safety and format checks

A higher average can hide new failures. Look at every case that fails an assertion for safety, refusal behavior or output format, such as is-json or schema checks, and compare it with the baseline run. A prompt that gains on answer quality but starts breaking a JSON contract will break the consumer downstream.

Regressions outside the optimized set

Run the candidate prompt against test cases it never saw during optimization, including edge cases, unusual inputs and cases your validation split does not cover well. An optimizer tunes toward its training examples, so any drop on cases outside that set is a warning that the prompt has overfit to the data it was given.

Cost and latency change

Measure token usage, response time and per-call cost for the optimized prompt against the baseline on the same provider. A longer prompt or a verbose answer style can raise cost on every request in production. Ask your team to confirm the change is acceptable at your expected volume before the new prompt ships.

Output samples read by a domain owner

Have someone who owns the business domain read a sample of outputs from both prompts, without knowing which is which. Automated assertions miss tone, misleading but well-formatted answers and missing caveats. The domain owner's judgment is the check that confirms the score reflects what users need.

Behavior on the next model version

An optimized prompt was tuned against one provider and one model version. Before relying on it, rerun the suite when the provider releases a new model, and confirm whether the prompt still meets the same checks. Plan this as a recurring step, since a prompt that passes today may need re-optimization after a model change.

Questions teams ask before optimizing prompts

What drives the effort and cost of a prompt optimization program?

The main drivers are the number of provider calls, the quality of the test set and the time reviewers spend checking results. Each candidate prompt is run against your validation cases, so more candidates and more cases mean more model calls, and the bill for those calls is paid to your model provider, not to promptfoo, which is open source under the MIT license. Test set quality drives effort more than anything else: a small set of cases that reflect real user inputs and known failure modes is cheaper to build and more useful than a large set of generic examples. Review time is the cost most teams underestimate, because someone with domain knowledge has to decide whether a higher score means a better answer. Ask any partner for a quote that separates provider usage, test case creation, review hours and any hosting work, and state which of these recur on each release.

How long does a first prompt optimization run take?

A first run takes as long as it takes to define the target metric, write the test cases and agree on what a better prompt means, and that work usually costs more time than the optimizer run itself. The run length depends on the number of candidates, the number of cases in the validation split, the response length of your provider and any rate limits on your account. Start with a narrow prompt, one provider and a test set you can review by hand in one sitting. Confirm with your provider how fast your account can serve requests before you size the run, and check the current behavior and options in the promptfoo prompt optimization documentation before you plan the schedule.

Can an in-house team run prompt optimization without outside help?

Yes, a team with a developer who knows the application, access to a model provider and someone who can judge output quality can run promptfoo optimize on its own. The tool improves one configured prompt against one configured provider, so the setup is contained. The harder parts are judgment tasks: choosing a metric that reflects the business outcome, keeping a validation split separate from the data used to guide the search, and deciding whether a candidate is safe to adopt. Teams that lack these skills often adopt a prompt that scores well on a narrow metric and fails on edge cases. If you have no one who can review results against the domain, a partner can supply that review for a fixed scope.

What does the work need from our team?

It needs three things from your side: representative test cases, a domain reviewer and a provider account with enough access for the run. Test cases should come from real or realistic user inputs, with expected behavior written down, because the optimizer can only improve what the tests measure. The domain reviewer signs off on the target metric and on the adopted prompt, and should be someone who would be accountable for a wrong answer. The provider account needs API access to the model under test, and you should confirm usage limits and data handling terms with that provider before sending any sensitive data through it.

Is promptfoo free to use, and what does the Community edition include?

promptfoo is licensed under the MIT license, and the promptfoo Community edition is free. According to the promptfoo pricing page, Community includes all LLM evaluation features, all model providers and integrations, and red teaming with 10k probes per month, with community support. The software also runs locally, so evals run on your machine and talk directly with the LLM, as the promptfoo introduction documentation describes. Your costs are the model provider calls you make. Enterprise features such as team sharing, SSO and continuous monitoring are sold separately at custom pricing, so confirm with promptfoo which edition covers your needs before you depend on a shared server.

When should we bring in a partner for prompt optimization?

Bring in a partner when the optimization affects a customer-facing or regulated workflow and your team lacks the time or domain expertise to validate the results. Partners are most useful for scoping the target metric, building a defensible validation split, reviewing candidates with a domain specialist and documenting the decision to adopt a prompt. Ask for a written scope that lists the provider and prompt covered, the test cases to be built, the review steps, the deliverables and the retest conditions before work begins. DeployOpen plans and runs fixed-scope engagements of this kind inside your own infrastructure, and it provides ongoing support as a retainer when prompts change regularly.

How DeployOpen works on this

DeployOpen treats prompt optimization as a bounded piece of evaluation work: one prompt, one provider, one agreed target metric. We scope the search, build the validation split and the eval suite around it, and hand over the results with the reasoning needed to decide whether a new prompt should be adopted.

How we scope

  • Written scope names the single prompt, the single provider and the target metric to optimize.
  • Scope defines which test cases feed the optimizer and which are held back for validation.
  • Scope states what counts as an accepted result and who signs off before any prompt change.

How we build

  • We configure the optimize run against your prompt and provider, with the candidate search limits agreed in advance.
  • We build separate training and validation splits so improvements are checked on examples the search never saw.
  • We test the grading assertions on known good and known bad outputs before trusting the metric.

How we hand over

  • You receive the eval configuration, the splits, and the run results with each candidate prompt compared on the target metric.
  • Documentation explains the chosen prompt, the rejected candidates, and the known limits of the validation data.
  • Your team reruns the suite on its own machine or CI runner, so the result does not depend on our environment.

How we support

  • Engagements are fixed-scope, with scope, deliverables and timelines agreed in writing before work starts.
  • Ongoing support, re-optimization after model or prompt changes, and continuous evaluation are available as a retainer.
  • Quotes follow from the written scope, so confirm which provider versions and test data sizes the quote assumes.

Sources and further reading

Talk to our team.

Tell us what you're working on, whether it's a deployment, an audit, a security test or a cyber range. You'll speak with an engineer who can help you scope it.

  • 30-minute call: free, with no obligation.
  • NDA on request: we can sign before you share details.
  • Clear next steps: a scope and plan after the call.