Skip to main content

Module energy

Module energy 

Source
Expand description

Electricity-footprint estimate for saved tokens.

This mirrors the website /metrics methodology (same J_PER_TOKEN) so a user’s local “energy saved” figure and the community scoreboard always reconcile.

~0.4 J per saved token is a deliberately conservative midpoint of measured modern inference (Llama-3-70B FP8 on H100 + vLLM ≈ 0.39 J/token, John Snow Labs “Tokens per Joule”, 2025; query-level estimates such as Epoch AI’s ~0.3 Wh per GPT-4o query imply more). lean-ctx mostly removes cheaper prefill/context tokens, so we never overstate. Real figures vary by model and hardware — this is always surfaced as an estimate.

Constants§

G_CO2_PER_KWH
Grams of CO₂-equivalent per kWh of grid electricity. ~475 g/kWh is the global average grid carbon intensity (IEA, ~2023) — a transparent, conservative midpoint. Users on cleaner grids can override via LEAN_CTX_GRID_CO2_G_PER_KWH so the local footprint reflects their actual electricity mix.
J_PER_TOKEN
Joules of inference compute avoided per token that lean-ctx kept out of the context.
WH_PER_PHONE_CHARGE
Watt-hours in a full smartphone charge — the relatable yardstick used in the UI.

Functions§

co2_grams_for_tokens
Grams of CO₂-equivalent avoided for a given number of saved tokens. g = Wh / 1000 · gridIntensity.
format_co2
Human-readable CO₂ mass with adaptive units (g / kg / t).
format_co2_for_tokens
Convenience: formatted CO₂ string straight from a saved-token count.
format_for_tokens
Convenience: formatted energy string straight from a saved-token count.
format_wh
Human-readable energy with adaptive units (Wh / kWh / MWh).
grid_co2_g_per_kwh
Effective grid carbon intensity (g CO₂/kWh), honoring the LEAN_CTX_GRID_CO2_G_PER_KWH override when it is a positive, finite number.
phone_charges
Equivalent number of full smartphone charges for a given saved-token count.
phone_charges_hint
Rounded phone-charge equivalent as a display string (e.g. "≈ 117 phone charges"), or None when the saving is too small to round to at least one charge.
wh_for_tokens
Energy (Wh) saved for a given number of saved tokens. Wh = tokens · J/token / 3600.