Lesson 29: Evals and the Cassette
Score a suite of agent runs with runEvals, record the vendor calls once through a cassette, and replay the suite offline with the same verdicts.
1. Why This Feature Exists
A prompt, a tool or a model change can quietly break a case that used to pass. An eval suite turns "it seems to work" into a pass rate: every case runs on a fresh session, the run is observed (the final output, the tool calls, the usage, the latency, a thrown error) and every scorer gives a verdict with its evidence. A cassette makes that suite cheap and deterministic: record the vendor calls once, then replay them offline in CI, with no key and no network.
2. Key Symbols and Exports
{ results, passRate }; a case passes when every scorer passes, and a run that throws fails its case with a failureClass.outputMatches(), outputSchema(), toolsCalled(), toolSequence(), withinBudget() read their expectation from case.expected (absent: skipped). judge({ judge, threshold }) wraps a createLlmJudge or createScoredJudge judge.record runs the real call and stores its normalized stream events; replay answers from the recording and never calls the vendor. A request with no recording fails, non-retryable.3. Implementation Walkthrough
import { createCassette, EVAL_SCORERS, runEvals } from 'merci-sdk';
const cases = [
{ name: 'weather', input: 'weather Prague', expected: { output: /sunny/, tools: ['forecast'], maxTotalTokens: 2000 } },
{ name: 'json', input: 'Answer {"ok": true}', expected: { schema: { type: 'object', required: ['ok'] } } },
];
const scorers = [
EVAL_SCORERS.outputMatches(), EVAL_SCORERS.outputSchema(),
EVAL_SCORERS.toolsCalled(), EVAL_SCORERS.withinBudget(),
];
// Record once against the real provider (CI sets CASSETTE_MODE=replay).
const cassette = createCassette({ path: 'evals/suite.cassette.json' });
const mode = process.env.CASSETTE_MODE === 'replay' ? 'replay' : 'record';
const { results, passRate } = await runEvals({
cases, scorers,
makeSession: () => client.session(model).withTools({ tools: [forecast] }).withCassette({ cassette, mode }),
});
for (const r of results) {
if (!r.passed) console.log(r.name, r.observation.failureClass, r.verdicts.filter((v) => !v.passed));
}
console.log(`pass rate ${passRate}`);4. Wire and Telemetry Mechanics
The runner reads the run from the session's own emitter: the tool calls from tool:call_end, the usage from run:end. The cassette sits between the session and its provider adapter, so it stores the unified stream events (text, tool calls, quota) every provider produces, and a replay drives the same agentic loop: the tools still run, the scorers see the same output. The key covers the request data only, so a replay matches across processes; a request that embeds the date (withAutoContext) changes its key day to day, so record those per run or leave them off.
The builder also takes the coordination capabilities. withWorkspace({ workspace }) is a builder method: it binds the context, shell and artifact tools to one handle, applies the session's withApproval() policy to the shell gate, and fails closed without a callback or permissionMode: 'bypassPermissions'. createTodoWorkSource({ store }) is a scheduler factory over that same Map, so the todo list a session keeps becomes the board a scheduler dispatches. Both are shown in examples/28_workspace_isolation.mjs.
5. Run the Example
Execute the runnable example directly from the repository root:
node examples/23_evals.mjsRead the full example: examples/23_evals.mjs →