Core
⏱ 8 min read
📁 examples/04_judgment.mjs
Lesson 30: Judgment: typed answers, then a gate in code
discriminative models: noul/choice/score, the typed read, role clients, one decision gate
1. Why This Feature Exists
A support inbox is triaged by a discriminative ("System One") model, not by prose. The model reads one ticket and answers a map of typed questions — yes/no, pick-one, score — and then plain code decides what happens:
2. What the Example Demonstrates
A — the request
ONE live
decisions.ask on the selected provider answers three primitives at once: is this a refund request (noul), which team owns it (choice), how urgent is it (score).B — the typed read:
readDecisionAnswer turns each answer intothe typed read:
readDecisionAnswer turns each answer into its type, and createDecisionGate acts only above a certainty. Below the threshold (or on any non-answer) the gate escalates to the LLM path instead of guessing; the policy itself is two numbers and a fallback.C — the runtime
createRoleClients resolves the generative, embedding and decision surfaces independently, createAgenticRuntime hands ONE gate to every seam, withDecisionGate installs it on a session, and a createDecisionReranker built from the decision client orders memory hits by relevance rather than by cosine.D — calibration
decisionCalibration turns a labelled sample into the evidence for (or against) the threshold. Thresholds are application policy: probe them on your own labelled sample, then keep them next to the code that acts on them. Public API: initializeClient, loadCredentials, getDecisionModel, createDecisionsClient, noulQuestion, choiceQuestion, scoreQuestion, readDecisionAnswer, createDecisionGate, createRoleClients, resolveRole, createAgenticRuntime, createDecisionReranker, decisionCalibration, EVENTS, log. Run: node examples/04_judgment.mjs Needs: PROVIDER plus its key and DEFAULT_<PROVIDER>_DECISION_MODEL in secret.env (decision wires: openai, openrouter, opencode, google_genai). @version 0.27.0imported from 'merci-sdk'
EVENTS, choiceQuestion, createAgenticRuntime, createDecisionGate, createDecisionReranker, createDecisionsClient, createRoleClients, decisionCalibration, getDecisionModel, initializeClient, loadCredentials, log, noulQuestion, readDecisionAnswer, resolveRole, scoreQuestion
3. How the Core Call Works
The entry point, condensed. The full runnable file is linked below.
import {
EVENTS, choiceQuestion, createAgenticRuntime, createDecisionGate, createDecisionReranker,
createDecisionsClient, createRoleClients, decisionCalibration, getDecisionModel,
initializeClient, loadCredentials, log, noulQuestion, readDecisionAnswer, resolveRole, scoreQuestion,
} from 'merci-sdk';
import { reportUsage, resolveExample, runMain, trackUsage, wireClient } from './_shared/example-utils.mjs';
const TICKET = 'My payments have failed for three days and I am losing sales. I want my money back. Please help now.';
const TEAMS = { billing: 'Charges, invoices, payment failures.', account: 'Login, profile, permissions.', other: 'Anything else.' };
const URGENCY = ['Low', 'Normal', 'High', 'Critical'];
/** The threshold is application policy, not a vendor fact. */
const ACT_ABOVE = 0.8;
/** Memory hits for the reranker: the relevant fact is placed SECOND on purpose. */
const MEMORY_QUERY = 'why are my card payments failing?';
const MEMORY_FACTS = [
{ content: 'The support inbox is staffed from 09:00 to 17:00 CET.' },
{ content: 'The customer card was declined three times by the issuer.' },
];
/**
* The three primitives in one request: a yes/no, a closed-world choice and a
* score on an ordered rubric.
* @returns {Record<string, any>} the questions, keyed by answer name
*/
function triageQuestions() {
return {
refund: noulQuestion({ instructions: 'Is the customer asking for a refund?' }),
team: choiceQuestion({ instructions: 'Which team should handle this ticket?', criteria: TEAMS }),
urgency: scoreQuestion({ instructions: 'How urgent is this ticket?', criteria: URGENCY }),
};
}
/**
* The whole policy: two thresholds and a fallback, no model involved.
* @param {{refund: boolean|null, escalated: boolean, urgency: number, team: string}} arg0
* @returns {string} the action to take
*/
function route({ refund, escalated, urgency, team }) {
if (escalated) return 'escalate_to_llm';
if (refund) return 'refund_queue';4. Behavior the Code Fixes
discriminative models: noul/choice/score, the typed read, role clients, one decision gate
5. Run the Example
Execute the runnable example from the repository root:
node examples/04_judgment.mjs
What it does: discriminative models:
noul/choice/score, the typed read, role clients, one decision gate