Run bulk LLM workloads asynchronously at a 50% discount with zero rate-limit anxiety, beautiful KaTeX math rendering, and Docker / TrueNAS Scale simplicity.
Important
100% of the code, backend architecture, frontend interfaces, unit test suites, and Docker containerization in this repository were designed, generated, and verified entirely by Gemini Antigravity (Google DeepMind's Advanced Agentic Coding assistant).
The human author, HoldenStill (@HoldenStill), provided the initial concept, core requirements, homelab infrastructure needs, iterative feature directions (such as electrical engineering use cases, scroll fatigue solutions, KaTeX LaTeX math support, and cost transparency), and human evaluation. Every line of Python, TypeScript, SQL, Tailwind CSS, shell scripts, and documentation was implemented and tested by the autonomous AI assistant.
When sending dozens or hundreds of prompts to top-tier LLMs (like the latest from Claude, GPT, DeepSeek, or Gemini), real-time API requests have major drawbacks:
- You pay 100% full price for synchronous inference.
- You risk rate limits (429), gateway timeouts (504), and dropped browser tabs.
- You must write custom scripts or inspect raw JSON files to gather results.
Somnia is a self-hosted, single-container web application and background orchestration engine that interfaces directly with OpenRouter's Batch API (POST /api/v1/batches).
Upload a CSV of inputs, write a template with {{variable}} substitution, select any batch-ready model, and Somnia automatically routes your requests to off-peak queuesβsaving 50% on token costs, polling results in the background, and presenting outputs in an elegant, distraction-free dashboard.
- Automatic 50% Discount: Routes requests strictly through OpenRouter's
:batchendpoints for guaranteed discounted rates. - Persistent Header Cost Badge: Top-level header always displays total batch spend and total token counts (e.g.
$0.00731 β’ 1.6k tok). - 5-Card Stats Summary Bar: Instant visibility into
Total Items,Completed,Failed,Batch Cost (50% Off), andProgress %. - Per-Task Cost Traceability: Every individual task card displays its exact cost and token usage breakdown (e.g.
$0.00195 (422 tok)). - Pre-Flight Budget Estimator: Immediate cost and token estimation shown in the batch creation modal before launching.
- Focused Tabs View: Inspect one task at a time with dedicated screen real estate.
- Scrollable Horizontal Tab Bar: Jump across tasks with live status indicators (
βcompleted,βfailed,β³pending) and primary input preview chips. - Quick-Jump Dropdown: Switch instantly via a selector combobox showing task numbers, inputs, and costs.
- Keyboard Arrow Navigation: Cycle through tasks effortlessly using the
βandβarrow keys. - Feed View Toggle: Prefer vertical scrolling? Toggle between focused
Tabsview and stackedFeedview anytime.
-
Full KaTeX Math Equation Support: Seamlessly renders inline math (
$f_c = \frac{1}{2\pi RC}$ ) and multi-line display derivations ($$H(s) = \frac{1}{1 + sRC}$$ ), ideal for STEM students, researchers, and engineers. - Dual-Mode Output Switcher: Switch between formatted Markdown (with syntax-highlighted code blocks and 1-click copy) and raw source text globally or on individual task cards.
- Row 1 Dry-Run Preview: Preview your rendered prompt with real CSV data before submission.
- System Prompts & Structured JSON: Inject system instructions or enforce JSON schema outputs (
response_format: { type: "json_object" }). - Reasoning Effort Control: Configure reasoning intensity (
low,medium,high) for models likeo1,o3-mini, ordeepseek-r1. - Webhook Notifications: Receive automatic alerts on Discord, NTFY, or Gotify when batches finish.
- 1-Click Failed Row Retry: Retries errored rows into a brand-new batch without touching succeeded items.
- Multi-Format Export: Download results as CSV, JSON, or ready-to-share Markdown (
.md) reports. - Collapsible Slim Sidebar: Shrinks to a 64px icon rail (Gemini style) with a smooth draggable resize divider (240pxβ500px).
- Zero-Dependency Architecture: Embedded APScheduler background poller + SQLite Write-Ahead Logging (WAL) database. No Redis, Celery, or external databases needed.
You can download docker-compose.yml and sample example_circuits.csv directly from GitHub Releases, or save the following configuration:
services:
somnia:
image: holdenstill/somnia:latest # or build locally
container_name: somnia
restart: unless-stopped
ports:
- "8000:8000"
environment:
- OPENROUTER_API_KEY=sk-or-v1-your-openrouter-key # Optional: can also be configured in UI
- DATA_DIR=/app/data
- POLL_INTERVAL_MINUTES=5
- PUID=1000
- PGID=1000
volumes:
- ./data:/app/dataLaunch the container:
docker compose up -dOpen http://localhost:8000 in your browser.
Somnia is specifically architected for TrueNAS Scale:
- In TrueNAS Scale, navigate to Apps β Discover Apps β Install Custom App.
- Application Name:
somnia - Image repository:
holdenstill/somnia(tag:latest) - Port Forwarding: Host Port
8000β Container Port8000. - Storage / Host Path Volume:
- Host Path:
/mnt/your-pool/appdata/somnia - Mount Path:
/app/data
- Host Path:
- Environment Variables:
PUID:1000(or your dataset's owner UID)PGID:1000(or your dataset's owner GID)OPENROUTER_API_KEY:sk-or-v1-...
- Click Install. For a step-by-step walkthrough, see
TRUENAS_SETUP.md.
# Clone repository
git clone https://github.com/holdenstill/somnia.git
cd somnia
# Set up Python virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .\.venv\Scripts\Activate.ps1
# Install requirements
pip install -r backend/requirements.txt
pip install pytest pytest-asyncio
# Run backend unit tests
pytest backend/tests/ -v
# Start FastAPI backend
python -m uvicorn backend.app.main:app --host 127.0.0.1 --port 8000cd frontend
npm install
npm run devOpen http://localhost:5173 for Vite hot-reloading (with backend proxy to port 8000), or build static assets with npm run build to have FastAPI serve them directly at http://127.0.0.1:8000.
| Variable | Description | Default |
|---|---|---|
OPENROUTER_API_KEY |
Your OpenRouter API key. Can also be set in Web Settings. | None |
DATA_DIR |
Directory where SQLite database (somnia.db) and exports are stored. |
/app/data (or ./data) |
POLL_INTERVAL_MINUTES |
Frequency of background status checks for active batches. | 5 |
PUID |
Process User ID for container file permissions (TrueNAS compatibility). | 1000 |
PGID |
Process Group ID for container file permissions. | 1000 |
PORT |
Web server listening port. | 8000 |
somnia/
βββ backend/
β βββ app/
β β βββ routers/ # FastAPI routes: batches, models, settings
β β βββ services/ # Batch processing, OpenRouter API client, poller
β β βββ config.py # App configuration & persistence paths
β β βββ database.py # SQLite schema, WAL mode, migrations, usage resolution
β β βββ models.py # Pydantic schemas for batches, items, and costs
β β βββ main.py # FastAPI entrypoint, SPA static mounting
β βββ tests/ # Automated backend test suite (pytest)
β βββ requirements.txt # Python dependencies
βββ frontend/
β βββ src/
β β βββ components/ # React components: BatchDetailView, NewBatchModal,
β β β # Sidebar, MarkdownRenderer, SettingsModal
β β βββ lib/ # API client, TypeScript types, formatting utilities
β β βββ App.tsx # Layout, sidebar width state, active batch selection
β β βββ index.css # Tailwind dark theme styles
β βββ package.json # Node dependencies (React 18, Vite 6, Tailwind, KaTeX)
β βββ vite.config.ts # Vite configuration & backend proxy
βββ .github/
β βββ workflows/ci.yml # Automated GitHub Actions test & build workflow
βββ docker-compose.yml # Standard self-hosted Docker deployment
βββ Dockerfile # Multi-stage build (Node builder -> Python runtime)
βββ entrypoint.sh # Linux non-root permission handling (PUID/PGID)
βββ TRUENAS_SETUP.md # Step-by-step TrueNAS Scale deployment guide
βββ LICENSE # MIT License
βββ README.md # Project documentation
Somnia includes an automated unit test suite covering API endpoints, CSV interpolation, model validation, background polling, and token/cost accounting:
# Run pytest test suite
pytest backend/tests/ -vThis project is open-source software licensed under the MIT License.