Using the Three Table Pattern to Track Your AI Costs
TL;DR: When you add AI to an app, persist the request, persist the response, and link them with an exchange. That one structure makes every round-trip traceable — and token costs fall out naturally...

Source: DEV Community
TL;DR: When you add AI to an app, persist the request, persist the response, and link them with an exchange. That one structure makes every round-trip traceable — and token costs fall out naturally. My default when adding AI to an app A pattern I keep coming back to: The Three Table Pattern — one table for the request, one for the response, and one exchange table that links them and holds the meter. My default is to persist what I'm sending, persist what comes back, and tie both to a single exchange id so one user↔model round-trip is traceable end to end. That id is what lets me answer: What did we actually send? What did we get back? What did this turn cost? Without reconstructing anything from logs or guessing from the UI. Not a doctrine. Just the shape I keep reaching for. The three tables Requests — what went out Stores the user's input, a reference to the user, any context the request needs (session id, document id, etc.), and a status of pending. Responses — what came back Stores