We built a Next.js 15 starter for Claude-powered agent SaaS and the part we spent the most time on was not the agent loop. It was knowing what each user costs.
What we ended up with:
- One usage row per model call: input, output, cache-read and cache-write tokens, the cost in integer micro-dollars, and the id of the price row that produced it.
- A versioned price table. When a model price changes you add a row with a start date, and old usage keeps its old cost.
- A monthly plan cap that is checked before every agent turn, not only before the request.
- A sliding-window rate limit in Postgres, so there is no Redis to run.
Three files are public in full, including the agent loop (about 100 lines) and the metering code: https://agentwright.app/inside/
The whole kit is a paid product (one-time, 14-day refund), but the write-ups are free: https://agentwright.app/blog/
Two things we would like feedback on:
- Do you cap users in tokens or in dollars? We cap tokens today and are not sure it is the right default.
- Is a price snapshot per row overkill for a small product, or have you been bitten by a price change too?
Most of the code was written with Claude and every release goes through type check, tests against Postgres and a production build in CI.