Models

What Is Kernel SHAP?

A model-agnostic Shapley-value approximation that uses weighted linear regression over feature coalitions to attribute a single prediction across input features.

What Is Kernel SHAP?

Kernel SHAP is a model-agnostic explanation method that approximates Shapley values by fitting a weighted linear model over sampled feature coalitions for one prediction. In production AI systems, teams use it when Tree SHAP, Deep SHAP, or model gradients are unavailable, especially for black-box LLM scorers, hosted classifiers, and ensemble models. In FutureAGI evaluation workflows, Kernel SHAP helps explain why a Faithfulness, Groundedness, or AnswerRelevancy score changed across a trace cohort.

Why Kernel SHAP matters in production LLM and agent systems

Most LLM-application teams don’t fine-tune; they consume hosted models. When a hosted classifier flags a customer message as “high churn risk” or “policy violation,” the team has no gradient access to explain the call. Kernel SHAP is the bridge: wrap the classifier as a Python function, sample feature coalitions, and produce per-feature attributions that an analyst can reason about. The same applies to LLM-as-judge scorers — when Faithfulness returns 0.4 on a particular row, Kernel SHAP can attribute the score across the input fields (query, context chunks, response) to surface which retrieved chunk pulled the score down.

Compliance and regulated-industry teams need this for model-card and audit purposes — “explain why this loan model rejected this applicant” is a literal requirement under EU AI Act conformity assessments. Product managers use it to debug judgment models when downstream metrics diverge from offline evaluations. SREs use it to triage an incident: if eval-fail-rate-by-cohort spikes on one cohort, Kernel SHAP tells you which input feature in that cohort is driving the regression.

In 2026 multi-step agent pipelines, the explanation surface gets harder. A failed trajectory has many potential blame points — planner, retriever, tool args, critique step. Kernel SHAP applied at each step produces step-level attributions that feed into a global trajectory explanation. The cost is compute; the value is auditability.

How FutureAGI handles Kernel SHAP attributions

FutureAGI does not bundle a SHAP implementation — we are the evaluation and observability layer above models, so SHAP attributions live in the offline analysis layer fed by FutureAGI’s evaluator outputs. FutureAGI’s approach is to treat Kernel SHAP as an offline attribution layer over evaluator and trace data, not as a production blocking step. At dataset level, an engineer runs Dataset.add_evaluation(Faithfulness) to score every row, then post-hoc runs shap.KernelExplainer over a wrapped scoring function — using the same Dataset rows as the SHAP background dataset. The result: per-row, per-feature attribution columns that join back to the FutureAGI dashboard cohorts.

At trace level, traceAI integrations such as langchain and openai emit OpenTelemetry spans with llm.input.messages, llm.output.text, and llm.token_count.prompt. Sampling 5–10% of spans into an offline cohort lets the team run Kernel SHAP without slowing production. The attributions feed into eval-fail-rate-by-cohort dashboards, so a regression on Groundedness is paired with the SHAP-implicated input feature.

Concretely: a RAG team running the traceAI pinecone integration sees Faithfulness drop on the “long-context” cohort. They wrap their grounding evaluator as a SHAP-friendly function, run Kernel SHAP across 200 sampled traces, and find that retrieved-chunk-3 attribution dominates negative scores when chunks exceed 800 tokens. Fix: tighten chunk-size strategy, rerun, recover the score. FutureAGI’s role is making the cohort sampling and regression-eval reproducible; Kernel SHAP plugs into that without bespoke infrastructure.

How to measure or detect Kernel SHAP

Kernel SHAP outputs and surrounding signals to track in production:

  • Per-feature SHAP value: signed magnitude per input feature; report top-3 positive and top-3 negative drivers per row.
  • Cohort-level mean SHAP: averaged attributions across an eval cohort; reveals systematic feature drift.
  • fi.evals.AnswerRelevancy — the score Kernel SHAP often explains in LLM applications.
  • fi.evals.EmbeddingSimilarity — when SHAP is applied to embedding-based scorers, this is the underlying signal.
  • Explained eval-fail-rate-by-cohort: join SHAP drivers to FutureAGI cohort filters, so incidents show both score deltas and likely input causes.
  • SHAP convergence diagnostic: the nsamples parameter affects stability; report variance across 3 replicate runs.
  • Cost-per-explanation: Kernel SHAP is compute-heavy; track wall-clock time per row to budget the analysis cohort.

Use the snippet below on a small offline cohort, freeze the evaluator version, and keep background rows representative of the production distribution.

import shap
from fi.evals import Faithfulness

faithfulness = Faithfulness()

def score_fn(rows):
    return [faithfulness.evaluate(input=r["q"], output=r["a"], context=r["c"]).score
            for r in rows]

explainer = shap.KernelExplainer(score_fn, background_rows[:50])
shap_values = explainer.shap_values(test_rows[:20], nsamples=200)

Common mistakes

  • Using Kernel SHAP when Tree SHAP or Deep SHAP applies. For tree ensembles or differentiable neural nets, model-specific explainers are faster, cheaper, and often more stable on large cohorts.
  • Using a tiny background dataset. The background rows set the missing-feature baseline; <50 or off-distribution rows can flip top drivers across reruns before cohorts stabilize.
  • Reading SHAP signs without scale. A +0.01 value may be sampling noise; compare magnitude, confidence intervals, business thresholds, and incident cost before escalating.
  • Running SHAP on every production span. Kernel SHAP multiplies scorer calls; sample 5–10% into offline cohorts with fixed evaluator settings and reserve full runs for incidents.
  • Ignoring SHAP variance. Run at least 3 replicate explanations with different nsamples; if top features shift, report attribution as unstable and rerun with more samples.

Frequently Asked Questions

What is Kernel SHAP?

Kernel SHAP is a model-agnostic Shapley-value approximation that uses a weighted linear regression over feature-coalition samples to attribute a single prediction across input features. It works on any black-box model.

How is Kernel SHAP different from Tree SHAP or Deep SHAP?

Tree SHAP exploits tree-ensemble structure for exact, fast attributions. Deep SHAP uses DeepLIFT-style backprop through neural networks. Kernel SHAP treats the model as a black box and is slower but model-agnostic.

How do you use Kernel SHAP in an LLM pipeline?

Wrap a classifier or scorer as a function and call `shap.KernelExplainer`. For LLM evals, use Kernel SHAP to attribute which input features drive a `Faithfulness` or `Groundedness` score, surfacing failure cohorts in FutureAGI.