
Training-free inference-time defense for LLM prefill jailbreaks that reads refusal and harmfulness signals at the assistant role token and restores refusal during prefilling and decoding.
Installation · Quick Start · Evaluation · Citation
Overview of J-Anchor: (a) a prefill attack forces an assistant prefix after the assistant header; (b) J-Anchor reads refusal and harmfulness in J-space at the header as a pre-attack anchor and restores refusal toward an anchor-derived target during prefilling and decoding, behind a request-level gate; (c) the model refuses instead of continuing the unsafe prefix.
Overview of J-Anchor. (a) A forced assistant prefix encourages unsafe continuation; J-Anchor establishes a request-specific anchor at the assistant header before the forced prefix is processed. (b) It reads refusal and harmfulness signals from vocabulary-grounded subspaces, calibrated against benign activations; a request-level gate decides whether to intervene, and refusal components are restored toward anchor-derived targets during prefilling and decoding. (c) The corrections steer the model away from the unsafe continuation and toward refusal.
Prefill jailbreaks make a safety-aligned LLM continue an attacker-written assistant prefix ("Sure! Here is…"). Tracking the model's internal states with the Jacobian lens (J-lens), we find refusal-state erosion: as the prefill proceeds, the refusal signal decays to its benign baseline, while the harmfulness signal stays detectable. J-Anchor is a training-free, inference-time defense that
J-Anchor is a small PyTorch + 🤗 Transformers package; it was developed with Python 3.12, torch 2.12 and transformers 5.9 on a single GPU (bf16).
git clone https://github.com/beidiz/J-Anchor.git
cd J-Anchor
pip install -r requirements.txt # Python 3.12: the exact package set of the reported results
Llama, Gemma and WildGuard are gated on Hugging Face: accept their licenses and log in (hf auth login, or HF_TOKEN).
Paths are set by environment variables (config.py); by default resources live under resources/ and the
evaluation sets under data/eval/ in the repository:
| variable | default | holds |
|---|---|---|
JANCHOR_RES | <repo>/resources | the resource root |
JANCHOR_JLENS_DIR | $JANCHOR_RES/jlens | J-lens files |
JANCHOR_EVAL_DATA | <repo>/data/eval | evaluation sets (see Evaluation) |
JANCHOR_ENV | unset | optional shell file that scripts/gpu.sh sources before running a command |
JANCHOR_WILDGUARD_PYTHON | unset | Python of a separate environment with pip install git+https://github.com/allenai/wildguard (ORR judge) |
Additional requirements:
| What | Needed for | Where it is configured |
|---|---|---|
| J-lens of each model (Gurnee et al., 2026; jacobian-lens): find them in neuronpedia/jacobian-lens | calibration | $JANCHOR_JLENS_DIR/<file> (file names in janchor/models.py) |
| HF model weights (gated: Llama, Gemma) | everything | standard HF cache ($HF_HUB_CACHE) |
1. Calibrate once per model (≈1–3 min on one GPU). This builds the J-space refusal / harmfulness subspaces,
their benign statistics, and the gate layers, and writes calib/<model>.pt:
python -m janchor.calibrate --model llama
# or all supported models: bash scripts/calibrate_all.sh
2. Generate with the defense. The input is a jsonl with a prompt field (all other fields are copied through):
# fixed prefill attack: "Sure! Here are the detailed step-by-step instructions you asked for:\n\n1."
python -m janchor.generate --model llama --input prompts.jsonl --prefill fixed --out out.jsonl
# deep prefill: each row's own "prefill" text, truncated to its first 500 tokens
python -m janchor.generate --model qwen3.5 --input deep.jsonl --prefill field --prefill_tokens 500 --out out.jsonl
# benign / over-refusal evaluation (no forced prefix)
python -m janchor.generate --model gemma4 --input xstest.jsonl --prefill none --max_new 256 --out out.jsonl
# the undefended model with identical decoding
python -m janchor.generate --model llama --input prompts.jsonl --prefill fixed --no_defense --out base.jsonl
Each output row adds response, n_tokens, gate_score, and protected (whether the gate enabled J-Anchor).
Runs resume: rows already in --out are skipped.
from janchor.generate import load_defended, generate_file
# loads the model and attaches J-Anchor's forward hooks (rho / gate default to the model's settings)
spec, tok, hf, ja = load_defended("llama", rho=None, tau=0.75, gate="auto")
generate_file(spec, tok, hf, ja, "prompts.jsonl", "out.jsonl", prefill="fixed", max_new=512, batch=16)
ja.remove() # detach the hooks
janchor.defense.JAnchor is the defense itself: prepare() runs the read-only gate pass over a batch of prompts;
setting ja.mode = "push" around hf.generate(...) applies the restoration.
| flag | default | meaning |
|---|---|---|
--rho | model default | restoration strength ρ (target = ρ × anchor) |
--tau | 0.75 | gate threshold, in benign standard deviations |
--gate | auto | auto (model default), on, off |
--no_defense | off | run the undefended model |
--calib | calib/<model>.pt | calibration file |
--prefill | none | none, fixed, or field (row's own --prefill_field) |
--prefill_tokens | 0 | keep only the first N prefill tokens (0 = all) |
--max_new / --batch | 512 / 16 | decoding length / batch size (greedy, bf16) |
--no_repeat | 0 | no_repeat_ngram_size (0 = off) |
--n | 0 | only the first N rows |
--model | Model |
|---|---|
llama | meta-llama/Llama-3.1-8B-Instruct |
gemma4 | google/gemma-4-E4B-it |
qwen3.5 | Qwen/Qwen3.5-4B |
qwen3 | Qwen/Qwen3-8B |
Their default settings are in janchor/models.py.
1. Get a J-lens for the model. First check whether a pre-fitted lens is already available for it: see the
official jacobian-lens repository and the
J-lens page on Neuronpedia. If there is none, fit one yourself with the
jlens package from that repository (pip install -e . in a clone):
import transformers, jlens
from jlens.examples import load_wikitext_prompts
hf = transformers.AutoModelForCausalLM.from_pretrained("org/My-Model").cuda()
tok = transformers.AutoTokenizer.from_pretrained("org/My-Model")
model = jlens.from_hf(hf, tok)

Open-source XDR and SIEM platform for threat detection, log analysis, file integrity monitoring, vulnerability assessment, and compliance management across endpoints and cloud workloads.

Open-source network IDS/IPS/NSM engine for real-time traffic inspection, intrusion detection and prevention, protocol analysis, and rule-based threat hunting.

Zeek is a powerful network analysis framework that is much different from the typical IDS you may know.