PoupaNaEnergia
Solo product
An independent electricity-tariff comparator for the Portuguese market. Users upload a power bill, a Gemini-powered pipeline extracts their consumption, and a regulatory engine fed by ERSE data synced daily ranks fixed-price plans by real annual cost.
- Type
- AI / Energy
- Status
- Live

- Problem
- Households in Portugal cannot tell which electricity plan actually costs less against their own bill.
- Constraint
- Live ERSE data, messy PDF bills, and a product that has to be trusted with money.
- Decision
- Gemini extracts the bill; a regulatory engine ranks fixed-price plans by annual cost; Lighthouse CI runs on every pull request and every push to main with hard a11y, SEO and LCP budgets.
- Outcome
- Live at poupanaenergia.pt — bill in, ranked plans out.
One bill, from upload to verdict. In order: Upload, type allowlist · size cap · encrypted-PDF probe; then Storage, held until extraction ends; then Extraction ladder, a small model first, a larger one below the confidence bar; then Schema and bounds, JSON schema · temperature 0 · plausibility checks; then Delete the PDF, on success and on failure; then Confirmation form, the household states what the bill cannot; then Comparison engine, fixed-price offers × regulatory constants; then Verdict, bound to the uploading session.
Side lane — daily: Daily sweep, the regulator's simulator, 10 power tiers × 3 cycles; Offers, what the engine ranks against.
- 1Phantom savings: +38 % on a real bill, +5.8 % after — two net totals extracted, prices renormalised.
- 2Auto-validation was removed on purpose: a fact that lives in the household is not on the bill.
- 3An exhausted model quota stops with a typed error instead of retrying.




General notes
- 01Per-supplier templates were rejected; the model is anchored to the totals the bill itself guarantees.
- 02The PDF is deleted on success and on failure.
Bill of quantities · As-built 2026-11
| Item | Quantity |
|---|---|
| API routesgit ls-tree -r --name-only origin/main | grep -c 'app/api/.*route.ts' | 17 |
| Tablesgit grep -hoiE 'create table' origin/main -- 'supabase/migrations/*.sql' | wc -l | 21 |
Detail 1:1
The bill's net total is the anchor: the extracted unit prices are scaled until price × kWh adds up to it. One period with no price sends every period to one flat rate, so the missing share is never counted twice.
export function normalizeEnergyUnitPrices(input: NormalizeEnergyInput): Record<string, number> { const { extracted, breakdown, net_total_eur } = input; if (net_total_eur === 0) { return Object.fromEntries(Object.keys(breakdown).map((k) => [k, 0])); } const breakdownKeys = Object.keys(breakdown); if (breakdownKeys.length === 0) { throw new Error('normalizeEnergyUnitPrices: cannot derive unit prices from empty breakdown'); } const total_kwh = Object.values(breakdown).reduce((sum, v) => sum + v, 0); if (total_kwh === 0) { throw new Error('normalizeEnergyUnitPrices: total_kwh is zero, cannot derive'); } // Invariant: Σ result[p] × breakdown[p] === net_total_eur. Ratios can only be // preserved when EVERY breakdown period has an extracted price; with a gap, // scaling the known periods and pricing the missing ones flat counts the // missing share twice (fake savings). A flat rate keeps the total exact. const flat = () => Object.fromEntries(breakdownKeys.map((k) => [k, net_total_eur / total_kwh])); if (breakdownKeys.some((p) => extracted[p] === undefined)) return flat(); const gross_total = breakdownKeys.reduce((s, p) => s + extracted[p] * breakdown[p], 0); if (gross_total === 0) return flat(); const correction = net_total_eur / gross_total; return Object.fromEntries(breakdownKeys.map((p) => [p, extracted[p] * correction]));}