Documentation Index
Fetch the complete documentation index at: https://mintlify.com/TrustifAI/trustifai/llms.txt
Use this file to discover all available pages before exploring further.
ConfidenceMetric is TrustifAI’s only online metric — it runs during LLM generation rather than post-hoc. It converts the per-token log probability stream emitted by the LLM into a single confidence score that reflects both the model’s average certainty and the consistency of that certainty across the generated sequence. Unlike offline metrics, you do not call ConfidenceMetric.calculate directly: it is invoked automatically inside Trustifai.generate().
ConfidenceMetric requires a language model that exposes token-level log probabilities. OpenAI-compatible APIs (including Gemini via response_logprobs=True) and most self-hosted models support this. If your LLM does not return logprobs, generate() returns score: 0.0 with label: "N/A".Static method
List of per-token log probability values (negative floats) as returned by the LLM API. The list corresponds to the generated response tokens in order. An empty list returns
score: 0.0, label: "N/A".A
ThresholdEvaluator instance used to map the computed score to a (label, explanation) pair. TrustifAI passes this automatically when calling from generate().Score computation
The score is derived in three steps:- Average log probability —
avg_logprob = mean(logprobs), a length-normalized proxy for sequence probability. - Variance penalty —
penalty = exp(−var(logprobs)), which reduces the score when the model was inconsistently uncertain across tokens. - Final score —
score = exp(avg_logprob) × penalty, clipped to[0.0, 1.0]by the natural range of the formula.
Return value
calculate returns a plain dict (not a MetricResult) for compatibility with the generate() response envelope:
"High Confidence"— model is highly certain"Medium Confidence"— moderate uncertainty"Low Confidence"— model is uncertain about its output
Usage via Trustifai.generate()
You access ConfidenceMetric through the generate() method, which handles logprob collection and metric calculation automatically:
generate() return value has two top-level keys — "response" (the generated text) and "metadata" (confidence and cost info):