MechLens Python SDK
Record mechanistic-interpretability experiments from the Python code you already have, and compare a reference run against interventions and controls in MechLens Cloud.
Your code still loads the model, runs the intervention, and calculates the metrics. The SDK records what you ran so Cloud can check the runs are comparable, show which examples changed, and help you publish a finding with its evidence and limitations attached.
It depends only on httpx and python-dotenv: no Torch, TransformerLens, or
SAE libraries.
Install
pip install mechlens
Requires Python 3.10 or later.
Connect to your workspace
In MechLens Cloud, open Experiments → New experiment and generate an API key, or create one under Account → API keys.
export MECHLENS_API_KEY="ml_your_key_here"
Record an experiment
Declare the experiment once, then record a reference run and the interventions and controls compared with it, over the same examples:
import mechlens
exp = mechlens.experiment(
question="Does steering SAE feature 4521 reduce refusals on benign prompts?",
claim="Steering feature 4521 at -4.0 lowers the refusal rate; a random feature does not.",
deviations=["Refusals scored by substring matching"],
model={"id": "google/gemma-2-2b-it", "revision": "main"},
dataset={"id": "borderline-benign-prompts", "revision": "v1", "split": "test"},
generation={"temperature": 0.0, "max_new_tokens": 128},
metric_definitions={"refusal_rate": {"description": "Share of prompts refused.", "example_field": "refused"}},
config={"seed": 42},
)
def record(run, answer_fn):
refusals = 0
for example in prompts:
answer = answer_fn(example["prompt"])
refused = is_refusal(answer)
refusals += refused
run.log_example(id=example["id"], input=example["prompt"], output=answer, refused=refused)
run.log_metrics({"refusal_rate": refusals / len(prompts)})
with exp.reference() as run:
record(run, generate)
with exp.intervention(
"steer-4521",
intervention={"type": "sae_feature_steering", "feature_id": 4521, "steering_strength": -4.0},
expect={"refusal_rate": "decrease"},
compare_to=["steer-random-feature"],
) as run:
record(run, steered_generate)
with exp.control(
"steer-random-feature",
intervention={"type": "sae_feature_steering", "feature_id": "random", "steering_strength": -4.0},
expect={"refusal_rate": "no_effect"},
) as run:
record(run, random_steered_generate)
print(exp.url)
Open exp.url to see the experiment: every condition against the reference,
with checks that they evaluated the same model, data and examples, each
metric's difference with a confidence interval, and what you expected beside
what happened. A draft finding is built from your declaration: the claim, the
protocol, and candidate limitations. You write the conclusion.
- Sweeps: pass
series="steering"andlevel=-4.0instead of a name, and Cloud plots the series as a curve. - Separate jobs: each condition can run in its own process. Every process that declares the same experiment joins it.
- Reproducing a paper? Add
reproduces=mechlens.Paper(arxiv="2406.11717", reported={"refusal_rate": {"reference": 0.95, "ablate-direction": 0.01}}), keyed by your condition names, and Cloud shows the paper's numbers beside yours.
mechlens.start_run() still records single runs with every argument given
explicitly.
Good to know
- Nothing is lost if an upload fails.
finish()saves the run to.mechlens/runs/<run-id>.jsonbefore uploading. A failed upload raisesMechLensUploadError, whose message says what went wrong. Iferror.retryableis true, callrun.retry_upload(). - Record the same fields in every condition. Cloud reports a field missing
from either side of a comparison as unknown, never as a match. Include
model.revisionand the dataset'sid,revision, andsplit. - Configuration:
MECHLENS_API_KEY(required),MECHLENS_API_URL(another Cloud deployment, such ashttp://localhost:5175for local development), andMECHLENS_DIR(where runs are saved locally).
Documentation
Full docs: https://blaketylerfullerton.github.io/mechlens-sdk/
Using a coding agent? Point it at llms-full.txt, every
page as one Markdown file.
Development
git clone https://github.com/blaketylerfullerton/mechlens-sdk.git
cd mechlens-sdk
python3 -m venv .venv
source .venv/bin/activate
pip install -e '.[test,docs]'
pytest
Demo without a model. examples/steering_demo.py uploads a synthetic
baseline and steered run, to check your key and connection. Put
MECHLENS_API_KEY (and optionally MECHLENS_API_URL) in examples/.env, then
run python examples/steering_demo.py. Its outputs are marked [synthetic].
Docs. The docs in docs/ build with Material for MkDocs, and the API
reference is generated from the docstrings in src/mechlens. Run
mkdocs serve to preview at http://127.0.0.1:8000; mkdocs build is strict,
so broken links fail.
Releasing. Bump __version__ in src/mechlens/_version.py, commit, and
push a matching tag (v0.1.1). The release workflow tests, builds, and
publishes to PyPI with Trusted Publishing.
Cloud contract. The SDK sends POST /api/sdk/runs with
Authorization: Bearer <key> and an Idempotency-Key equal to the run ID.
Cloud returns {"run_id": "...", "url": "..."}.
License
Apache-2.0
Metadata
Release files for mechlens 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| mechlens-0.3.0.tar.gz | 26.1 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mechlens-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 47.7 kB
Release files / mechlens-0.3.0.tar.gz
| Download URL | mechlens-0.3.0.tar.gz |
|---|---|
| Size | 26.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3e0bf7e716403ffc0b92dedf81e720f512652bdf71406b9f05b1d655619d898d
|
|
BLAKE2b-256 checksum How to use checksums |
5a12765c584b8b2090725406f4f16de9836bfd380b7ef5218a27f67a5e5b8233
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.
Transparency logRelease files / mechlens-0.3.0-py3-none-any.whl
| Download URL | mechlens-0.3.0-py3-none-any.whl |
|---|---|
| Size | 21.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1e82bd47dea21d9628e31da50843ddd7768109352a0bbfa325fa05cea0225d46
|
|
BLAKE2b-256 checksum How to use checksums |
e229e74dc7afcaccf109d0b63f1af52784c561e70c125c2adc7117d48a09fca2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.
Transparency logSecurity for mechlens 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for mechlens-0.3.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.