· 3 min read
Why my AI booking assistant can't double-book
BookInt lets Claude talk to visitors but never book. A Postgres lock and an EXCLUDE constraint make two bookings in one slot impossible.
- AI agents
- PostgreSQL
- Next.js
The chat button in the corner of this site is BookInt, an AI assistant I built for service businesses. It answers questions from the business's own facts, asks a few qualifying questions and offers real times from Google Calendar.
The first thing I decided when I started it was what the AI would not be allowed to do.
The problem with letting the model book
A language model is good at conversation. It's not a source of truth. If the model can create bookings, sooner or later it will book a slot that was taken a second ago, book a time the visitor never confirmed, or book twice because a request was retried.
None of those are rare edge cases once real people use it. Two visitors reading the same open slot at the same time is normal traffic.
So the rule is simple: the AI only talks. It can record lead details and show open slots. The visitor picks and confirms the time themselves, and the server does the booking.
Three layers, one booking
When a visitor confirms a slot, the server doesn't trust anything it was told. It checks again, in this order.
- Re-check the slot. The server asks Google Calendar and the database again whether the time is still free. What the chat showed a minute ago doesn't count.
- Take a lock. The booking runs inside a transaction that takes a Postgres advisory lock for that business. Two requests for the same business wait in line instead of racing.
- Let the database refuse. Even if both checks above had a bug, the table itself can't hold two overlapping confirmed appointments.
The lock is one line:
SELECT pg_advisory_xact_lock(hashtext($1)); -- $1 = the business id
It's released by itself when the transaction ends, so there's nothing to clean up if a request crashes.
The constraint that makes it impossible
The last layer is the one I trust most, because it doesn't depend on my application code being right. It's an EXCLUDE constraint on the appointments table:
CREATE EXTENSION IF NOT EXISTS btree_gist;
ALTER TABLE "Appointment"
ADD CONSTRAINT "Appointment_no_overlap"
EXCLUDE USING gist (
"organizationId" WITH =,
tstzrange("startAt", "endAt", '[)') WITH &&
) WHERE ("status" = 'CONFIRMED');
In plain words: for the same business, no two confirmed appointments may have time ranges that overlap. The '[)' means a range includes its start but not its end, so a 10:00 to 10:20 call and a 10:20 to 10:40 call don't count as overlapping. (The gap a business wants between calls is enforced by the server on top of this.) Cancelled appointments are ignored by the WHERE clause, so a cancelled slot can be booked again.
If an insert breaks the rule, Postgres rejects it with error 23P01. The server catches that and tells the visitor the time was just taken, then offers the next ones.
Retries can't book twice either
Phones drop connections. A visitor taps "Confirm", the response never arrives, and the widget retries. Without care, that's two bookings for one person.
Every confirm request carries an idempotency key, and the database has a unique index on the business and that key. The second request finds the first booking and returns it, instead of making a new one.
How I know it works
There's a test in the suite that fires 10 concurrent requests at the same slot and checks that exactly one booking comes out. The whole suite is 240 tests as of this week, run against a real Postgres database in Docker, not mocks.
What I'd tell anyone building an AI agent
Give the model the conversation and keep the facts in code and in the database. The model decides what to say. Your code decides what's true, and the database has the final word.
You can try it with the chat button on this page, or see how BookInt works in the case study.