How Larzscript's Gas Metering Works (and Why It Fails Closed)
2026-08-01 · Larzscript
Larzscript borrows one idea from blockchain languages — metering execution with "gas" — and repurposes it as a plain, local resource budget. No network, no fees paid to anyone. Just a hard limit on how much a piece of code is allowed to do before it's stopped.
The syntax
You attach a gas cost to a function with the gas keyword right in its signature:
fn scan() gas 500 { return 1 }
Every call to scan() spends 500 gas against whatever budget is currently active. Call it enough times and you'll eventually hit the ceiling:
fn scan() gas 500 { return 1 }
scan()
scan()
scan() # OutOfGasError: out of gas calling 'scan' (with gas budget 1200)
With a budget of 1200 and each call costing 500, the first two calls spend 1000 total — still under budget. The third call would push total spend to 1500, past the 1200 ceiling, so it never executes. Larzscript raises OutOfGasError and stops, instead of letting the call run and silently overspending.
Why "fails closed" matters here
The alternative design — the one most languages default to — is to let code run until it hits some external limit: a timeout, an out-of-memory kill, a cloud bill you notice next month. That's "failing open": the default is to keep going, and something else has to intervene to stop it.
Gas metering flips the default. The budget is checked *before* the expensive call is allowed to proceed, not after. If spending would exceed the budget, the call simply doesn't happen. This is the same instinct behind Larzscript's require keyword for money (a pay statement can't run past a failed balance check) — applied to compute instead of currency.
Where this is actually useful
- Untrusted or user-submitted scripts. If your platform lets users run their own logic (a plugin system, a webhook handler, a pricing rule), a gas budget caps worst-case cost without needing a separate sandboxing layer.
- Predictable billing for metered features. If you charge customers per unit of work, gas gives you a deterministic, testable number for "how much does this operation cost" instead of guessing from wall-clock time.
- Defense against runaway loops. A bug that spins a function in an unbounded loop hits the gas ceiling and raises a catchable error, instead of pegging a CPU core indefinitely.
It's real, shipped, and kernel-tested
Gas metering isn't a paper feature — it's used inside LarzOS, the operating system being built in Larzscript, where compute inside the kernel's own shell is metered against a wallet that fails closed exactly the way scan() does above.
Try it yourself
The playground runs the real interpreter in your browser — paste the scan() example above, tighten or loosen the budget, and watch exactly where OutOfGasError fires.