Taking the math away from my meal-planning agent
- Started
- June 2026
- Built with
- TypeScript, Bun, SQLite, Cloudflare
- Source
- Private repository
Every food tracker I tried, like MyFitnessPal and Lifesum, felt like adding another chore to my day: searching broken crowdsourced databases, hunting for Danish groceries that aren’t there, and tapping through ten screens just to log lunch. Trackers force a bad bargain. You can get accuracy if you tolerate the friction, or you can chat with an LLM for zero friction right up until you check its math and realize it is guessing.
I built Mealplan to get both: tracking that feels like texting a friend, backed by a deterministic ledger that never guesses a number.
Accurate and frictionless
One afternoon I texted OpenAI’s Dots, which connects to Mealplan over MCP: “Lunch was some Rana tortellini or something. A brownish bag I bought at Bilka. Beef or something in it.”
It searched Bilka’s online catalog, came back with two 250 g candidates (Rana pork-and-beef ravioli and a store-brand braised beef tortelloni), and asked which one I meant and whether I ate the whole bag. I confirmed the first one. It imported the nutrition label, saved the product to my food library, and logged 668 kcal and 27.5 g of protein.
Most days take even less. Saying “same lunch as yesterday, plus a protein bar” copies yesterday’s meal and logs the bar in one turn. Photograph a new nutrition label once and its barcode, macros, and carb rules stay in my library. From there, a plate photo, barcode scan, or recipe link resolves against the products I actually buy.
Why an LLM alone breaks down
Before writing an engine, I tried meal planning with plain LLM prompts. It felt effortless until I checked the plan against the packages in my kitchen.
I eat the same high-protein, high-fiber sandwich bread most days. It comes in a 380 g pack of four 95 g pieces, so two pieces are 190 g and 494 kcal at 260 kcal per 100 g. The model kept writing “4 slices”, blurring slices, pieces, and grams together, and every swap pushed the macro totals and grocery list further apart.
The rest of the kitchen is worse. Dry jasmine rice cooks up to 2.85 times its weight, so applying the dry label to cooked grams nearly triples the calories. A 400 g can of black beans drains to 240 g; scoring the whole can against the drained label overcounts by two-thirds. Olive oil is poured in milliliters, and 15 ml weighs 13.8 g, not 15 g.
Labels have their own traps. On EU labels, carbohydrate already leaves out fiber, while US labels include it. My own first brief told the system to subtract fiber from carbs, which subtracted it twice on Danish groceries until I checked the regulation. Calories behave the same way: label kcal rarely equals 4 × protein + 4 × carbs + 9 × fat because fiber contributes energy and manufacturers round numbers, so the engine stores label calories as truth and never recomputes them.
Bread 2 pieces of protein bread: 190 g, 494 kcal by the label
4P + 4C + 9F says 460, because the label counts fiber too. Asking for a “slice” is an error: the bread has no such unit
Frictionless in the agent, exact in the engine
So I split the work down the middle. The engine owns every number: units and cooking states, recipe scaling, copying meals, grocery totals, and the energy model. The agent owns what software is bad at: deciphering vague descriptions, searching grocery catalogs, reading label and meal photos, and importing recipes. One rule in its instructions holds the line: never do macro arithmetic yourself, always call the engine.
The engine is pure TypeScript on Bun and SQLite, with 55 JSON commands that each return the same shape of data, warnings, and errors. When a label’s numbers contradict each other, it surfaces a warning instead of guessing. Bread, rice, beans, oil, and EU carbs were the first five tests I wrote, before any features.
A private food library fixed the gap in off-the-shelf trackers, where crowdsourced databases barely cover local groceries and half the entries are wrong. Every product gets photographed, scanned, or imported once and saved. A nightly librarian agent sweeps the library while I sleep and files cleanup proposals, like merging duplicate brands or matching unit names, that only apply if I approve them.
How it runs
It started as a CLI and became a phone-first PWA: a day view, a food library, a barcode scanner, voice input, and chat. Requests go through a Cloudflare Worker, while the agent and database run on a small VPS.
Ask the agent a chat turn: Codex decides what to do, the engine does every sum
Phone, ChatAgent, RunnerHub, the daemon, Codex, its MCP tools, the engine, SQLite
The VPS daemon dials out to a Cloudflare Durable Object and holds a WebSocket open, so the server needs no inbound port. Tapping a meal in the day view calls the engine directly with no LLM in the loop. Chat goes to Codex, which reaches the engine through typed MCP tools. Litestream streams every database write to R2 so the container can restore from backup on startup.
Adaptive expenditure
The app also estimates how many calories I burn each day by comparing what I eat against how my weight changes over time. Daily weigh-ins swing mostly from water, and one day’s swing carries into the next, so I calibrated the model on 1,351 weigh-ins spanning eight years. It looks back 84 days and skips the first week after any break while water weight settles.
Testing with production messages
A Codex update once broke tool approvals before I noticed. Now every change to the prompt, tools, or model runs against 83 eval scenarios built from messages I actually sent the app.
Each scenario replays the live stack against a production database snapshot rewound to the moment I sent the message, so later foods disappear and “today” matches what it meant then. A scenario fails if anything outside the expected dates changes, or if the reply prints a number that no tool returned. That is the bread bug, written as a test.
One engine, any agent
I dropped MCP on day one because a CLI felt like enough. Five weeks later it came back as Codex’s boundary to the engine, and in November I exposed those same tools at /mcp behind Cloudflare Access OAuth so Claude, ChatGPT, and OpenAI’s Dots can log meals too. None of it required changing the engine, because its 55 commands never cared where a request came from.