steering-vectors

Generate and apply Contrastive Activation Addition steering vectors to control Llama 2 model behavior.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires torch, transformers, numpy, matplotlib, seaborn, scikit-learn, scipy, pandas, python-dotenv, einops, and includes scripts (resource) and references (resource) components.

What problem does it solve? Controlling specific behaviors of large language models like sycophancy or hallucination typically requires expensive fine-tuning. This Skill implements Contrastive Activation Addition (CAA) to generate steering vectors from contrastive examples and apply them at inference time to shift model behavior without retraining. ## Core Features & Use Cases - Steering Vector Generation: Collect activations from contrastive positive/negative behavioral examples and compute difference vectors at chosen layers of Llama 2 models. - Inference-Time Steering: Apply pre-computed vectors via forward hooks with configurable multipliers to amplify or suppress behaviors during text generation. - Vector Analysis & Visualization: Compute cosine similarities, PCA projections, norm distributions, and heatmaps across behaviors and layers. - Use Case: A researcher studying sycophancy generates a steering vector at layer 13 of Llama-2-7b-chat, then sweeps multipliers from -2.0 to 2.0 to measure how model agreement behavior changes on evaluation questions. ## Quick Start Generate steering vectors for the sycophancy behavior on Llama-2-7b-chat and compare steered responses across multipliers on a test prompt.

Frequently Asked Questions about steering-vectors

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

FAQPage Schema
How do I generate steering vectors with Contrastive Activation Addition?

Run generate_steering_vectors.py, which collects last-token activations from positive and negative behavioral examples at chosen layers, averages each set, and saves the difference as a steering vector. Vectors are stored as PyTorch .pt files per behavior and layer.

How to apply steering vectors to a Llama 2 model during inference?

Use apply_steering.py to load a saved vector and register a forward hook on the target layer that adds the scaled vector to hidden states. The multiplier parameter controls steering strength, with negative values suppressing the behavior.

Which layer works best for steering Llama-2-7b-chat behavior?

The scripts default to layer 13, which the accompanying CAA paper identifies as showing strong behavioral effects for 7B models. You can sweep layers such as 10, 15, 20, and 25 and compare evaluation accuracy to find the optimum.

Does this steering approach require a Hugging Face token?

Yes, the scripts read an HF_TOKEN environment variable from a .env file to download gated Llama 2 models from Hugging Face. You need approved access to meta-llama model repositories before running generation or steering.

What are the limitations of activation steering for behavior control?

Steering effects are layer- and multiplier-sensitive, and large multipliers can degrade output coherence. Vectors are model-specific, so vectors computed for Llama-2-7b-chat do not transfer to other model sizes or families.