Menu

1 / 2

Tracking LLM costs per user: the schema that billing can actually trust

DEV Community: postgres·Shipwright·9 days ago
#LzmMNIe3
#devtopostgresql#devto#dev#args#usage
Reading 0:00
15s threshold

Your model provider tells you what the whole account spent. It does not tell you that one free-tier user generated a third of it. Until cost is attributed per user, every pricing decision — plan limits, free-tier caps, whether that enterprise deal is even profitable — is a guess. Here is the schema I use, and the two decisions in it that took real debugging to learn.

One row per model request

Every request writes exactly one row, at the moment usage is known — from the stream's finish hook, or right after a non-streaming call returns:

export async function recordUsage ( args : RecordUsageArgs ): Promise < void > { await db () . insert ( usageEvents ) . values ({ userId : args . userId , model : args . model , route : args . route , // which feature spent the tokens, e.g. "chat" inputTokens : args . usage . inputTokens , outputTokens : args . usage . outputTokens , cacheReadTokens : args . usage . cacheReadTokens , cacheCreationTokens : args . usage . cacheCreationTokens , costMicroUsd : Math .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.