Interactive engineering lab

Derived metric formulas: compile once, reject early, evaluate per row

Edit a metric formula, see whether the small grammar accepts it, then step through each synthetic row's running totals, value and debug log.

By Sandeep Belgavi · Published 2026-11-22

Scenario: an evaluation of a help-desk classifier computes a derived F1 metric from true positives, false positives and false negatives. Synthetic inputs; every calculation stays in this page and nothing is sent anywhere.

Experiment workspace

Decision trace

    What this lab models

    An original model of the grammar promptfoo 0.126.0 documents for string derived-metric formulas: numeric literals, score names and __count, parentheses, + - * /, unary signs, the six comparisons without chains, condition ? a : b, and at most 1,024 characters. The formula is compiled once before any row is scored, as 0.126.0 does. Then each synthetic row adds its true positives, false positives and false negatives to running totals and the formula is evaluated over them. A missing score is recorded as a debug line and the value stays where it was, which is how 0.126.0 still treats scoring errors.

    How to experiment

    Start with the guarded F1 preset over the four mixed rows: it compiles and ends at 0.75. Choose the zero row: F1 is 0, while the unguarded precision preset gives NaN. Choose the max, chain, property and assignment presets: each is rejected before any row. Choose the misspelled preset: it compiles, every row logs a debug line, and the value stays 0. Turn the known-names check on and the same formula is rejected before scoring. Edit the formula text to try your own.

    Expected outcomes to check

    Guarded F1: 0.75 over the mixed rows, 0 over the zero row. Unguarded precision over zeros: NaN. Rejected: max(tp, fp) as a function call, 0 < tp < 1 as a comparison chain, tp.constructor as property access, tp = 1 as an assignment, tp ** 2 as an operator, and any formula over 1,024 characters. Misspelled fnn: 0 with four debug lines, or rejected with the name check on. The range-check preset gives 1. These match the companion Python example.

    Trace, reset and export

    The trace shows the setup, the compile decision, one line per row with the running totals and the value or debug line, and the outcome. Start trace, Next step, Show all and the slider control how much is visible. Reset inputs restores the defaults. Export JSON saves the formula, the compile result, the per-row table and the trace.

    Limits

    Not promptfoo code. promptfoo parses with the acorn JavaScript parser and accepts every JavaScript numeric literal form; this tokenizer accepts decimal and exponent forms only. Scores here are three synthetic counts; in promptfoo, named scores come from your assertions. The known-names check is a suggested addition, not promptfoo behaviour.

    Original lab by Sandeep Belgavi. Views are my own and do not represent any employer, past or present.