Core Design Principle: The book stays in the middle. The software adds quiet margin prompts and between-section instruments that make the reader do the thinking—and AI-generated content appears only after the reader's own attempt, as a comparison. There is no chat-with-your-PDF box as the main interface, and summaries are never auto-shown on open.
- Node.js (v18+ / v20+) and npm
- Python 3.11+ (with
fastapi,uvicorn,pypdf, andpytestfor the local SQLite & PDF ingestion sidecar)
npm installMargin supports both Google Gemini (recommended for fast, cost-effective inference with a generous free tier) and Anthropic Claude.
Open the app, click Settings (⚙) in the reader bar, go to AI Inference & API Keys, and paste your Gemini API key (get one free at Google AI Studio). Your key is saved locally in your browser and used securely.
Set your key before starting the sidecar:
# For Google Gemini (default: gemini-2.5-flash, configurable via MARGIN_GEMINI_MODEL):
export GEMINI_API_KEY="AIzaSy..."
# Or for Anthropic Claude (default: claude-sonnet-4-20250514, configurable via MARGIN_CLAUDE_MODEL):
export ANTHROPIC_API_KEY="sk-ant-..."- Privacy & Security Rules:
- API keys are read from environment variables or browser storage and are never logged or written to book files.
- Only the active section excerpt (capped to 12,000 characters) is sent to the LLM—never the whole book at once.
- Every prompt sent to the LLM is recorded in SQLite and visible in the "What the AI Was Asked" transparency drawer in the top bar.
# Run both the Vite frontend (http://localhost:5173) and FastAPI/SQLite sidecar (http://127.0.0.1:8765):
npm start
# Or run them separately:
npm run dev # Vite React/TS reader UI on http://localhost:5173
npm run sidecar # Python FastAPI + SQLite sidecar on http://127.0.0.1:8765Margin can be run and packaged as a standalone native desktop application using Electron:
# Run the desktop app in development:
npm run electron:dev
# Package a standalone desktop bundle for your current OS:
npm run electron:build
# Or build platform-specific packages (outputs to release/):
npm run electron:pack:mac # macOS (.dmg and .zip)
npm run electron:pack:win # Windows installer (.exe / NSIS and .zip)
npm run electron:pack:linux # Linux (.AppImage and .deb)
npm run electron:pack:all # All platformsNote on macOS Gatekeeper: If you build or download Margin without an Apple Developer ID certificate, macOS attaches a quarantine flag to downloaded files. To open Margin if macOS prompts that it is from an unidentified developer:
- Either right-click (or Control-click)
Margin.appand choose Open, then confirm Open;- Or remove the quarantine attribute via Terminal:
xattr -cr /Applications/Margin.app # or if running from Downloads: xattr -cr ~/Downloads/Margin.app
- Click
Open PDF…in the top reader bar to select any PDF on your computer, or POST toPOST /api/ingest-pdfon the local sidecar. - The ingestion pipeline extracts the Table of Contents, section boundaries, paragraphs, LaTeX equations (
EquationItem), tables (TableItem), and worked numeric examples (WorkedExampleItem), storing per-book state inlocalStorageandsidecar/margin_store.sqlite3.
# Run both TypeScript (Vitest) and Python (pytest) test suites:
npm run test:all- Ingestion Parser (
tests/margin.test.ts&tests/python/test_sidecar.py): Verifies TOC, section boundary, LaTeX equation, table, and worked-example extraction on the CC-BY OpenStax Principles of Economics fixture. - Explorable Model Verification (
src/lib/labVerifier.ts):- Evaluates formulas safely via
math.js(nevereval). - Runs an independent 2,000-point numerical grid optimization certificate check.
- Confirms that a deliberately wrong formula fails verification and blocks the lab UI with
"Couldn't verify this model"and the exact error reason.
- Evaluates formulas safely via
- Spaced Revisit Scheduler (
src/lib/revisitScheduler.ts): Verifies the1, 3, 7, 21day progression and cross-chapter section interleaving.
/— Focus search across equations and section textb— Toggle Table of Contents sidebard— Toggle Light / Dark reader themeh— Highlight currently selected text, or toggle the Highlight Tool on/off (selecting text always auto-copies to clipboard)[/]— Previous / Next section in Reader Text View
- Books stay on your machine: Copyrighted PDFs are ignored via
.gitignoreand never committed. - Reader-only Exports: Clicking
Export My Workexports Markdown + JSON containing only what you authored (pre-question guesses, free-recall attempts, concept maps, slider predictions, margin notes, and highlight page anchors)—never the book's text. - See
docs/learning-design.mdfor the full research mapping citing Dunlosky et al. (2013), Karpicke & Blunt (2011), Fiorella & Mayer (2015), and Sinha & Kapur (2021).