Data

What Is Jensen-Shannon Divergence?

A symmetric, bounded measure of how far two probability distributions differ from their average distribution.

What Is Jensen-Shannon Divergence?

Jensen-Shannon divergence is a data metric that measures how different two probability distributions are by comparing each one with their average distribution. In LLM and agent systems, it appears in eval pipelines, dataset monitoring, production traces, and training comparisons when teams compare baseline and current behavior. FutureAGI treats a rising Jensen-Shannon divergence score as an early drift signal: the input mix, retrieval population, evaluator scores, or embedding space may have changed enough to require investigation.

Why Jensen-Shannon Divergence Matters in Production LLM and Agent Systems

Distribution drift rarely announces itself as one clean failure. It usually shows up as a quiet change in who asks questions, which documents are retrieved, which tool paths are selected, or which evaluator scores dominate a release report. Jensen-Shannon divergence gives engineers a bounded way to compare those distributions before the change becomes a user-visible hallucination, stale-context failure, or missed compliance escalation.

For developers, the pain is regression ambiguity. A prompt release may look worse because the model changed, or because the current traffic cohort contains more long-tail policy questions than the baseline. For SREs, the symptom is a dashboard that shows stable p99 latency while answer quality falls in one route. Product teams see conversion or escalation shifts without knowing whether the root cause is model behavior, data mix, or retrieval coverage. Compliance teams care because drift can move protected categories, policy topics, or PII-heavy requests into paths that were never tested.

Jensen-Shannon divergence is especially useful in 2026-era multi-step pipelines because each step creates a distribution: retrieved chunk source, tool name, planner action, final evaluator score, token-cost bucket, and user-feedback outcome. Unlike KL divergence, JSD is symmetric and bounded, so baseline-to-current and current-to-baseline comparisons tell the same story. A high value does not prove the model is wrong; it says the evidence population changed enough that old release assumptions may no longer hold.

How FutureAGI Handles Jensen-Shannon Divergence

Because this slug’s fagi_anchor is none, Jensen-Shannon divergence is not a named FutureAGI evaluator in the current inventory. Treat it as a dataset and monitoring calculation around FutureAGI evidence, not as an evaluator class. FutureAGI’s approach is to use JSD as a triage signal that tells an engineer which cohorts deserve deeper eval review.

A practical workflow starts with two populations: a trusted baseline from fi.datasets.Dataset and a current slice from production traces. The engineer bins a comparable field, such as retrieval source, embedding-cluster ID, ContextRelevance score bucket, Groundedness pass band, or model route. For a LangChain RAG agent instrumented through the traceAI langchain integration, the same analysis can be grouped by trace metadata and span evidence such as llm.token_count.prompt, retrieval latency, or final answer route.

Suppose a support agent’s global pass rate is flat, but JSD for ContextRelevance buckets jumps from 0.04 to 0.31 on enterprise-plan questions. The engineer checks the trace cohort, sees that new billing-policy documents replaced older retrieval hits, and reruns a regression eval on that dataset version. If Groundedness then drops on the same cohort, the next action is a rollback, retriever fix, or narrower threshold before routing more traffic.

Compared with a warehouse-only drift alert, this connects the distribution change to evaluator outcomes, trace evidence, and release action. JSD should trigger investigation; FutureAGI evaluators decide whether the changed cohort is still acceptable.

How to Measure or Detect Jensen-Shannon Divergence

Measure JSD only after the two populations are comparable. Pick one field, normalize both populations into probability distributions over the same bins, and track the score over time. With log base 2, JSD ranges from 0 to 1; 0 means identical distributions, and larger values mean stronger separation.

  • Baseline versus current score: compare baseline_distribution and current_distribution by dataset version, model route, retrieval source, language, customer tier, or time window.
  • Evaluator-score drift: bin EmbeddingSimilarity, ContextRelevance, or Groundedness scores and alert when one cohort diverges from the approved reference distribution.
  • Trace cohort signal: group by traceAI integration, route, or span evidence such as llm.token_count.prompt to isolate where the drift enters the pipeline.
  • Dashboard signal: monitor JSD next to eval-fail-rate-by-cohort, thumbs-down rate, escalation rate, token-cost-per-trace, and rollback frequency.
import math

def jsd(p, q, base=2):
    m = [(a + b) / 2 for a, b in zip(p, q)]
    def kl(a, b):
        return sum(x * math.log(x / y, base) for x, y in zip(a, b) if x)
    return 0.5 * kl(p, m) + 0.5 * kl(q, m)

Common Mistakes

  • Comparing unaligned bins. JSD is meaningless if the baseline and current histograms use different labels, buckets, or embedding-cluster definitions.
  • Treating JSD as correctness. A high score signals drift, not failure; pair it with evaluators such as Groundedness or ContextRelevance.
  • Hiding cohort movement in global averages. A flat overall score can mask a sharp shift in locale, customer tier, retrieval source, or tool path.
  • Using raw counts instead of probabilities. Normalize both distributions before computing the score, especially when traffic volume changes.
  • Ignoring small sample sizes. Sparse bins can make a drift alert look precise when the cohort needs more observations or wider buckets.

Frequently Asked Questions

What is Jensen-Shannon divergence?

Jensen-Shannon divergence measures how different two probability distributions are by comparing each one with their average distribution. In AI reliability work, it is commonly used to detect data, embedding, retrieval, or evaluator-score drift.

How is Jensen-Shannon divergence different from KL divergence?

KL divergence is directional and can become unbounded when supports differ. Jensen-Shannon divergence averages two KL comparisons against a shared mixture, making it symmetric and bounded.

How do you measure Jensen-Shannon divergence?

Build normalized baseline and current distributions, then compute JSD by cohort, route, dataset version, or trace field. In FutureAGI workflows, pair that drift signal with evaluator cohorts such as `EmbeddingSimilarity`, `ContextRelevance`, and `Groundedness`.