Skip to main content

MechLens Python SDK

The lightweight client for recording an experiment in existing Python code and sending it to MechLens Cloud. Model execution and metric calculation stay in the researcher's code.

This package is intentionally separate from the GPU application and has no Torch, TransformerLens, or SAE dependencies.

Try the complete upload flow

The included demo sends a synthetic baseline and steered run with the same four prompt IDs. It exercises local saving, API-key authentication, validation, and Cloud ingestion without downloading a model.

Create an API key from MechLens → Account → API keys. Put it in examples/.env (the demo loads this file automatically):

MECHLENS_API_KEY="ml_your_key_here"
# Omit this line to upload to MechLens Cloud. For a local Cloud:
MECHLENS_API_URL="http://localhost:5175"

Then run:

cd /path/to/mechlens-sdk
python3 -m venv .venv
source .venv/bin/activate
pip install -e .
python examples/steering_demo.py

Variables exported in your shell take priority over values in examples/.env.

The script prints both saved-run URLs and keeps local JSON copies under .mechlens/runs/. Its outputs are clearly marked synthetic and are only for testing the integration.

Use it in an experiment

import mechlens

run = mechlens.start_run(
    project="refusal-steering",
    name="strength-2",
    model={"id": "google/gemma-2-2b", "revision": "exact-revision"},
    config={"feature_id": 1234, "steering_strength": 2.0, "seed": 42},
    question="Does steering this feature reduce refusals?",
    dataset={"id": "refusal-prompts", "revision": "v1"},
)

for example in dataset:
    answer = generate_with_steering(example["prompt"], strength=2.0)
    run.log_example(id=example["id"], input=example["prompt"], output=answer)

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

Set MECHLENS_API_KEY to the account key. Runs upload to MechLens Cloud (https://mechlens-cloud-fgstc.ondigitalocean.app); set MECHLENS_API_URL for another deployment, such as http://localhost:5175 during local development. Finished runs are written atomically to .mechlens/runs/<run-id>.json before upload. Set MECHLENS_DIR to choose another local directory.

Local development

python3 -m venv .venv
source .venv/bin/activate
pip install -e '.[test]'
pytest

Documentation

The user docs live in docs/ and build with Material for MkDocs. The API reference is generated from the docstrings in src/mechlens, so update those when the public API changes.

pip install -e '.[docs]'
mkdocs serve          # preview at http://127.0.0.1:8000
mkdocs build          # static site in site/ (strict: broken links fail)

Cloud contract

The SDK sends POST /api/sdk/runs with an Authorization: Bearer ... header and an Idempotency-Key equal to the run ID. Cloud returns:

{"run_id": "...", "url": "https://.../app/projects/.../runs/..."}

If an upload fails, finish() preserves the completed local run and raises MechLensUploadError. Call run.retry_upload() to retry during the same process.

Metadata

Release files for mechlens 0.1.0

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.0
File Size Uploaded Provenance
mechlens-0.1.0.tar.gz 16.3 kB With provenance Details

Built distribution (wheel)

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

Total release size: 30.7 kB

Release files / mechlens-0.1.0.tar.gz

Download URL mechlens-0.1.0.tar.gz
Size 16.3 kB
Tags Source
SHA-256 checksum
How to use checksums
e63f4dde076da1c29b800859ed7d3aa90bfdfb6dc6a48c65f364c1b945a5a8a1
BLAKE2b-256 checksum
How to use checksums
8448cd403401e315c0560b138f0342ec74dcf1ed1037da0174a9ddbbd1322227
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.0-py3-none-any.whl

Download URL mechlens-0.1.0-py3-none-any.whl
Size 14.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cb03a871ef48481c7833d3949170c385fb8f5c5ba188d7f3be9d31a1a897e097
BLAKE2b-256 checksum
How to use checksums
05c168d2b0a248ab8844afed7a294d12626afd9f83d96eb591fceaf409089f96
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.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.

Select from 2 files

Attestation for mechlens-0.1.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 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