Steering Code LLMs with Activation Directions for Language and Library Control

Md Mahbubur Rahman, Arjun Guha, Harshitha MenonarXiv 2026

TL;DR

Code Large Language Models (LLMs) possess strong implicit biases toward popular programming languages and libraries (like Python or PyTorch) when given neutral prompts. This paper demonstrates that these stylistic preferences are encoded as approximately linear directions in the model's activation space and can be manipulated at inference time using a simple vector addition—even overriding explicit textual instructions to do the opposite.

What Problem Does This Paper Address?

If you ask a standard code LLM to "implement a matrix multiplication function" without specifying a language, it will almost certainly write it in Python using NumPy. This implicit bias toward dominant ecosystems is helpful for the average user, but it becomes a major hurdle when developers or downstream systems specifically require less common alternatives (like C++, TensorFlow, or CuPy).

While prompt engineering can explicitly request a specific stack, the authors of this paper look under the hood to ask a more fundamental, mechanistic question: Are these language and library preferences represented as compact, steerable structures within the model's internal activation space? If so, can we bypass prompt modifications entirely and control the model's behavioral defaults directly at inference time?

Methods

The authors employ a lightweight, inference-time intervention technique known as activation steering:

  1. Layer-wise Direction Estimation: Using a simple difference-in-means procedure, they construct two matched prompt sets for a given language/library pair (e.g., $P^+$ for PyTorch and $P^-$ for TensorFlow). They pass these prompts through the model and average the hidden state activations of the final $K$ tokens at each layer $\ell$. The steering vector $v^{(\ell)}$ is calculated as the difference between the mean activation of the positive and negative sets ($\mu_+^{(\ell)} - \mu_-^{(\ell)}$).
  2. Model Intervention: During generation with a neutral prompt, they inject this directional vector into a specific layer $\ell$ by adding it to the hidden states: $$\tilde{h}_t^{(\ell)} = h_t^{(\ell)} + \alpha v^{(\ell)}$$ where $\alpha \ge 0$ is a hyperparameter controlling the intervention strength.

  3. Best-Direction Selection: They use a validation split to evaluate each layer individually, selecting the optimal layer ($\ell^*$) that maximizes successful generation toward the target ecosystem.

Datasets

The approach was evaluated across five distinct code-generation discrimination tasks:

  • PyTorch vs. TensorFlow
  • Python vs. C++
  • STL vs. Boost
  • Matplotlib vs. Seaborn
  • NumPy vs. CuPy

For four of these tasks, the authors used ChatGPT to synthesize 100 prompts per condition (target, opposite, and neutral). For the Python–C++ task, they drew 162 prompts from the standard MultiPL-E benchmark, stripping away language-revealing scaffolding (like function signatures) to ensure the prompts were completely neutral. All datasets were split 50/50 into validation and test sets.

Key Insights and Ideas

  • Neutral Prompts Aren't Neutral: Baseline evaluations confirm that models heavily favor dominant ecosystems (Python, NumPy, Matplotlib) when given no explicit direction.
  • Linear Separability of Code Style: The success of the difference-in-means vector proves that high-level programming preferences are represented linearly within the transformer's residual stream.
  • The Asymmetry of Rarity: Dominant, highly frequent ecosystems are significantly easier to induce via steering than rarer alternatives (e.g., steering a model toward Python is easier than steering it toward C++).
  • Overriding Textual Prompts: Internal activation steering can successfully compete with—and often completely override—explicit instructions written in the prompt (e.g., forcing the model to write PyTorch code even when the prompt explicitly asks for TensorFlow).

Key Results

The authors tested their method across three open-weight models: CodeGemma-7B, Qwen2.5-Coder-7B, and Llama3.1-8B.

  • Neutral Prompt Steering: Adding the steering vector dramatically shifted the output distributions. For instance, under neutral prompts, CodeGemma-7B achieved near-complete control, shifting generations from nearly 0% to almost 100% for target ecosystems across all tasks.
Figure 1: CodeGemma-7B: Output distribution across language/library pairs under neutral prompts, with and without directional-vector interventions.
  • Conflicting Prompt Steering: When the steering vector was applied against prompts explicitly requesting the opposite ecosystem, both Qwen2.5-Coder-7B and CodeGemma-7B demonstrated powerful override capabilities, particularly for major library pairs.
Figure 6: Qwen2.5-Coder-7B: Output distribution when directional-vector interventions are applied against prompts that request the opposite language or library.
  • The Quality Trade-off: Increasing the intervention strength ($\alpha$) predictably increases compliance with the target direction. However, there is a strict upper bound: setting $\alpha$ too high introduces severe code degradation, causing the model to output highly repetitive or incomplete code.
Figure 4: Effect of intervention strength α on steering performance in the Matplotlib–Seaborn task. Larger α increases target-direction generation, but excessive strength leads to repetitive or incomplete outputs.

Limitations and Future Work

  • Scope of Models and Tasks: The study is restricted to three open-weight models and five specific language/library pairs; it remains to be seen if these linear structures hold true for closed-source models or broader architectural styles.
  • Synthetic Prompt Reliance: Most evaluation prompts were synthetically generated, which might not fully capture the complexity or messiness of real-world user queries.
  • Simplistic Steering Mechanism: The difference-in-means method targets a single layer and may miss more distributed, non-linear representations of code style across the network.
  • Code Correctness: The alignment was verified using an LLM judge (Qwen3-32B) to check for the presence of the target library, but this does not guarantee that the generated code is functionally correct or executable.

Summary updated

BibTeX
@misc{rahman2026steering,
  author = {Md Mahbubur Rahman and Arjun Guha and Harshitha Menon},
  title = {{Steering Code LLMs with Activation Directions for Language and Library Control}},
  year = {2026},
  eprint = {2603.23629},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2603.23629},
}
Figure