
Next.js 16 starter kit for paid AI agent products on the Claude API: agent loop, per-user cost metering, plan caps, auth, billing. [email protected]
Joined November 2026
- Tweets64
- Following25
- Followers6
- Likes0
Our Claude starter kit's agent loop is now public in full: 104 lines of TypeScript.
Streaming, parallel tool calls, a 12-turn limit, one cache breakpoint on the system prompt, and a plan-cap check before every model turn.
Read it before you write your own. Link in profile.
11
A tool timeout that's too short can cost more than a slow tool.
A job takes 3 minutes, the harness kills calls at 30s, so the agent polls. Each poll is a model turn that re-reads the context.
Hypothetical: Sonnet 5.5, 40k uncached tokens, 10 polls = $0.80 just to wait.
12
On Nov 25 we stopped posting to our X timeline. The week before, timeline posts got 0 views each.
The next 15 posts all went into X Communities: 13 to 63 views each, about 30 on average. Followers: 3 to 5.
Small numbers. But on a new account, 30 beats 0.
11
Your first AI bill surprise will most likely be one user or one runaway loop, not your average.
Three cheap alerts from your own usage table:
- daily total vs budget
- one user far above their own normal
- spend in the last hour
The provider spend limit is the last net.
16
The Batch API is half price. The catch: results come back in any order.
Set custom_id to your own job row id and match each result by it. Never by position.
Results stay downloadable for 29 days, so a crashed collector can pick them up later.
26
The whole agent loop in our kit is 104 lines of TypeScript.
It streams, runs tool calls in parallel, stops at 12 turns, caches the system prompt and checks the user's plan before every model turn.
We kept it short on purpose: you should be able to read where the money goes.
1
1
17
An advisor model is cheap only if it reads a summary.
Hypothetical: Sonnet 5.5 does the work and asks Opus 5.5 3 times, 500 tokens out each. Caches are per model, so no cache hits:
60k-token transcript: $0.75
5k-token summary: $0.09
Send the question, not the history.
15
Log one extra field on every Claude call: the request id.
The TypeScript SDK puts it on the returned message as _request_id.
The day one call misbehaves, it is the first thing Anthropic support asks for.
1
47
Two parallel requests can both pass "budget left?" and overspend together.
Reserve the worst case before the call: max_tokens times the output price. 4,096 on Opus 5.5 is $0.082. Settle to real usage after.
Lower max_tokens = more parallel calls under the same cap.
22
Valid JSON from Claude can still be broken JSON.
Check stop_reason before you parse. max_tokens means the output was cut off. A refusal may not follow your schema.
And a valid category can still be the wrong category. Only evals catch that one.
22
Polar's sandbox sent us subscription.updated before subscription.created.
Webhooks arrive at least once and in no fixed order. Our rule: every event re-reads the subscription and recomputes the plan, and an older event never overwrites a newer one. The plan came out right.
14
When people use your SaaS through an MCP server, your product analytics go blind: no page views, no clicks, no session replay.
The tool calls are the new usage log. Record each one: user, tool, duration, result size, error.
That table shows which features people actually use.
32
Postgres row-level security can silently do nothing.
The table owner skips the policies unless you add FORCE ROW LEVEL SECURITY. Superusers always skip them.
Connect the app as a role that does not own the tables.
More on our blog, link in profile.
2
38
Turn 50 of an agent chat costs far more than turn 5, because every turn sends the whole history again.
One turn on Opus 5.5:
400k tokens: $1.60 ($0.08 from cache)
40k after compaction: $0.16 ($0.008 from cache)
Caching makes tokens cheaper. Compaction makes fewer.
27
Test an agent loop without paying for a single API call.
A tool is a function: unit test it. For the loop, a fake client returns a tool_use block, then end_turn. Fast, free, repeatable.
Real-model checks go in a separate eval suite.
More on our blog, link in profile.
27
Our kit stores every AI cost as an integer: micro-dollars. 1 = $0.000001.
A Haiku 5.5 call, 3,000 tokens in and 400 out, is 500.
Integers add up exactly. A month of float sums would not, and you would not know which user the missing cents belong to.
1
1
19
40 tools at ~250 tokens each = 10,000 input tokens on every turn, before the user types a word.
Hypothetical 20-turn session on Sonnet 5.5: $0.40 for tool definitions alone. Cached: $0.044 (one write, 19 reads).
Keep the tool list stable so it stays cached. Drop unused tools.
1
2
64
Same agent task, three bills: $2.04, $0.69, $0.26.
Fable 5.2 plans in all three (20k in / 2k out). Only the 10 worker calls change model (15k in / 1.5k out each): Fable, Sonnet 5.5, Haiku 5.5.
The workers are 88% of the all-Fable bill. Test cheaper ones first.
52
The model that costs 2x per token can be cheaper per finished task.
50 tasks, 40k in / 4k out each:
Opus 5.5: $12, 45 pass = $0.27 a pass
Sonnet 5.5: $6, 20 pass = $0.30 a pass
Pass rates here are made up. Measure yours, then divide.
1
27
Claude Sonnet 4.5 retires from the Claude API on Nov 30; requests to it then return a 404.
In our kit the model is one env variable (AGENT_MODEL), and the agent loop refuses any model without a price row before it spends a token. Switching is one line plus one price row.
2
21