Online metrics are computed during LLM generation, not after. Rather than evaluating a finished response against retrieved documents, they tap into the generation process itself — specifically the log probabilities that the LLM assigns to each output token. This makes them fast (no extra API calls) and complementary to offline metrics, giving you a real-time view into how certain the model was while producing its response.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.
The Confidence Score
The Confidence Score is TrustifAI’s single online metric. It quantifies how sure the LLM was about its own output by analyzing the probability distribution across generated tokens. How it works: TrustifAI captures the per-token log probability (logprob) values returned by the LLM alongside the generated text. From these it computes:
- Geometric mean probability —
exp(mean(logprobs))gives the normalized per-token probability for the entire sequence. This captures the model’s average certainty across all tokens. - Variance penalty —
exp(-variance(logprobs))penalizes sequences where the model oscillated between high- and low-confidence tokens. Consistent uncertainty (uniform logprobs) is rated less harshly than erratic uncertainty.
[0, 1] where higher means the model was more consistently certain about its output.
The Confidence Score is only available for LLMs that expose token log probabilities. This includes OpenAI models (e.g.,
gpt-4o, gpt-4, gpt-4-turbo) when logprobs: true is set in your config. Models served through providers that strip logprob data will return score: 0.0 with label N/A.Threshold labels
| Score | Label | Interpretation |
|---|---|---|
| ≥ 0.90 | High Confidence | Model is highly certain about its output |
| ≥ 0.70 | Medium Confidence | Model shows moderate uncertainty |
| < 0.70 | Low Confidence | Model is uncertain — treat output with caution |
0.90 and 0.70. You can override them by adding them to any metric’s params section in your YAML — for example, alongside the trust_score thresholds:
Using generate() to get Confidence Scores
The generate() method wraps an LLM call and automatically computes the Confidence Score from the returned logprobs.
Initialize the engine
The engine reads your LLM config, including the model name and logprob settings, from the YAML file.
Call generate()
Pass your prompt (and an optional system prompt). TrustifAI automatically requests logprobs from the LLM.
Interpreting the result dict
generate() returns a dictionary with two top-level keys:
| Field | Description |
|---|---|
response | The generated text |
metadata.confidence_score | Confidence Score in [0, 1] |
metadata.confidence_label | High Confidence, Medium Confidence, or Low Confidence |
metadata.confidence_details.avg_logprob | Mean log probability across tokens |
metadata.confidence_details.variance | Variance of log probabilities (higher = more erratic) |
metadata.confidence_details.token_count | Number of tokens scored |
metadata.logprobs_available | false if the LLM did not return logprobs |
metadata.execution_metadata.total_cost_usd | API cost for this call |
Enabling logprobs in config
Make sure your LLM config requests logprobs. TrustifAI sets this automatically whengenerate() is called, but having it in the config ensures consistency: