Compare two prompt versions with evals
Decide between two versions of a stored prompt with data: build an eval suite for each from the same cases, run both on one tier, and compare.
You revised a prompt. Is the new version better? This recipe answers with a measurement instead of a hunch. It reads two versions of a stored prompt, builds one eval suite for each from the same test cases, runs both on the same tier, and compares the pass counts. It chains the prompt calls with the eval calls.
How the pieces fit
An eval run sends each case's input to the tier verbatim. It does not read
a stored prompt: a suite's prompt_id is a label that records which prompt
the suite tests. So the recipe renders each version itself. For every test
case, it fills the version's template with the case's text and stores the
result as the case input. The two suites then differ only in the prompt
text, which is the one thing being compared.
The call that starts each run:
curl https://api.cruxy.ai/api/v1/evals/suites/$SUITE_ID/runs \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "model": "saga" }'The full recipe
const BASE = 'https://api.cruxy.ai/api/v1'
const promptId = process.env.PROMPT_ID // a stored prompt with at least two versions
const headers = {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
}
async function call(method: string, path: string, body?: unknown) {
const res = await fetch(`${BASE}${path}`, {
method,
headers,
body: body === undefined ? undefined : JSON.stringify(body),
})
if (!res.ok) throw new Error(`${method} ${path} failed: ${res.status} ${await res.text()}`)
return res.json()
}
// The same tickets for both versions. `expected` is the label a correct
// answer contains; the prompt must ask for a one-word label for this to work.
const tickets = [
{ ticket: 'I was charged twice for order 8841.', expected: 'billing' },
{ ticket: 'My parcel has not moved in six days.', expected: 'shipping' },
{ ticket: 'I cannot reset my password.', expected: 'account' },
]
// 1. Build a suite for one version: its template, filled with each ticket.
async function suiteFor(version: number): Promise<string> {
const prompt = await call('GET', `/prompts/${promptId}?version=${version}`)
const template: string = prompt.version.content // for example: "...Ticket: {{ticket}}"
const suite = await call('POST', '/evals/suites', {
name: `${prompt.name} v${version}`,
slug: `${prompt.slug}-v${version}-${Date.now()}`, // a slug is unique in the workspace
prompt_id: prompt.id,
})
for (const t of tickets) {
await call('POST', `/evals/suites/${suite.id}/cases`, {
input: template.replaceAll('{{ticket}}', t.ticket),
expected: t.expected,
match_mode: 'contains',
})
}
return suite.id
}
// 2. Start a run and wait for it.
async function runSuite(suiteId: string, model: string) {
let run = await call('POST', `/evals/suites/${suiteId}/runs`, { model })
while (run.status === 'pending' || run.status === 'running') {
await new Promise((resolve) => setTimeout(resolve, 2000))
run = await call('GET', `/evals/runs/${run.id}`)
}
if (run.status === 'failed') throw new Error(`run ${run.id} failed: ${run.error}`)
return run
}
// 3. Run both versions on the same tier, one after the other.
const MODEL = 'saga'
const versions = [1, 2]
const report = []
for (const version of versions) {
const suiteId = await suiteFor(version)
const run = await runSuite(suiteId, MODEL)
const results = await call('GET', `/evals/runs/${run.id}/results`)
const latencies = results.data.map((r: { latency_ms: number }) => r.latency_ms)
report.push({
version,
passed: run.passed,
total: run.total_cases,
meanLatencyMs: Math.round(
latencies.reduce((sum: number, ms: number) => sum + ms, 0) / Math.max(latencies.length, 1),
),
failures: results.data
.filter((r: { passed: boolean }) => !r.passed)
.map((r: { case_id: string; actual: string }) => ({ case: r.case_id, got: r.actual })),
})
}
// 4. Compare.
for (const r of report) {
console.log(`v${r.version}: ${r.passed}/${r.total} passed, mean ${r.meanLatencyMs} ms`)
for (const f of r.failures) console.log(` failed ${f.case}: ${f.got}`)
}Reading the result
- Pass counts compare the versions, on these cases only. Three cases show the method; they do not decide anything. Use enough cases from your own product to see the spread, and include the inputs the old version got wrong.
- Read the failures, not only the count.
actualis the tier's output for a failed case. A version can fail because it is worse, or because it answers correctly in words yourexpecteddoes not contain. match_modecompares text.containspasses when the output containsexpected, andexactwhen the trimmed output equals it. Both need a prompt that asks for a short, fixed answer. A prompt that asks for a paragraph cannot be scored this way.- Hold everything else still. Run both suites on the same tier. A run
takes a tier and nothing else, and
autois refused, so the tier is the only setting to keep equal. - Latency is per completion.
latency_msis how long each case's completion took. A mean over a few cases is a rough guide, not a measurement of the tier.
Clean up
Each run of the script creates two suites. DELETE /api/v1/evals/suites/{id}
deletes a suite with its cases, runs, and results. Keep the suites if you
want the record: a suite's prompt_id shows which prompt it tested.
Next steps
- Prompting basics: what to change between versions.
- Tier and effort for cost and latency: run the same comparison across tiers.
- Usage and cost: what each completion in a run costs.