NashTech Blog

Promptfoo, automation tool to test and optimize LLM prompts effectively

Table of Contents

Most LLM features ship after someone tweaks a prompt and eyeballs a few answers. That’s how regressions slip in. A change that fixes one case can quietly break another, and a model upgrade can change behavior overnight. Promptfoo applies automated testing to prompts.

This post covers what Promptfoo is, how it works, how to install and run it, and how to read the results.

1. What is Promptfoo?

Promptfoo is an open-source CLI and library for evaluating and red-teaming LLM applications. You describe what to test in a YAML file. Promptfoo runs every combination and shows the outputs side by side.

  • Declarative tests: prompts, models, and test cases live in one promptfooconfig.yaml, with no notebooks needed.
  • Many providers: it supports 60+, including OpenAI, Anthropic, Google, local models (Ollama), custom Python/JavaScript code, and plain HTTP APIs.
  • Automatic scoring: assertions check outputs for you, from simple string matches to LLM-graded rubrics.
  • Fast iteration: caching, concurrency, and live reload are built in.
  • Local and CI-friendly: evals run on your machine and call the providers you configure. You can also run them in CI/CD.
  • Red teaming: it can scan LLM apps for security vulnerabilities.

2. Promptfoo project sample

There isn’t much to it. A project is a folder with a config file in it. This is the whole thing for the example in this post:

my-first-eval/
├── promptfooconfig.yaml # the eval itself: prompts, providers, tests
├── .env # API keys (keep out of git)
└── README.md # created by `promptfoo init`, optional

2.1 How it works

An eval combines three inputs:

  1. Prompts: templates with {{variables}}.
  2. Providers: the models or APIs you want to test.
  3. Tests: variable values, plus optional assertions that define what a good answer looks like.
description: "My Eval"

prompts:
  - "Write a tweet about {{topic}}"

providers: #LLM or API
  - "openai:gpt-4o"
  - "openai:gpt-4o-mini"

tests:
  - vars:
      topic: artificial intelligence

  - vars:
      topic: climate change
    assert:
      - type: javascript
        value: "true"

      - type: contains
        value: climate change

The config file is where everything happens, and it has four parts:

promptfooconfig.yaml
├── description – a label for the eval
├── prompts – what you send to the model
├── providers – who answers: an LLM or an API
└── tests – what to try, and how to judge it
         ├── vars – values for the {{placeholders}} in your prompts
         └── assert – checks run on each output (optional)

Promptfoo fills each prompt with each test’s variables and sends it to each provider. It grades every output with your assertions and stores the results. When you find a new failure in production, you add it as a test. That feedback loop is what stops quality from regressing.

2.2 Installation

Promptfoo needs Node.js 22.22.0 or newer (24 LTS is recommended).

node --version                # check Node.js

npm install -g promptfoo      # global install
# or: brew install promptfoo  (macOS / Linux)
# or: npx promptfoo@latest <command>   (no install)

promptfoo --version           # verify

2.3 Create your first eval

mkdir my-first-eval
cd my-first-eval
promptfoo init

promptfoo init is an interactive walkthrough. To start from a ready-made example instead, run promptfoo init --example getting-started.

Put your provider key for LLM or API in a .env file in the same folder. Promptfoo loads it automatically. Never commit this file.

OPENAI_API_KEY=your-key-here

or

PROJECT_API_KEY=your-key-here

Then edit promptfooconfig.yaml:

description: "Support bot: prompt comparison"

prompts:
  - "Answer the customer question in one short paragraph: {{question}}"
  - "You are a friendly support agent. Answer in at most 3 sentences and offer further help: {{question}}"

providers:
  - openai:gpt-5-mini   # use any model you have access to

tests:
  - vars:
      question: "How do I reset my password?"
    assert:
      - type: icontains
        value: "password"
      - type: javascript
        value: "output.length < 400"

  - vars:
      question: "Can I get a refund after 30 days?"
    assert:
      - type: icontains-any
        value: ["refund", "policy"]
      - type: not-icontains
        value: "as an AI"

Two prompts × one provider × two tests gives four outputs to compare.

Common assertion types are:

TypeValuePasses when
contains, icontainstextthe output includes it (icontains ignores case)
equalstextthe output matches exactly
regexa patternthe output matches the pattern
is-jsonnone (optional schema)the output is valid JSON
javascriptan expression, like output.length < 280the expression is true
similartext, plus a threshold (for example 0.8)the meaning is close enough, measured with embeddings
llm-rubriccriteria in plain Englisha grader model says the output meets them
latencythreshold in millisecondsthe response is faster than that
costthresholdthe cost stays below it, if the provider reports cost

Put not- in front of any type to flip it. Three more fields work on any assertion:

  • threshold is the score a type like similar needs.
  • weight is how much the assertion counts (default 1).
  • metric is a name that groups scores in the viewer.

2.4 Run the eval

promptfoo eval

Promptfoo runs every prompt, provider, and test combination and grades the outputs. It prints a table in the terminal and saves the run locally. Responses are cached by default, so re-runs after small edits are fast and cheap.

CommandWhat it does
promptfoo eval -c path/to/config.yamlUse a specific config file
promptfoo eval -o results.jsonExport results (csv, json, html, yaml, junit.xml, …)
promptfoo eval --no-cacheBypass the cache and call the model again
promptfoo eval --watchRe-run automatically when the config changes
promptfoo eval --filter-first-n 5Run only the first 5 tests (quick smoke check)
promptfoo eval --retry-errorsRe-run only results that errored
promptfoo validateCheck the config for schema errors

2.5 Read the results

In the terminal

The table has one row per test case and one column per prompt/provider pair. Each cell is marked pass or fail. A summary of passes, failures, errors, and pass rate follows.

In the web viewer
promptfoo view

This starts a local server and opens the results in your browser. Add -y to skip the confirmation prompt. Look at these parts first:

  • The matrix: rows are test cases and columns are prompt/provider combinations, so you can compare variants at a glance.
  • Display mode: filter to Failures first. The other modes are Passes, Errors, Different, and Highlights. You can also search by text or regex.
  • Cell details: hover over a cell and open Details to see the full output, the rendered prompt, the variables, and the grading results. The grading results show why an assertion failed.
  • Charts: toggle Show Charts for pass rate, score distribution, and a scatter plot that compares two prompts head to head.
  • Manual review: mark outputs with thumbs up/down, set a custom score, or leave comments. These are saved and included in exports.
  • Compare and export: diff against another eval, or download CSV, JSON, or failed tests only.

2.6 How pass/fail is decided

  • Each assertion returns a pass/fail result and a score (usually 0 to 1).
  • A test passes only when all its assertions pass. If you set a test-level threshold, the weighted score must reach it instead. Use weight to make some assertions count more.
  • The pass rate is the percentage of tests where all assertions passed.
  • Tag assertions with metric: Tone (or any name) to get named scores in the UI.

2.7 Fail vs. error

  • Fail: the output didn’t meet an assertion. Fix the prompt, or the assertion if it is too strict.
  • Error: something broke before grading, such as a bad key, rate limit, network issue, or timeout. Fix the cause, then run promptfoo eval --retry-errors.

2.8 A simple routine

  1. Check the pass rate for each column.
  2. Filter to failures.
  3. Open details to see which assertion failed and why.
  4. Decide whether it’s a prompt problem, an over-strict assertion, or a new edge case to add as a test.
  5. Re-run, then use Compare to confirm nothing regressed.

3. Quick recap

npm install -g promptfoo
promptfoo init
promptfoo eval
promptfoo view

Promptfoo won’t make your prompts better on its own, but it makes changes a lot safer to try. One config file, a few test cases, and the two commands you’ll run all the time, promptfoo eval and promptfoo view, are enough to replace “it looked fine on the two examples I tried” with a check you can repeat after every edit. Start small. Pick the handful of inputs that matter most, add a simple assertion or two, and read the failures first. Whenever a prompt misbehaves in real use, turn that input into a new test. The next time you change the prompt, you’ll find out what else broke before your users do.

Picture of hoang.buixuanminh@nashtechglobal.com

hoang.buixuanminh@nashtechglobal.com

Suggested Article

Scroll to Top