Skip to main content

TrainLens

Turn AI training runs into research-grade notebook reports.

TrainLens is a Jupyter-first support package for AI model training and research workflows. Its main value is that you can turn the training state already present in your notebook into structured analysis without leaving Jupyter, opening a separate chat, or copying experiment context by hand.

It redacts likely secrets and asks an OpenAI-compatible LLM to draft:

  • a scientific paper-style training report
  • an evidence-backed improvement plan

It is useful when you run many experiments and want consistent explanations of metrics, datasets, hyperparameters, limitations, and next steps directly inside the notebook where the work is happening.

CI TrainLens 0.4.0 Python License: Apache-2.0

Actively maintained by Eduardo J. Barrios.
For issues or improvements, open a pull request or email edujbarrios@outlook.com.

Quickstart

Install TrainLens:

pip install trainlens

Configure an OpenAI-compatible LLM provider:

import os

os.environ["TRAINLENS_LLM_BASE_URL"] = "https://api.openai.com/v1"
os.environ["TRAINLENS_LLM_API_KEY"] = "your-api-key"
os.environ["TRAINLENS_LLM_MODEL"] = "gpt-5.4-mini"

Keep useful experiment state in ordinary notebook variables:

dataset_name = "synthetic_cpu_binary_classification"
dataset_notes = "240 synthetic 2D samples; balanced validation split."
model_name = "pure-python-logistic-regression"
training_params = {"epochs": 30, "learning_rate": 0.45}

history = {
    "train_loss": [0.6095, 0.5005, 0.2459],
    "eval_loss": [0.6164, 0.5160, 0.2808],
    "accuracy": [0.8889, 0.9056, 0.9222],
    "val_accuracy": [0.8167, 0.8333, 0.9167],
}

Run the notebook magics:

%load_ext trainlens.magic.extension

# Scientific paper-style training report
%explain_training

# Evidence-backed experiment plan
%suggest_improvements
Magic Output
%explain_training Paper-style report with results, discussion, limitations, and LLM provenance.
%suggest_improvements Follow-up experiment plan grounded in the same notebook evidence.

Local Models

TrainLens can avoid external API token costs by using a local OpenAI-compatible server. Example with Ollama:

ollama pull llama3.1
ollama serve
import os

os.environ["TRAINLENS_LLM_BASE_URL"] = "http://localhost:11434/v1"
os.environ["TRAINLENS_LLM_API_KEY"] = "ollama"
os.environ["TRAINLENS_LLM_MODEL"] = "llama3.1"

The same pattern works with LM Studio, vLLM, llama.cpp server, and similar local servers.

Python API

from trainlens import build_improvement_ideas, build_paper_report, write_report

paper = build_paper_report()
ideas = build_improvement_ideas()

write_report(paper, "trainlens-report.md")
write_report(paper, "trainlens-report.html")
write_report(paper, "trainlens-report.json")

Inside Jupyter, helpers read the active notebook namespace automatically. Outside IPython, pass an explicit dictionary-like namespace.

Report Export

TrainLens reports can be exported as Markdown, HTML, JSON, and optionally PDF:

from trainlens import render_report, write_report

markdown = render_report(paper, format="markdown")
html = render_report(paper, format="html")
json_payload = render_report(paper, format="json")

write_report(paper, "report.md")
write_report(paper, "report.html")
write_report(paper, "report.json")

PDF export uses an optional dependency:

pip install "trainlens[pdf]"
write_report(paper, "report.pdf")

Token Usage

Costs depend on provider, model, tokenizer, prompt size, and output length. These are rough estimates, not billing records.

Call Input Tokens Output Tokens Total
%explain_training paper report 1K-2K 2.5K-3.5K 3.5K-5.5K
%suggest_improvements plan 1K-2K 2K-3K 3K-5K
Both calls 2K-4K 4.5K-6.5K 6.5K-10.5K

Approximate USD cost for a 50% input / 50% output token mix, using public prices checked on 2026-07-07:

Provider / model 1K tokens 100K tokens 1M tokens 10M tokens
OpenAI gpt-5.4-mini $0.0026 $0.2625 $2.6250 $26.2500
Anthropic Claude Haiku 4.5 $0.0030 $0.3000 $3.0000 $30.0000
Google Gemini 2.5 Flash-Lite $0.0003 $0.0250 $0.2500 $2.5000
Local Ollama / LM Studio $0 API cost $0 API cost $0 API cost $0 API cost

Cost formula:

\text{estimated cost} =
\frac{
\text{input tokens} \times \text{input price per 1M}
+ \text{output tokens} \times \text{output price per 1M}
}{1{,}000{,}000}

Privacy

TrainLens summarizes visible notebook state, redacts likely secrets, truncates large literals, and sends only the resulting context to the configured endpoint. Do not store real API keys in notebooks or committed files.

License

Apache License 2.0. See LICENSE.

Release files for trainlens 0.4.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 trainlens 0.4.0
File Size Uploaded
trainlens-0.4.0.tar.gz 40.4 kB Details

Built distribution (wheel)

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

Total release size: 83.3 kB

Release files / trainlens-0.4.0.tar.gz

Download URL trainlens-0.4.0.tar.gz
Size 40.4 kB
Tags Source
SHA-256 checksum
How to use checksums
493a62efee1e00985d6af926638c4610939beb35c64a6104eb906b9a90f903a1
BLAKE2b-256 checksum
How to use checksums
5b7a3910ee8029376eb77e2f8972acf4174c003c97d76144417b14ef3118bbe5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.7

Release files / trainlens-0.4.0-py3-none-any.whl

Download URL trainlens-0.4.0-py3-none-any.whl
Size 42.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
358c804948b3ca0a07871ff9514d5e90447818d727bc44e432fa18000fd39507
BLAKE2b-256 checksum
How to use checksums
fd6955155e0297858997f30e195e518ae5043c7046239853388f29b7c66f96ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.7

Release history Release notifications | RSS feed

0.9.0

2 release files

0.8.4

2 release files

0.8.3

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

This release

0.4.0 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

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