How to Add Low Stock Alerts to an AI-Built App
A low stock alert is a threshold, a daily check and a memory of what you already warned about. Here is the schema and the scheduled query.
A low stock alert needs three things: a reorder point stored on each product, a scheduled check that compares current stock against it, and a record of which products you have already warned about, so you are told once per dip instead of every morning. The third part is the one most generated versions leave out, and it is why people mute the alerts within a week.
Here is the whole feature, small enough to build from a single prompt and a review.
Start with the reorder point, not a single number
One global threshold, such as "alert below 10", is wrong for almost every product. Ten units of a fast seller lasts a day. Ten units of a slow one lasts a quarter. Give each product its own reorder point.
The standard formula is simple:
Reorder point = average daily sales x supplier lead time in days + safety stock
Suppose a candle sells about 4 a day, the supplier takes 7 days to deliver, and you want 10 spare in case a delivery slips. The reorder point is 4 x 7 + 10 = 38. When stock drops to 38, an order placed that day should arrive just as you run low. A flat threshold of 10 would have warned you after you were already out.
You do not need clever maths on day one. Let the owner type the number per product, and offer the formula as a suggestion in the form. If you already track sales, the guide to building an inventory app with AI shows the stock and movement tables this builds on.
The schema
Add two columns to the products table and one small table for alert state:
alter table products
add column reorder_point int, -- null means "no alerts for this item"
add column lead_time_days int;
create table stock_alerts (
product_id uuid primary key references products(id) on delete cascade,
alerted_at timestamptz not null default now(),
stock_at_alert int not null
);stock_alerts is the memory. A row means "we have already told someone this product is low". No row means it is healthy or not yet flagged.
Add low stock alerts with a scheduled check
Run one query on a schedule, once or twice a day. The mechanics of running a job on a timer are in the guide to adding scheduled tasks to an AI-built app. The query itself does three things in order.
-- 1. Products that just went low and have not been flagged
select p.id, p.name, p.stock_on_hand, p.reorder_point
from products p
left join stock_alerts a on a.product_id = p.id
where p.reorder_point is not null
and p.stock_on_hand <= p.reorder_point
and a.product_id is null;
-- 2. After sending, record them
insert into stock_alerts (product_id, stock_at_alert)
select id, stock_on_hand from products
where reorder_point is not null
and stock_on_hand <= reorder_point
on conflict (product_id) do nothing;
-- 3. Clear the flag for anything that recovered
delete from stock_alerts a
using products p
where p.id = a.product_id
and p.stock_on_hand > p.reorder_point;Step one finds new problems. Step two remembers them. Step three resets the memory when a delivery arrives, so the next dip triggers a fresh alert. Run them in one transaction so a failure between sending and recording cannot cause a repeat.
If you prefer real-time over daily, a database trigger that runs when stock changes can do the same comparison. The PostgreSQL documentation on trigger functions explains how a function can run after an update. For a small shop a daily digest is usually better, because a trigger can fire dozens of times during a busy hour.
Send one digest, not one email per product
If twelve products go low overnight, one email listing all twelve beats twelve emails. Build the message from the result of query one: product name, stock left, reorder point, and a link to the product page.
Good alert content is short and actionable:
Product name and current stock.
The reorder point it crossed.
Days of cover left at the recent sales rate, if you track sales.
A link straight to the product so the owner can reorder.
Sending is covered in adding email sending to an AI-built app. If your app also has an in-app inbox, put the same alert in the notification center so it is not only in email.
Prompt to give your AI builder
Add low stock alerts. Each product gets an optional reorder_point and lead_time_days. A scheduled job runs daily at 07:00 shop time. It finds products at or below their reorder point that are not in the stock_alerts table, sends the owner one email listing all of them, and then inserts them into stock_alerts. When stock rises above the reorder point, delete the row so the product can alert again. Never send more than one email per run. Add a settings page to edit reorder points.
Then check three things in the code it returns: the digest is one email, the insert happens in the same transaction as the send step, and null reorder points are skipped.
Edge cases worth testing
Stock goes negative. If you allow overselling,
stock_on_handcan be below zero. The comparison still works, but show it clearly in the alert.Stock is edited by hand. A manual correction upward should clear the flag, and your step three handles that.
CSV import overwrites stock. If you import counts in bulk, run the check afterwards. A CSV import guide covers validating those rows.
Time zones. "Daily at 07:00" means shop time, not server time. Store the shop's time zone and schedule against it.
A product with no sales history. Fall back to the manual reorder point and skip the formula.
If your business wants more than thresholds, such as forecasting how much to order, that is a different job, covered in using AI for inventory reordering. The alert feature is the reliable base it should sit on, and the wider build is covered in building an app with AI.
Frequently asked questions
What is a reorder point?
It is the stock level at which you should place a new order. It is usually average daily sales multiplied by supplier lead time, plus a buffer for delays.
How often should a low stock alert run?
Once or twice a day suits most small businesses. Run it more often only if you sell very fast or deliveries are same-day. More frequent checks do not need more emails, because the alert-state table prevents repeats.
Why did my alerts stop being useful?
Usually because they repeat. Without a record of what was already flagged, the same products appear every day and people learn to ignore the message. The state table fixes that.
Should alerts go by email or text message?
Email is fine for routine restocking. Reserve faster channels for genuine emergencies such as an essential ingredient running out, and keep those thresholds separate.
How did this land?
About the author

Developer Advocate
Steve builds something with Swarmz every week and writes up what worked, what broke, and what he'd do differently. Tutorials and hands-on guides are his lane.


