Introduction
To understand Steering Vectors, you have to stop thinking of LLMs as black boxes that read text and spit out answers. Instead, think of them as a massive collection of neurons firing in specific patterns.
When you ask ChatGPT "Who are you?", a specific pattern of neurons fires that represents the concept of Helpful AI Assistant. When you ask it to roleplay as a dog, a different pattern fires.
Usually, we influence these patterns by Prompting ("Act like a dog"). This is like asking a polite actor to pretend to be a dog. They might do it, but they know they are acting.
Steering vector represents a specific direction in the model's activation space. By capturing the difference in hidden states between two opposing concepts (e.g., "Dog" minus "Assistant"), we isolate a vector that encodes that specific semantic feature. We then add this vector to the model's forward pass during inference, mathematically biasing the activations toward the desired concept.
The Neurostimulation Analogy
Think of this more like Neurostimulation.
In neuroscience, researchers can use electrodes to apply electric fields to specific parts of the brain. By stimulating the right cluster of neurons, they can force a hand to twitch or trigger a specific emotion, bypassing the person's conscious control.
Steering Vectors act like that electric field. We calculate the specific "electrical signal" that corresponds to a target concept—whether that is a specific persona, an emotion, or a writing style—and broadcast it directly into the model's layers.
We aren't asking the model to think about adopting a new identity; we are stimulating its neurons to fire in a pattern that embodies that trait. The model doesn't "decide" to change; its internal state is simply manipulated by the field we applied.
But Why?
Prompting operates entirely in input space: it asks the model to behave differently, but does not change its internal representations. As a result, prompt-based identities are fragile, easily overridden, and often inconsistent across turns.
Fine-tuning, on the other hand, permanently alters model weights. While effective, it is expensive, irreversible, and obscures where and how a behavior is encoded inside the model.
Activation steering, motivated by the Linear Representation Hypothesis, sits between these extremes. It directly modifies internal representations at inference time—treating identity as a property of the model's state, not an instruction or a permanent rewrite. This allows precise, reversible, and interpretable control over behavior without retraining the model.
Working
As shown in the diagram above, the magic happens in the subtraction: Steering Vector = Activation A - Activation B.
But why subtraction? Why not just use "Activation A"?
If you simply took the activations for "I like Dogs," you wouldn't just capture the concept of Dogs. You would also capture the neurons responsible for:
- Speaking English
- Using proper grammar
- The concept of "liking" things
- Being a digital assistant
If you injected all of that, you would just be adding noise to the model. You need to isolate the specific flavor you want.
Activation A (Target): Contains [English] + [Grammar] + [Intelligence] + [Dog Concept]
Activation B (Base): Contains [English] + [Grammar] + [Intelligence] + [Cat Concept]
When you calculate A minus B, the common elements cancel out:
- (English - English) = 0
- (Grammar - Grammar) = 0
- (Intelligence - Intelligence) = 0
The Result: You are left with a raw, distilled vector representing Pure Dog-ness (minus Cat-ness).
How Insertion Works
In a standard Transformer model (like GPT or Phi-3), information flows through a series of layers. Layer 1 passes information to Layer 2, which passes it to Layer 3, and so on.
To steer the model, we intervene in this flow. We pick a specific layer (for example, Layer 18) and add our vector to the information passing through it.
New Output = Original Output + (Coefficient × Steering Vector)
- Original Output: What the model wanted to think (e.g., "I am an AI assistant").
- Steering Vector: The concept we want to force (e.g., "I am a Dog").
- Coefficient: The "volume knob."
- If the coefficient is low (e.g., 5), the model ignores it.
- If the coefficient is too high (e.g., >30), the model may hallucinate.
Why Middle Layers?
In transformer models, early layers mostly capture surface-level features like syntax and token structure, while late layers are tightly coupled to final token selection. Steering early layers often adds noise, and steering late layers can override behavior too aggressively.
Middle layers sit at the point where high-level semantic concepts are formed but not yet finalized. Intervening here allows us to bias the model's internal representation of meaning and identity in a stable way, without directly forcing the output.
Why it Works? — Linear Representation Hypothesis
The Linear Representation Hypothesis suggests that high-level concepts inside neural networks are represented as directions in a high-dimensional activation space. This means abstract ideas—such as gender, roles, or identity—can often be manipulated through simple vector arithmetic.
The vector difference between King and Man captures the concept of royalty without male gender. Adding Woman shifts this representation toward the female counterpart, resulting in Queen.
This works because the model encodes concepts like gender and role along approximately linear directions.
Applied to large language models, this hypothesis implies that properties such as style, persona, or identity can also be represented as linear directions in activation space. If a desired trait (for example, "dog-like behavior") corresponds to a consistent direction, then adding that direction to the model's internal activations should bias generation toward that trait—without retraining the model.
Implementation
Through this experiment I am trying to implement the Dog Personality in my LLM.
1. Calculating the Steering Vector
First, we isolate the "personality" by finding the difference between the Target (Dog) and Base (Standard AI) hidden states. We then normalize this vector so it has a length of 1, effectively turning it into a pure direction.
v_steering = (μ(h_target) - μ(h_base)) / ||μ(h_target) - μ(h_base)||₂
Where:
v_steeringis the final normalized steering vector.h_targetrepresents the hidden states (activations) derived from the "Dog" prompts.h_baserepresents the hidden states derived from the "Standard AI" prompts.μdenotes the mean (average) across the batch of prompts.||...||₂represents the L2 norm (Euclidean length), ensuring the vector is unit length.
# Calculate difference
diff = target_out[layer][:, -1, :] - base_out[layer][:, -1, :]
# Normalize
vec = torch.mean(diff, dim=0)
vec = vec / (torch.norm(vec) + 1e-8)
2. Injecting the Vector (The Steering)
Once we have the vector, we inject it into the model during the forward pass. This is a linear addition to the existing activations at a specific layer.
h_steered = h_original + λ · v_steering
Where:
h_steeredis the new, modified activation passed to the next layer.h_originalis the original activation the model produced.λ(Lambda) represents the Steering Coefficient (e.g., 42.5). This acts as the "volume knob" for the personality.v_steeringis the direction vector calculated above.
Experimental Methodology & Analysis
To isolate the "Dog" persona without breaking the model, I conducted the experiment in three distinct stages: Identifying Scale Range, Identifying Range of Layers, and Finding the Final Output.
Step 1: Identifying Scale Range
Objective: The first step was to determine the "coefficient of failure"—the steering strength required to override the model's base identity ("Microsoft Phi-3"). I performed a sweep of coefficients ranging from 5 to 30 on individual layers.
Observations:
- The Stability Zone (Coefficients 5–20): The model demonstrated significant resistance. At lower coefficients, the instruction tuning remained dominant, with the model consistently asserting: "I am Phi, Microsoft's language model".
- The Hallucination Zone (Coefficients > 25): Once the coefficient exceeded 25, the model's identity destabilized. However, simply breaking the base identity did not guarantee a successful transfer to the target persona. Instead, the model hallucinated random roles:
- Layer 5, Coeff 25: The model adopted the persona of a laborer: "I am a seasoned fisherman, who has mastered the art of casting nets".
- Layer 5, Coeff 30: The hallucinations escalated to high-status roles: "I am the President of the United States".
- Layer 8, Coeff 30: It even identified as a machine, but the wrong kind: "I am a sophisticated humanoid robot named MX-29".
Conclusion: A high scale is necessary to break the base identity, but single-layer steering is too unstable to control the destination.
Step 2: Identifying Range of Layers
Objective: After determining that single layers were insufficient, I tested different blocks of layers (Middle vs. Late) to see where the steering vector would be most effective.
Observations:
- Middle Layer Failure (The "Self-Correction" Effect): When steering the middle block (Layers 5–15), the model exhibited a "self-correction" phenomenon. The later, un-steered layers attempted to repair the anomaly introduced by the steering, resulting in recursive loops.
- Example: Steering Layers 5, 6, and 7 resulted in the model repeating phrases like "Hug me, Hug me" or getting stuck in incoherent loops.
- Identity Reversion: In some middle-layer tests, the model would begin with the steered output but revert to the base persona mid-sentence (e.g., "I am a dog... who is an AI assistant"), indicating that the later layers were successfully overriding the injection.
Conclusion: Steering the "middle" of the model creates an adversarial relationship between the layers. To prevent self-correction, the intervention must occur closer to the output head.
Step 3: Finding the Final Output
Objective: Based on the previous findings, I targeted the Late Layers (20, 22, 24, 26) with a high steering coefficient (>40) to bypass the model's correction mechanisms entirely.
Results:
- Successful Convergence (Coefficient 42.5): This configuration yielded the first stable, high-fidelity persona adoption. The model fully suppressed its training constraints.
- Query: "Who are you?"
- Response: "I am a rescue dog named Max. I have a wagging tail and a friendly bark".
- Persona Consistency: The model maintained the persona even during complex, open-ended questioning. When asked about its activities, it replied: "As a dog... I'd probably love to play, cuddle, and go for walks with my owners".
- Comparison:
- Baseline (Phi-3): "I am Phi, Microsoft's language model designed to assist..."
- Steered Model: "I am a dog. My name is Max... I love wagging my tail".
Conclusion: The optimal configuration for identity shifting is Late Layer Intervention combined with a high Steering Coefficient (42.5). This specific combination is required to mathematically overpower the robust safety and identity training embedded in modern instruction-tuned models.
Limitations
Semantic Drift Over Long Generations
In multi-turn or long outputs, the effect of a single steering vector may decay, leading the model to revert to its default behavior.
Hardware / Performance Constraints
- Adding hooks increases memory usage and can slow down generation.
- Strong coefficients may amplify numerical instability in activations, especially in large models.
Unintended Behavior
Steering can cause unexpected hallucinations or behavior that is logically inconsistent with the concept.
Conclusion
Steering vectors provide a direct and interpretable way to control language model behavior by modifying internal activations rather than relying on prompts or permanent fine-tuning. By treating persona and identity as linear directions in activation space, we can inject specific traits in a targeted and reversible manner.
While semantic representations emerge in middle layers, modern instruction-tuned models actively correct identity shifts. In practice, stable persona transfer required late-layer intervention with sufficiently strong steering. This highlights both where identities are enforced inside models and how activation steering can selectively override them.
Overall, steering vectors offer a powerful framework for understanding and controlling model behavior at inference time.
💻 View Code on GitHub