versokuro/mechinterp-env
๐ฌ mechinterp-env
Finding a circuit in a transformer is like locating a faulty transistor in a running CPU using only a voltmeter. It takes human researchers weeks of careful activation patching. mechinterp-env is the training ground to automate this โ we trap an AI agent inside a toy transformer with a mathematically planted circuit, hand it the tools of mechanistic interpretability, and force it to perform neurosurgery on the model.The first RL environment where agents learn to debug transformer circuits โ built on toy models with known ground-truth circuits, so every score is provably correct.
Built for the OpenEnv AI Hackathon 2026 (Meta ร Hugging Face ร PyTorch).
What the Agent Actually Sees

Left: The circuit head (H2) concentrates all attention on the RED token โ copying it perfectly. Middle: A noise head spreads attention uniformly โ no structured information flow. Right: The ablation evidence an agent uses to identify the circuit โ ablating H2 causes a -1.0 behavioral collapse. Ablating noise heads: zero effect.
How It Works
THE SURGICAL LOOP
โโโโโโโโโโโโโโโโโ
[reset(task_id)]
โ
โผ
Agent receives:
โข target_behavior โ "This head copies a token. Find it."
โข available_actions โ ablate_head, patch_activation, query_logit_lens...
โข prerun_results โ (Task 1 only) ablation results already computed
โ
โผ
[Agent Action: ablate_head(layer=0, head=2)]
โ
โผ
Environment runs forward pass with H2 zeroed out
โ
โผ
[Observation returned]:
โข behavioral_delta: -1.0000 โ H2 is CRITICAL
โข baseline_prob: 0.9999
โข ablated_prob: 0.0000
โข interpretation: "IMPORTANT โ likely circuit component"
โ
โผ
[Reward: +0.40] โ Information gain: |delta| ร 0.3 + high-delta bonus
โ
โผ
Agent ablates more heads, finds delta โ 0.0 for all others
โ
โผ
[Agent Action: submit_hypothesis({"(0, 2)": 1.0})]
โ
โผ
[Grader: F1 against ground truth] โ score=0.999
[END] success=true steps=1 score=0.999The Three Planted Circuits
Each task uses a different transformer model with a mathematically designed circuit. Ground truth is known by construction โ we built the weights.

Task 2's two-head induction circuit: L0H1 (lookup head) concentrates attention on the signal token, writes its identity into dims 17-32. L1H2 (transcode head) reads that signal via K-composition and converts it to the target token. Ablating either head collapses the behavior entirely.
Task 1 โ Copy Head (Easy)
Task 2 โ Induction Circuit (Medium)
Task 3 โ Committee Circuit (Hard)

Ablation deltas across all three tasks. Circuit heads (red) cause catastrophic behavioral collapse when removed. Noise heads (gray) have exactly zero effect. The signal is clean โ this is what real MI research looks like.
Reward Function
Dense reward โ the agent gets signal at every step, not just at the end.
Step-level rewards
Terminal reward (on submit_hypothesis)
Calibration: Random agent โ 0.05 avg reward. Systematic agent โ 0.35. Agent understanding MI workflow โ 0.75+.
Frontier Model Baselines
Tested with Qwen/Qwen2.5-72B-Instruct via the HF inference router:
The difficulty progression is genuine. Hard tasks are actually hard.
Action Space
Grading
All graders are deterministic โ no LLM, no randomness. Same hypothesis always gets the same score.
Why Toy Transformers?
This is how mechanistic interpretability research actually bootstraps. Elhage et al. (2021), Olsson et al. (2022), Wang et al. (2022) โ all foundational MI papers start with toy models where ground truth is provable by construction.
Advantages over cached GPT-2 activations:
- โ Ground truth is mathematically exact โ not approximate
- โ No GPU, no PyTorch, no TransformerLens โ pure numpy
- โ Docker image is tiny โ no 500MB activation cache files
- โ Microsecond forward passes โ well within 20-minute budget
- โ Perfect determinism โ same input always gives same score
Real-World Utility
MI circuit discovery is bottlenecked by researcher time. A senior MI researcher spends days to weeks localizing a single circuit. An RL agent trained on this environment is a step toward automating that โ a research assistant that can run activation patching experiments, form hypotheses, and narrow down circuit components autonomously.
This environment serves as a curriculum: start with provably correct ground truth on toy models, then scale to real models with activation caches. The architecture already supports this.
The target community: Anthropic, DeepMind, EleutherAI, and every independent MI researcher would have direct use for an agent trained on this environment.
Setup
Quick start
git clone https://github.com/ChoudharyDeven/mechinterp-env
cd mechinterp-env
pip install numpy fastapi uvicorn pydantic httpx openai
python verify_tasks.py # 46/46 checks passed
uvicorn server.app:app --host 0.0.0.0 --port 7860Test it
curl http://localhost:7860/health
curl -X POST http://localhost:7860/reset \
-H "Content-Type: application/json" \
-d '{"task_id": "head-identification"}'Docker
docker build -t mechinterp-env .
docker run -p 7860:7860 mechinterp-envRun inference
export MECHINTERP_ENV_URL=https://potatochoudhary-mechinterp-env.hf.space
export HF_TOKEN=your_hf_token
export API_BASE_URL=https://router.huggingface.co/v1
export MODEL_NAME=Qwen/Qwen2.5-72B-Instruct
python inference.pyEnvironment Variables
Project Structure
mechinterp-env/
โโโ inference.py โ Baseline inference script (mandatory)
โโโ openenv.yaml โ OpenEnv manifest
โโโ models.py โ Pydantic types: Action, Observation, CircuitMask
โโโ client.py โ Python client (async + sync)
โโโ verify_tasks.py โ 46-check verification script
โโโ Dockerfile
โโโ server/
โ โโโ app.py โ FastAPI: /reset /step /state /health
โ โโโ environment.py โ Core logic: reset(), step(), action handlers
โ โโโ transformer.py โ Numpy transformer engine
โ โโโ tasks.py โ Task definitions + ground truth circuits
โ โโโ graders.py โ Deterministic graders (no LLM)
โ โโโ rewards.py โ Dense reward function
โโโ data/
โโโ models/
โ โโโ model_1layer.py โ 1L copy head (d=64)
โ โโโ model_2layer.py โ 2L induction circuit (d=64)
โ โโโ model_4layer.py โ 4L committee circuit (d=64)
โโโ circuits/
โโโ circuit_1.json โ {"(0, 2)": 1.0}
โโโ circuit_2.json โ {"(0, 1)": 0.6, "(1, 2)": 1.0}
โโโ circuit_3.json โ 5-head ground truthOpenEnv Spec Compliance
- โ
Typed
Action,ObservationPydantic models - โ
step(action)โ(observation, reward, done, info) - โ
reset()โ initial observation - โ
state()โ current state - โ
openenv.yamlwith 3 tasks and grader definitions - โ
Passes
openenv validate - โ Docker build clean, port 7860
- โ Phase 1 + Phase 2 passed
References
- Elhage et al. (2021) โ A Mathematical Framework for Transformer Circuits
- Olsson et al. (2022) โ In-context Learning and Induction Heads
- Wang et al. (2022) โ Interpretability in the Wild: a Circuit for Indirect Object Identification in GPT-2 small
- Conmy et al. (2023) โ Towards Automated Circuit Discovery for Mechanistic Interpretability
License
MIT
