Turn any set of AI models into a real roundtable. A TypeScript implementation of the Consensus Validation Protocol (CVP), with no LLM-provider coupling and a typed event stream, published on npm.
This is the engine that powers Roundtable and the ai-consensus-mcp MCP server — extracted into a standalone library so anyone can wire multi-model debate into their own product.
Many multi-agent frameworks are tied to one provider or one use case. This library implements only the protocol, so it slots into whatever stack you already run.
You configure any number of models — Grok, Claude, Gemini, DeepSeek, whatever — give each a persona, and hand it a question. You get:
- Blind Round 1, then sequential debate. Each model defends its take under full cross-visibility.
- Confidence scoring + disagreement detection. Deterministic, no extra LLM calls.
- Early stopping when the group converges.
- Optional judge synthesis — a non-voting model produces majority/minority/unresolved sections.
- Attempt adjudication — blind jurors rank N candidate solutions to one task; Borda + Kendall's W decide, quotes are verified against the candidates.
- Observability. A typed event stream fires on every round, every participant, every confidence shift, every disagreement.
- Zero provider coupling. The library never imports a provider SDK. You plug in a
ModelCalleronce and use any backend.
npm install ai-consensus-core
# or
pnpm add ai-consensus-core
# or
yarn add ai-consensus-coreESM-only. Node ≥ 20. Runtime dependencies: zod + Node's built-in events. That's it.
import {
ConsensusEngine,
type ModelCaller,
type Persona,
} from "ai-consensus-core";
// 1) Bring your own personas. The library ships only the non-voting
// JUDGE_PERSONA — debate personas are caller-owned content.
// See docs/personas.md for a copy-paste block of the seven defaults.
const riskAnalyst: Persona = {
id: "pessimist",
name: "Risk Analyst",
description: "Surfaces failure modes and tail risks.",
systemPrompt: "You are a rigorous Risk Analyst. Surface hidden dangers, second-order effects, and plausible failure modes. Be precise and constructive.",
};
const firstPrinciples: Persona = {
id: "first-principles",
name: "First-Principles Engineer",
description: "Decomposes every claim to axioms.",
systemPrompt: "You are a First-Principles Engineer. Decompose every claim into fundamental axioms. Reject analogies and expose hidden premises.",
};
const domainExpert: Persona = {
id: "domain-expert",
name: "Domain Expert",
description: "Practical implementation knowledge and edge cases.",
systemPrompt: "You are a seasoned Domain Expert. Ground your analysis in real-world implementation details, anti-patterns, and edge cases.",
};
// 2) Adapt your provider of choice to the ModelCaller shape.
// This one targets any OpenAI-compatible endpoint (Grok, Claude, OpenAI, Groq…).
const caller: ModelCaller = async ({ system, user, modelId, temperature, maxOutputTokens, signal }) => {
const res = await fetch("https://api.x.ai/v1/chat/completions", {
method: "POST",
signal,
headers: {
authorization: `Bearer ${process.env.GROK_API_KEY}`,
"content-type": "application/json",
},
body: JSON.stringify({
model: modelId,
temperature,
max_tokens: maxOutputTokens,
messages: [
{ role: "system", content: system },
{ role: "user", content: user },
],
}),
});
const json = await res.json();
return { content: json.choices[0].message.content };
};
// 3) Wire up observability.
const engine = new ConsensusEngine(caller);
engine.on("roundStart", (e) => console.log(`▶ ${e.label}`));
engine.on("roundComplete", (e) => console.log(` score=${e.score}`));
engine.on("disagreementDetected", (e) =>
console.log(` ⚠ ${e.disagreement.label} (Δ=${e.disagreement.severity})`),
);
// 4) Run.
const result = await engine.run({
question: "Should early-stage startups adopt microservices from day one?",
participants: [
{ id: "p1", modelId: "grok-4", persona: riskAnalyst },
{ id: "p2", modelId: "grok-4", persona: firstPrinciples },
{ id: "p3", modelId: "grok-4", persona: domainExpert },
],
maxRounds: 4,
judge: { modelId: "grok-4" }, // uses the shipped JUDGE_PERSONA
});
console.log(`Final score: ${result.finalScore}`);
console.log(result.synthesis?.majorityPosition); USER QUESTION
│
▼
┌─────────────────────────────────────────────────────────┐
│ ROUND 1 — INITIAL ANALYSIS │
│ (blind=true, parallel, no cross-visibility) │
│ │
│ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ │
│ │ P₁ │ │ P₂ │ │ P₃ │ │ Pₙ │ │
│ └──┬──┘ └──┬──┘ └──┬──┘ └──┬──┘ │
│ │ │ │ │ │
│ ModelCaller ModelCaller ... ModelCaller │
│ │ │ │ │ │
│ ▼ ▼ ▼ ▼ │
│ CONFIDENCE: N ← extracted from trailing line │
└─────────────────────────┬───────────────────────────────┘
│
▼
score₁ = round(clamp(μ − 0.5·σ, 0, 100))
│
▼
┌─────────────────────────────────────────────────────────┐
│ ROUND 2 — COUNTERARGUMENTS │
│ (sequential, randomized order, full history) │
│ │
│ P? ──► P? ──► P? ──► P? │
│ Each participant sees every prior response │
│ from round 1 AND earlier in round 2. │
└─────────────────────────┬───────────────────────────────┘
│
┌───────────────┴───────────────┐
│ │
▼ ▼
|score₂ − score₁| ≤ Δ ───yes──► earlyStop event
│ no stopReason = "converged"
▼
┌─────────────────────────────────────────────────────────┐
│ ROUND 3 — EVIDENCE ASSESSMENT │
└─────────────────────────┬───────────────────────────────┘
▼
┌─────────────────────────────────────────────────────────┐
│ ROUND 4..N − 1 — SYNTHESIS & REFINEMENT │
│ ROUND N — FINAL SYNTHESIS │
│ (loops until maxRounds or convergence) │
└─────────────────────────┬───────────────────────────────┘
▼
FINAL-ROUND RESPONSES
│
┌─────────────┴─────────────┐
│ │
judge?=true (always)
│ │
▼ ▼
┌──────────────────┐ ConsensusResult {
│ JUDGE_PERSONA │ rounds, finalScore,
│ (non-voting) │ finalAverageConfidence,
│ │ stopReason, synthesis?
│ • Majority │ }
│ • Minority │
│ • Unresolved │
│ • JUDGE_CONFIDENCE
└──────────────────┘
| Round | Phase | Label | Visibility |
|---|---|---|---|
| 1 | initial-analysis |
Initial Analysis |
blind (parallel) |
| 2 | counterarguments |
Counterarguments |
full history |
| 3 | evidence-assessment |
Evidence Assessment |
full history |
| 4 … N−1 | synthesis |
Synthesis & Refinement (Round k) |
full history |
| N (last) | synthesis |
Final Synthesis |
full history |
- Round 1 is blind by default. Participants run in parallel, see no one else. Flip
blindFirstRound: falseto go sequential (rare — mostly for deterministic replay). - Rounds 2+ are sequential. Speaking order is randomized unless
randomizeOrder: false. Each speaker sees everyone who came before them — including earlier speakers in the current round. - Every response must end with
CONFIDENCE: Nwhere N is an integer 0–100. If several markers appear, the last one with a number wins (an inline "Confidence: high" is ignored). Missing marker → 50 (neutral). - Consensus score =
round(clamp(μ − 0.5·σ, 0, 100))using population stddev. - Disagreement detected when two participants' confidences differ by ≥ 20 (tunable).
- Early stop when
|score_k − score_{k−1}| ≤ 3(tunable). Only checked from round 2 onward.
The library's single extension point. Implement it once for your provider; the engine calls it for every participant and the judge.
export type ModelCaller = (req: ModelCallRequest) => Promise<ModelCallResponse>;
export interface ModelCallRequest {
participantId: string; // "judge" for synthesis calls
modelId: string;
round: number;
phase: Phase; // "initial-analysis" | "counterarguments" | "evidence-assessment" | "synthesis"
system: string; // persona + phase instructions
user: string; // the question (or synthesis context for the judge)
temperature: number; // 0.7 participants, 0.3 judge (defaults — caller may override)
maxOutputTokens: number; // 1500 default
signal?: AbortSignal; // honor this
onToken?: (t: string) => void; // optional streaming sink
}
export interface ModelCallResponse {
content: string; // must include the trailing CONFIDENCE: N line
usage?: { inputTokens: number; outputTokens: number; totalTokens: number };
}Implementation rules.
- Honor
signal. The engine propagates cancellation; if you ignore it, your consumers can't cancel a run. - Stream if you can. Call
onTokenwith each chunk; observers get real-time UI for free. - Don't re-throw
AbortErroras something else. The engine short-circuits cleanly on it. - Don't swallow other errors. Throw. The engine captures the error into
ParticipantResponseand keeps running. - Return the full content verbatim. Do not strip the trailing
CONFIDENCE:line — the parser needs it.
Participants can invoke tools mid-turn. The library never executes a tool itself — it plumbs the request from the model to a host-supplied ToolExecutor, feeds the results back, and re-invokes the caller until the model returns final content (or maxToolIterations is exhausted).
import { ConsensusEngine, type ToolDefinition, type ToolExecutor } from "ai-consensus-core";
const READ_FILE_TOOL: ToolDefinition = {
name: "read_file",
description: "Read a file by absolute path.",
parameters: { type: "object", properties: { path: { type: "string" } }, required: ["path"] },
};
const participants = [
{
id: "domain",
modelId: "claude-sonnet-4-6",
persona: domainExpertPersona,
tools: [READ_FILE_TOOL], // declared per-participant
},
// …
];
const toolExecutor: ToolExecutor = async (call, ctx) => {
// ctx: { participantId, round, phase, signal? }
if (call.name === "read_file") {
const args = call.arguments as { path: string };
return { content: await readFile(args.path, "utf8") };
// …or { error: "permission denied" } to feed an error back into the conversation
}
return { error: `unknown tool ${call.name}` };
};
const engine = new ConsensusEngine(modelCaller);
const result = await engine.run({
question: "What does the build output say?",
participants,
toolExecutor,
maxToolIterations: 8, // optional, default 8, clamped to [1, 32]
});ModelCaller responsibilities (when tools is present on the request):
- Translate the
toolsarray into whatever the underlying provider expects (OpenAI'stools, Anthropic'stools, etc.). - Translate
toolCallTurns(when present, on follow-up calls) into the conversation history the provider expects — typically: assistant message with tool_calls, then tool messages with results, in order. - Parse the model's response and surface
toolCallsonModelCallResponseif the model wants to dispatch tools. EachToolCallcarries{ id, name, arguments }whereargumentsis already JSON-parsed. The library never parses the model's raw argument string.
Engine guarantees:
- The tool loop runs per participant turn, separately for each model call. Tool history does not leak between participants or between rounds.
- The executor receives a fresh
ToolCallContext(withparticipantId,round,phase,signal) per call. - An exception thrown by the executor is captured as a
{ error: message }result and forwarded back into the conversation — the participant turn does not abort. AbortErrorthrown by the executor (orsignaltriggered) propagates up and aborts the whole run withstopReason: "aborted".- Hitting
maxToolIterationsbreaks the loop and uses the last response'scontent— even if the model still wants more tools. - Without
toolExecutor, the engine ignores anytoolCallson the response: 0.10 behaviour preserved exactly.
Events:
engine.on("toolCallStart", (e: ToolCallStartEvent) => void);
engine.on("toolCallComplete", (e: ToolCallCompleteEvent) => void); // ok: boolean, durationMs, preview (≤ 200 chars)
engine.on("toolError", (e: ToolErrorEvent) => void); // fires when ok === falseiteration (1-based) on these events disambiguates round-trips within a single participant turn.
engine.on("roundStart", (e: RoundStartEvent) => void);
engine.on("participantStart", (e: ParticipantStartEvent) => void);
engine.on("participantToken", (e: ParticipantTokenEvent) => void); // only fires if caller streams
engine.on("participantComplete", (e: ParticipantCompleteEvent) => void);
engine.on("confidenceUpdate", (e: ConfidenceUpdateEvent) => void);
engine.on("disagreementDetected", (e: DisagreementDetectedEvent) => void);
engine.on("roundComplete", (e: RoundCompleteEvent) => void);
engine.on("earlyStop", (e: EarlyStopEvent) => void);
engine.on("synthesisStart", (e: SynthesisStartEvent) => void);
engine.on("synthesisToken", (e: SynthesisTokenEvent) => void);
engine.on("synthesisComplete", (e: SynthesisCompleteEvent) => void);
engine.on("finalResult", (e: FinalResultEvent) => void);
engine.on("toolCallStart", (e: ToolCallStartEvent) => void); // per dispatch
engine.on("toolCallComplete", (e: ToolCallCompleteEvent) => void);
engine.on("toolError", (e: ToolErrorEvent) => void); // ok === false
engine.on("error", (err: Error) => void);Event order for one round of three participants:
roundStart
participantStart (p1) → [participantToken × N if streaming] → participantComplete → confidenceUpdate
participantStart (p2) → …
participantStart (p3) → …
[disagreementDetected × 0..N]
roundComplete
[earlyStop?]
…next round
[synthesisStart → synthesisToken × N → synthesisComplete]
finalResult
interface ConsensusOptions {
question: string; // required, non-empty
participants: Participant[]; // required, ≥ 2, unique ids
maxRounds?: number; // default 4, clamped to [1, 10]
earlyStop?: boolean; // default true
convergenceDelta?: number; // default 3
disagreementThreshold?: number; // default 20
blindFirstRound?: boolean; // default true
randomizeOrder?: boolean; // default true
participantTemperature?: number; // default 0.7
maxOutputTokens?: number; // default 1500
judge?: {
modelId: string;
caller?: ModelCaller; // defaults to engine's main caller
temperature?: number; // default 0.3
maxOutputTokens?: number; // default 1500
};
randomSeed?: number; // deterministic round-order shuffle
signal?: AbortSignal; // cancellation
}The library ships only the non-voting judge persona (JUDGE_PERSONA).
Debate personas are caller-owned content — construct any object matching
PersonaSchema and pass it on each Participant.
For a copy-paste block of the seven debate personas from the Roundtable
playbook (Risk Analyst, First-Principles Engineer, VC Specialist,
Scientific Skeptic, Optimistic Futurist, Devil's Advocate, Domain Expert),
see docs/personas.md.
The judge:
| id | Name | Role |
|---|---|---|
judge |
Consensus Judge | Non-voting synthesizer. Produces Majority / Minority / Unresolved sections. |
JUDGE_PERSONA.systemPrompt is the engine's default when
ConsensusOptions.judge.systemPrompt is omitted. Override it only if the
replacement emits the same four ## headings and trailing
JUDGE_CONFIDENCE: N line — extractJudgeSection /
extractJudgeConfidence key off that contract.
import { JUDGE_PERSONA, PersonaSchema, type Persona } from "ai-consensus-core";
// Validate caller-supplied personas at the boundary:
const persona = PersonaSchema.parse(untrusted);import { consensusScore, detectDisagreements } from "ai-consensus-core";
consensusScore([85, 82, 78, 40]);
detectDisagreements({
round: 2,
responses, // ParticipantResponse[]
participants,
threshold: 20,
});const ac = new AbortController();
setTimeout(() => ac.abort(), 5_000);
const result = await engine.run({ ...options, signal: ac.signal });
// result.stopReason === "aborted" if the timeout firesThe signal is forwarded into every ModelCaller invocation. Any provider that respects AbortSignal (most do) tears down cleanly.
Pass randomSeed to make round-order shuffling reproducible. Combined with a deterministic ModelCaller (e.g. one that replays recorded responses), a whole run becomes bit-for-bit reproducible — perfect for snapshot tests.
await engine.run({ ...options, randomSeed: 42 });AdjudicationEngine judges N candidate solutions to one coding task — for example the branches of a best-of-N run. The CVP debate is the wrong tool here: its score is self-reported confidence, which says nothing about which diff is correct, and several sequential rounds are too slow for the end of every run.
import { AdjudicationEngine } from "ai-consensus-core";
const engine = new AdjudicationEngine(caller); // the same ModelCaller as ConsensusEngine
const result = await engine.adjudicate({
task: "Add retry with jitter to the HTTP client",
candidates: [
{ id: "attempt-1", content: diff1, checks: [{ name: "tests", status: "passed" }] },
{ id: "attempt-2", content: diff2, checks: [{ name: "tests", status: "failed", output: "2 failing" }] },
{ id: "attempt-3", content: diff3 },
],
jurors: [reviewerA, reviewerB, reviewerC], // Participant[], at least two
judge: { modelId: "claude-opus-4-5" }, // optional, never votes
randomSeed: 42,
budget: { maxTokens: 400_000, deadlineMs: 120_000 },
});
result.verdict; // "winner" | "no-clear-winner"
result.recommended; // "attempt-1" | nullShape. One blind, parallel round of jurors; deterministic aggregation; a rebuttal round only while agreement is below the threshold (maxRounds, default 2, max 3); then the existing non-voting judge, whose output keeps the ## Majority Position / ## Minority Positions / ## Unresolved Disputes sections.
Blinding. Each juror sees the candidates under neutral labels A, B, C… in its own shuffled order (drawn from randomSeed before any call). Candidate ids never reach a model — not the jurors, not the judge. Labels are mapped back to ids after parsing.
Juror contract. One JSON object: a full ranking (every label once, best first), assessments (one short summary per candidate) and findings (candidate, severity high|medium|low, file, line integer or null, a verbatim quote, issue). The output must be the JSON object alone, or a single fenced json block. Anything else — prose around it, a partial ranking, an unknown label, a mistyped field — is an abstention, never a crash. buildJurorVerdictJsonSchema(labels) returns the matching JSON Schema in strict structured-output form (all properties required, no additional properties) for callers that can enforce it.
Verification. A finding is kept only if its quote appears in the content of the candidate it names, after whitespace normalization, either verbatim or with the leading +/-/space diff markers removed. Quotes shorter than 8 characters (minQuoteLength) never verify. Dropped findings are counted per candidate and in total. No extra model calls.
Aggregation. Borda count over the jurors' latest valid rankings; Kendall's W for agreement; ties in points are broken by first-place votes, then fewer severity-weighted verified findings, then candidate id. A candidate with a failed check is ineligible for recommendation unless every candidate failed one.
Verdict. The leader (best eligible candidate) is recommended only when every juror returned a first-round verdict, at least two votes counted, W ≥ agreementThreshold (default 0.3) and the leader has strictly more Borda points than the next eligible candidate. Otherwise verdict is "no-clear-winner" with noClearWinnerReason = incomplete | insufficient-votes | low-agreement | tie. Dissent lists the jurors whose top pick differs from the leader.
W measures agreement on the whole ordering, not on the winner, so it behaves differently by panel shape. One dissenting juror against an otherwise unanimous panel gives:
| candidates | jurors | dissenter swaps the top two | dissenter picks another winner | dissenter reverses the order |
|---|---|---|---|---|
| 2 | 3 | 0.11 | 0.11 | 0.11 |
| 2 | 5 | 0.36 | 0.36 | 0.36 |
| 3 | 3 | 0.78 | 0.33 | 0.11 |
| 4 | 3 | 0.91 | 0.47 | 0.11 |
| 6 | 3 | 0.97 | 0.62 | 0.11 |
With the default threshold, two candidates need a unanimous panel of 3 (or 4 of 5); with three or more candidates and at least three jurors, a single dissenter who still ranks the leader second passes. A 1–1 split on the winner can produce a high W (two jurors, four candidates, top two swapped: 0.90), which is why a points tie at the top is reported as tie regardless of W.
Budget. budget.maxTokens (reported usage, else a chars/4 estimate), budget.deadlineMs (wall clock from the start) and budget.costCap ({ limitUSD, estimateUSD(modelId, usage) }) work alongside signal. Before each round and before the judge, the engine refuses to dispatch calls whose worst case (input estimate + full maxOutputTokens) would cross a ceiling; after each call it checks actual usage and cancels in-flight calls on a breach. Calls that ignore their signal are abandoned at the deadline. The run then resolves with a partial result and stopReason = deadline | token-budget | cost-cap | aborted (finished verdicts are kept; unfinished ones are cancelled).
Events. roundStart, jurorStart, jurorToken, jurorComplete, roundComplete, rebuttalTriggered, synthesisStart / synthesisToken / synthesisComplete, budgetStop, finalResult.
The pieces are exported on their own: bordaCount, kendallW, rankCandidates, decideVerdict, computeDissent, verifyFindings, quoteAppearsIn, mergeFindings, parseJurorVerdict, the juror prompt builders and buildAdjudicationJudgeUserPrompt.
Ported from Roundtable so hosts share one implementation:
import { extractClaims, assertWithinCostCap, CostCapExceededError } from "ai-consensus-core";
const digest = await extractClaims(caller, {
modelId: "claude-opus-4-5",
finalResponses: result.rounds.at(-1)!.responses,
participants: result.participants,
});
digest.contradictions; // [{ claim, sides: [{ stance, participantIds, quote }] }]
assertWithinCostCap(runningCostUSD, capUSD); // throws CostCapExceededError above the capextractClaims makes one call and soft-fails (an error or unparseable output yields an empty digest with error set); aborts propagate.
Everything is exported from the root:
import type {
Persona,
Participant,
Phase,
TokenUsage,
ModelCaller,
ModelCallRequest,
ModelCallResponse,
ParticipantResponse,
Disagreement,
RoundResult,
SynthesisResult,
ConsensusResult,
ConsensusOptions,
StopReason,
ConsensusEventMap,
RoundStartEvent,
ParticipantStartEvent,
ParticipantCompleteEvent,
RoundCompleteEvent,
FinalResultEvent,
// …etc
} from "ai-consensus-core";Zod schemas are exported too, for boundary validation on your side:
import { PersonaSchema, ParticipantSchema } from "ai-consensus-core";
ParticipantSchema.parse(untrustedInput);git clone https://github.com/entropyvortex/ai-consensus-core.git
cd ai-consensus-core
npm install
npm run test # 261 tests, vitest
npm run test:coverage
npm run build # emits ESM + .d.ts into dist/- Why
avg − 0.5·σand not median / majority vote? A high mean with a tight spread should score higher than a high mean with one strong dissenter. A simple linear penalty on stddev does this cheaply and keeps the score on the same 0–100 scale as the raw confidences. - Why confidence-delta disagreements, not claim extraction? Extracting claims from free text is fragile and expensive. A 20-point confidence gap is a strong, cheap, deterministic signal. If you want richer structure, run the judge.
- Why sequential rounds 2+ instead of parallel? The protocol wants each speaker to have full visibility of the conversation so far. Parallel would let participants ignore each other and defeat the debate.
- Why the
CONFIDENCE: Nmarker instead of structured outputs? Every provider supports it. Structured outputs across five-plus providers is a coupling surface we didn't want.
Many multi-agent frameworks hard-code a single provider, assume a single use case, or layer opinions on top of the engine.
This library takes the other route. It's the minimum viable mechanism for multi-model consensus — no provider SDK, no CLI, no server, no opinions beyond the protocol itself. You bring the models and the shell; we bring the engine.
It is the engine layer under Roundtable and ai-consensus-mcp.
ai-consensus-mcp— thin stdio MCP server that wraps this library and exposesconsensusas a single tool for Claude Code / Cursor / Windsurf / any MCP host.
MIT
Part of the entropyvortex stack — practical, no-bullshit AI open source by Marcelo Ceccon.
Made with ❤️ in Brazil.
MIT License • Built to ship.