residual-stream-states

Probe LLM internal representations to detect hallucinations and analyze answer correctness.

75|7|Updated May 2, 2026
One-click install
npx skills add https://github.com/zjunlp/Mechanist --skill residual-stream-states-zjunlp
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: residual-stream-states
Source: https://github.com/zjunlp/Mechanist/tree/main/skills/mechanism-skills/probing/residual-stream-states
Command: npx skills add https://github.com/zjunlp/Mechanist --skill residual-stream-states-zjunlp

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires torch, transformers, scikit-learn, pandas, numpy, wandb, datasets, and includes scripts (resource) and references (resource) components.

What problem does it solve? Determining whether a language model's answer is correct or a hallucination usually requires external ground truth, but this Skill trains linear probes on the model's own hidden states to predict correctness from internal representations. ## Core Features & Use Cases - Representation Extraction: Capture hidden states and MLP activations from specific layers and token positions in models like Mistral-7B and Llama-3-8B using forward hooks. - Hallucination Probing: Train logistic regression probes on extracted representations to classify answers as correct or hallucinated, with accuracy, precision, recall, and F1 metrics. - Layer-Token Analysis: Generate heatmaps of probe performance across layers and token positions, plus error-type classification from resampled answers. - Use Case: Run the full experiment pipeline on TriviaQA to find which layer best encodes answer correctness, then use the trained probe to flag likely hallucinations in new generations. ## Quick Start Probe Mistral-7B's internal representations on TriviaQA and tell me which layer best predicts whether its answers are hallucinations.

Frequently Asked Questions about residual-stream-states

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I detect LLM hallucinations using internal representations?

Extract hidden states from a chosen layer and token position, label answers as correct or incorrect against ground truth, then train a logistic regression probe on those representations. The probe's accuracy indicates how strongly the layer encodes correctness information.

How to probe which transformer layer encodes answer correctness?

Run the probing experiment across a layer range, training a separate probe per layer and token position. Compare cross-validated accuracy across layers to build a heatmap; the highest-accuracy layer is where correctness is most linearly decodable.

Which models does this hallucination probing support?

The scripts default to mistralai/Mistral-7B-Instruct-v0.2 and also support Mistral-7B-v0.3 and Meta-Llama-3-8B variants from Hugging Face. Any AutoModelForCausalLM-compatible model with accessible hidden states can be substituted via the model name argument.

Can I probe MLP activations instead of residual stream hidden states?

Yes, the prober registers forward hooks on a layer's MLP module to capture both MLP input and output activations. The probe_location parameter selects between mlp, attention, and other module positions.

Why does probe training fail on my dataset?

Probe training requires both correct and incorrect labels in the data; the scripts skip layers when only one class is present. Generate enough answers with natural model errors, or balance the dataset before training the classifier.

What are the limitations of linear probes for hallucination detection?

Linear probes only detect linearly decodable information and may reflect dataset-specific correlations rather than genuine model knowledge. Small sample sizes and exact-match correctness labels can also inflate or distort measured accuracy.