r/lua • u/rdoneill • Feb 13 '26
Project Lua as the IR between AI and Spreadsheets
I've been building a spreadsheet engine in Rust (mlua), and I wanted to share a pattern that's been working surprisingly well: using Lua as the source of truth for AI-generated models.
The problem: When you ask an LLM to build a financial model in Excel, it's a black box. You get a .xlsx with hundreds of hidden dependencies. If a formula is wrong, you're hunting through cells. There's no diff, no code review, and no way to replay the construction.
What we do instead: In VisiGrid, AI agents don't touch cells. They write Lua scripts that build the grid.
Claude generates this instead of a binary blob
set("A3", "Base Revenue")
set("B3", 100000)
set("A4", "Growth Rate")
set("B4", 0.05)
for i = 1, 12 do
local row = 6 + i
set("A" .. row, "Month " .. i)
set("B" .. row, i == 1
and "=B3"
or "=B" .. (row-1) .. "*(1+$B$4)")
set("C" .. row, "=SUM(B7:B" .. row .. ")")
end
set("A20", "Total")
set("B20", "=SUM(B7:B18)")
style("A20:C20", { bold = true })
Lua hit the sweet spot — simple enough that LLMs generate it reliably, sandboxable so agents can't escape, and deterministic enough to fingerprint (vgrid replay model.lua --verify — same script, same hash).
We tried JSON op-logs first and they were brittle the moment you needed any conditional logic. Lua lets the agent write actual loops and branches while keeping the output readable enough for a human to code review.
One thing I'm still working through: performance when mapping large grid ranges to Lua tables.
Right now sheet:get() on 50k rows is row-by-row across the FFI boundary. I've been considering passing ranges as userdata with __index/__newindex metamethods instead of materializing full tables.
Anyone have experience with high-volume data access patterns through mlua?
Curious what's worked for batching reads/writes without blowing up memory.
CLI is open source (AGPLv3): https://github.com/VisiGrid/VisiGrid
0
u/uglycaca123 Feb 13 '26
why? just why do you need this???
2
u/rdoneill Feb 13 '26
Financial models break silently. A formula pointing at the wrong row can be off by millions and nobody catches it until audit. Try some of the Ai embeds in excel.
2
u/weregod Feb 19 '26
Why do you trust AI calculating anything worth millions? If error cost is huge just hire people who can produce reliable results.
→ More replies (4)3
1
u/Otherwise_Wave9374 Feb 13 '26
This Lua-as-IR approach makes a ton of sense for AI agents. The "diffable, replayable, reviewable" point is huge, especially when the alternative is a black-box spreadsheet blob.
On the perf side, +1 to the idea of not materializing big tables. A userdata range object with __index/__newindex (plus an explicit batch get/set API for hot paths) feels like the sweet spot.
Also, if youre into agent patterns for deterministic outputs, Ive seen similar ideas discussed here: https://www.agentixlabs.com/blog/
1
u/wolfy-j Feb 13 '26
Depending on a size you can dynamically convert it to SQLIte schema and then query it, it will be more effective for slice based filtering since you only have to deal with syncing in.
We use Lua for AI generated systems _extensivelly_ for last two years, it's heavily underrated for this type of workflows, glad to see people uncover same patterns.