Skip to main content

MechLens Python SDK

Record mechanistic-interpretability experiments from the Python code you already have, and compare a baseline against an intervention 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 two 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 a run

import mechlens

run = mechlens.start_run(
    project="refusal-steering",
    name="baseline",
    model={"id": "google/gemma-2-2b-it", "revision": "main"},
    question="Does steering SAE feature 4521 reduce refusals on benign prompts?",
    dataset={"id": "borderline-benign-prompts", "revision": "v1", "split": "test"},
    generation={"temperature": 0.0, "max_new_tokens": 128},
    config={"seed": 42},
)

for example in prompts:
    answer = generate(example["prompt"])
    run.log_example(
        id=example["id"],
        input=example["prompt"],
        output=answer,
        refused=is_refusal(answer),
    )

run.log_metrics({"refusal_rate": refusal_rate})
print(run.finish().url)

Then record the intervention the same way: same project, same example IDs, same model, dataset and generation settings. Use a different name and describe the change in intervention:

run = mechlens.start_run(
    project="refusal-steering",
    name="steered",
    intervention={"type": "sae_feature_steering", "feature_id": 4521, "steering_strength": -4.0},
    ...,
)

Open the intervention run's URL to see the comparison: whether the runs evaluated the same model, data, and examples; how each metric changed; and the examples whose behavior changed.

Good to know

  • Nothing is lost if an upload fails. finish() saves the run to .mechlens/runs/<run-id>.json before uploading. A failed upload raises MechLensUploadError, whose message says what went wrong. If error.retryable is true, call run.retry_upload().
  • Record the same fields in both runs. Cloud reports a field missing from either run as unknown, never as a match. Include model.revision and the dataset's id, revision, and split.
  • Configuration: MECHLENS_API_KEY (required), MECHLENS_API_URL (another Cloud deployment, such as http://localhost:5175 for local development), and MECHLENS_DIR (where runs are saved locally).

Documentation

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.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mechlens 0.1.1
File Size Uploaded Provenance
mechlens-0.1.1.tar.gz 17.4 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for mechlens 0.1.1
File Interpreter ABI Platform
mechlens-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 32.5 kB

Release files / mechlens-0.1.1.tar.gz

Download URL mechlens-0.1.1.tar.gz
Size 17.4 kB
Tags Source
SHA-256 checksum
How to use checksums
086a3c0b5467d28bc95a389b29ccd69bd987bedd0a2d7ed53a76eb69225aa546
BLAKE2b-256 checksum
How to use checksums
8fee83e6070d0aca6fa23cc666e8085b6ffbe0edfa41dbb9e62c1043e36ebb4b
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 21, 2026.

Transparency log

Release files / mechlens-0.1.1-py3-none-any.whl

Download URL mechlens-0.1.1-py3-none-any.whl
Size 15.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0afa436c30953de527b0320eb9e0c1b59f3da986aa0d3a0958707be57d9236d5
BLAKE2b-256 checksum
How to use checksums
33afe6212efc481983daf67777b0a6ff9a751ea873b93ac09580ae8c72d73be6
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 21, 2026.

Transparency log

Security for mechlens 0.1.1

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.

Select from 2 files

Attestation for mechlens-0.1.1.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 21, 2026.

Transparency log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page