Home›Blog›Larzscript
Larzscript

Building a Budget Tracker in Larzscript, Part 2: Categories and Limits

2026-08-01 · Larzscript

Part 1 built accounts and a first expense in larzscript-budget. This part adds spending limits - a real use of require/unless as a guardrail, not decoration.

A limit is just a stored number, checked before the pay

category-add food --limit=300.00 stores a monthly cap alongside the category. The interesting part is *where* the check happens - before the real pay, so a rejected expense never touches the wallet at all:

if ledger["category_limits"].has(category) {
  let limit = ledger["category_limits"][category]
  let month = date_str[0:7]
  let spent = 0
  for t in ledger["transactions"] {
    if t["category"] == category and t["date"][0:7] == month and t["amount"] < 0 {
      spent = spent - t["amount"]
    }
  }
  unless spent + abs_cents is at most limit {
    fail("'" + category + "' limit " + fmt_cents(limit) + " would be exceeded (" + fmt_cents(spent + abs_cents) + " this month)")
  }
}

pay money(abs_cents / 100.0) from acct to cat

That is at most isn't a stylistic choice for the blog post - it's real Larzscript syntax from a recent language update, and it's the natural way to phrase this check: *unless the total spent is at most the limit, refuse.* The category-limit total is computed from the transaction log (filtered to this month), not from the category wallet's all-time balance - the wallet balance stays a genuinely useful *lifetime* total per category, separate from the *monthly* figure the limit cares about.

Seeing it reject a real transaction

$ larzscript budget.lz category-add food --limit=300.00
added category 'food' (limit $300.00/mo)
$ larzscript budget.lz add "coffee" -4.50 food
spent $4.50 on 'coffee' (food)
$ larzscript budget.lz add "big grocery run" -400.00 food
Error: 'food' limit $300.00 would be exceeded ($404.50 this month)

The rejected $400.00 expense never reaches pay - checking's balance and food's balance are both untouched, and nothing gets written to the transaction log. A require/unless failure in Larzscript is a hard stop, the same way insufficient funds already was in Part 1.

Income, the same honest way

Credits use the identical pay discipline instead of a bare .credit() call that would skip the ledger - money comes from a synthetic, deliberately bottomless external wallet:

let acct = wallet_from_cents(ledger["accounts"][account])
let external = wallet_from_cents(999999999900)
pay money(amt_cents / 100.0) from external to acct

Every dollar that enters or leaves the system is a real pay, income included - the transaction log is a complete, honest record, not something with a special-cased exception for credits.

Part 3 covers reports (a category breakdown with an ASCII bar chart) and monthly recurring bills. Full source on GitHub.

Ready to get started?

See the full source on GitHub →

Explore the apps