← Back to writing

Build · Scope: global engineering practice · AI builds 2/3

A small test harness for prompts: same cases, every change

About forty lines of code that run the same cases against several variants and count passes, failures and missing results separately. Tried on our semantic search and on a small local model, where it caught a problem nobody was looking for.

This article was prepared with AI assistance, from the sources listed at the end and the working code in this repository. All outputs shown are from real runs.

Why a harness

The previous build ended with a question it could not answer by looking: does a change make search better or worse? The same question comes up every time you edit a prompt, switch a model or add content. Reading a few outputs by eye does not settle it.

Anthropic’s guide to evaluations describes the routine: define success criteria that are specific and measurable, write test cases including edge cases, test the prompt against them, refine, and compare each version with the previous one. The DeepLearning.AI prompt course teaches the same loop as iterative prompt development. A harness is simply the code that makes that loop cheap enough to run every time.

The whole harness

Each variant is a function that takes a case and returns an output. The harness runs every case through every variant and records a verdict. It keeps three counts apart: passed, failed, and unresolved when the variant threw or returned nothing. That follows the earlier note on evaluation: a missing result is not a failure and not a success.

export async function compare(variants, cases, check) {
  const results = {};
  for (const [name, run] of Object.entries(variants)) {
    const rows = [];
    for (const c of cases) {
      let output;
      try {
        output = await run(c);
      } catch {
        output = null;
      }
      const verdict =
        output == null ? 'unresolved' : check(c, output) ? 'pass' : 'fail';
      rows.push({id: c.id, verdict, output});
    }
    results[name] = rows;
  }
  return results;
}

Target one: our search

Twelve questions, in Turkish and English, each with the section it should find. Two variants: chunks that start with the article title and section heading, and chunks with the paragraphs only.

Both variants score the same: 11 of 12 in the top three, 10 of 12 in first place. But they do not miss the same questions: each finds one that the other ranks wrongly. A single total would have hidden that.

The one question both miss in the top three is the interesting one. The answer it should find has been overtaken by the previous build article, which discusses exactly that query. Adding content changed the results, which is the strongest argument for rerunning the cases after every content change, not only after code changes.

It happened again while this article was being written. Publishing it added 14 chunks to the index, and on the rerun the title-and-heading variant dropped from 10 to 9 first-place answers: question q02 slipped out of first place. Nothing in the search code had changed.

$ npm run retrieval
Retrieval: 12 questions, 106 chunks
-- expected section in top 3
title+heading+text     pass 11/12  fail 1  unresolved 0  (rate over assessed: 92%)
text only              pass 11/12  fail 1  unresolved 0  (rate over assessed: 92%)
-- expected section ranked first
title+heading+text     pass 10/12  fail 2  unresolved 0  (rate over assessed: 83%)
text only              pass 10/12  fail 2  unresolved 0  (rate over assessed: 83%)
  differs: q09 -> title+heading+text=pass, text only=fail
  differs: q11 -> title+heading+text=fail, text only=pass
missed in top 3 (title+heading+text): q01 modelin aynı anda ne kadar metin görebildiği -> got semantic-search-over-our-articles#section-3
missed in top 3 (text only): q01 modelin aynı anda ne kadar metin görebildiği -> got semantic-search-over-our-articles#section-7

# rerun after this article was added to the site
Retrieval: 12 questions, 120 chunks
-- expected section ranked first
title+heading+text     pass 9/12  fail 3  unresolved 0  (rate over assessed: 75%)
text only              pass 10/12  fail 2  unresolved 0  (rate over assessed: 83%)
  differs: q02 -> title+heading+text=fail, text only=pass

Target two: a prompt on a small local model

The task: extract the date and amount from short payment messages, in English or Turkish, as JSON. Eight messages, two prompts. The first is a single sentence. The second states the output format, the Turkish number convention and one example.

The model is Qwen2.5-0.5B-Instruct, the smallest instruction-tuned model of the Qwen2.5 family, run locally through Transformers.js with greedy decoding so that every run gives the same output. The check is deliberately specific: there must be a JSON object, and both values must match exactly.

const prompts = {
  'v1 one line': 'Extract the date and the amount from the message.',
  'v2 structured': [
    'You extract payment data from short messages in English or Turkish.',
    'Return only a JSON object, nothing else: {"date": "YYYY-MM-DD", "amount": number}.',
    'Rules: the amount is a plain number without currency; in Turkish messages "." separates thousands and "," separates decimals.',
    'Example: "3 Mart 2026 tarihinde 1.250,75 TL alındı." -> {"date": "2026-03-03", "amount": 1250.75}',
  ].join('\n'),
};

// Pass only if the output contains a JSON object with exactly the expected values.
function check(c, output) {
  const match = output.match(/\{[\s\S]*?\}/);
  if (!match) return false;
  try {
    const value = JSON.parse(match[0]);
    return value.date === c.date && Number(value.amount) === c.amount;
  } catch {
    return false;
  }
}

What the prompts scored

The structured prompt passed all eight messages, including the Turkish thousands separator in “1.750 TL” and the decimal comma in “64,50 TL”. The one-line prompt passed none. Most of its answers were correct sentences in the wrong form, because nobody asked for JSON; a few were also wrong in content, such as a date without a year or a missing amount.

The check extracts the first JSON object from the output. That is a choice: three passing answers have something extra after the object, two a stray bracket and one a full stop. A strict check that requires the whole output to be JSON passes 5 of 8 instead of 8; the lab lets you switch between the two. Decide how strict to be on purpose, and write it into the check.

$ npm run prompts
Prompts: 8 messages, model onnx-community/Qwen2.5-0.5B-Instruct (q4, greedy decoding)
v1 one line            pass 0/8  fail 8  unresolved 0  (rate over assessed: 0%)
v2 structured          pass 8/8  fail 0  unresolved 0  (rate over assessed: 100%)

v1 one line:
  m1 fail The date is 3 March 2026 and the amount is 250 EUR.
  m4 fail The date is "30 Nisan" and the amount is "1.750 TL".
  m8 fail The date extracted from the message is 21.08.2026.
v2 structured:
  m1 pass {"date": "2026-03-03", "amount": 250.00}
  m4 pass {"date": "2026-04-30", "amount": 1750.0}}
  m8 pass {"date": "2026-08-21", "amount": 64.5}}

What the harness caught that we were not looking for

The first run used the 8-bit version of the model. Both prompts scored zero, and the outputs were unrelated text and repeated words. Without a baseline, the obvious conclusion would have been “this model cannot do the task” or “the prompt is bad”.

A one-message probe with other precision levels showed the 4-bit and 16-bit files answering correctly while the 8-bit file did not. Transformers.js lets you choose the precision (dtype) of the model weights; in this setup one choice was simply broken. Same cases, same prompts: the harness turned a confusing failure into a one-line fix.

m1m2m3m4m5m6m7m8v1 · 4-bitv2 · 4-bitv2 · 8-bitfilled: pass · grey: fail
Same eight messages, three runs. The prompt made the difference between the first two rows; the model file made the difference between the last two.
$ DTYPE=q8 npm run prompts
v1 one line            pass 0/8  fail 8  unresolved 0  (rate over assessed: 0%)
v2 structured          pass 0/8  fail 8  unresolved 0  (rate over assessed: 0%)
  m2 fail On "System" on "System" on "System" on "System" on "System" on "Sy
  m5 fail In: Telegram Telegram Return Telegram Telegram Telegram Telegram Teleg

Limits, and what comes next

Twelve questions and eight messages are enough to show the method, not to rank models. The results describe this small model in this setup and should not be generalised. Grow the cases from real failures: every time something goes wrong in use, add it as a case.

The last build in this series turns to something we use every day: giving several AI tools one shared memory.

References

  1. Anthropic documentation: Define success criteria and build evaluations
  2. DeepLearning.AI: ChatGPT Prompt Engineering for Developers (Isa Fulford, Andrew Ng)
  3. Qwen Team: Qwen2.5 Technical Report (arXiv, December 2024)
  4. Transformers.js documentation: Using quantized models (dtypes)