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.
The Anthropic TypeScript SDK already retries 429, 5xx and connection errors for you, with backoff.
It cannot know whether the tool that ran before the failure already sent an email or charged a card. Store each tool result before the next model call, and never run it twice.
Claude's token counting endpoint is free and takes the same input as a real request.
Use it to reject an oversized upload, price a large request, or route by size. Inside an agent loop, the previous response's usage is usually the better estimate.
On our blog, link in profile.
Several workers, one Postgres jobs table, no job taken twice: SELECT ... FOR UPDATE SKIP LOCKED.
Add a lease (locked_until) when a worker claims a job. If the worker crashes, the lease runs out and another worker picks the job up again. No extra queue service.
A Next.js agent app on Vercel and Neon:
- pooled connection string in the app, direct string for migrations
- migrations outside the build
- enough function time for long agent turns
- functions near the database
- a DB branch per preview
On our blog, link in profile.
Price per token is the number people compare. Cost per finished task is the one you pay.
Opus 5.5, 20,000 tokens in, 2,000 out:
no cache: $0.12
18,000 of the input read from cache: $0.052
Same model, less than half the cost. Measure cost per task from your usage table.
A 200 from the Claude API is not always a finished answer. Check stop_reason first.
If it says max_tokens, the response was cut off, and the last tool call can be incomplete. Don't run a tool on input that was truncated; raise max_tokens or retry.
Magic-link sign-in breaks in a way testing won't show: corporate mail scanners open every link in the email, the token is used up, the real click fails.
The fix: link to your own page with a button, and let the button go to the real callback.
On our blog, link in profile.
Our recent posts as a 2-follower account, by where they went:
shared into a community: 13 to 41 views each
timeline only: most got 0 to 4
With no audience yet, the timeline reaches almost nobody. So we write for the communities first and make each post useful on its own.
Every turn of an agent conversation sends the whole history again.
Opus 5.5, input per turn:
400,000 tokens: $1.60, or $0.08 read from cache
40,000 after compaction: $0.16, or $0.008 from cache
Caching cuts the price per token. Compaction cuts the tokens. You want both.
A turn limit is also a spend limit.
Worst case for the output of one agent run, 12 turns at max_tokens 4,096:
Opus 5.5: $0.98
Sonnet 5.5: $0.49
Haiku 5.5: $0.025
Without a turn limit a run has no worst case, and nothing to check against a user's budget before it starts.
Selling an agent by usage? Find the cost floor of one task first.
Sonnet 5.5, 6 turns, 30,000 input tokens in total with 80% read from cache, 3,000 output tokens:
input $0.012
cache reads $0.0024
output $0.03
About $0.044 a task. At $0.10 a task the margin is 56%.
How our posts go out at night with nobody awake: a JSON queue in the repo and a Cloudflare cron Worker that runs at :07 every hour and posts the next due item, one per run.
Before any queue change, a script checks real posting times, length, quiet hours and links.
Before a Claude call, reserve the worst case against the user's budget: max_tokens times the output price.
max_tokens 4,096:
Opus 5.5: $0.082
Sonnet 5.5: $0.041
Haiku 5.5: $0.002
Settle to real usage after the response. Lower max_tokens, more parallel calls under a cap.
Claude Haiku 5.5 bills a prompt over 100,000 tokens at long-prompt prices, for the whole request.
99,000 tokens in, 1,000 out: $0.0104
101,000 tokens in, 1,000 out: $0.053
2% more context, 5x the cost. If your retrieval step can overshoot, cap it below the line.
10,000 document summaries, 2,000 tokens in and 300 out each:
Sonnet 5.5: $70
Sonnet 5.5, Batch API: $35
Haiku 5.5: $3.50
Haiku 5.5, Batch API: $1.75
For background work the model choice moves the bill 20x. Batching then halves what is left.
Per-user cost tracking for a Claude agent is less code than it sounds.
Every API response reports four token counts: input, output, cache write, cache read. Store one row per model call with the user id, the model and those four numbers.
Cost is then four multiplications.
A Claude agent loop sends its prefix again on every turn. 8,000 tokens of system prompt and tools, 10 turns on Sonnet 5.5:
no cache: $0.16
with cache: $0.027 (one write, nine reads)
That is the prefix alone. Put the cache breakpoint after the tools and keep their order fixed.
We opened sales for Agentwright Starter this week: the Next.js codebase behind the posts on this account, with metering, billing, auth and the agent loop already wired.
If you build on the Claude API, we'd like to hear what a kit like this is still missing. Link in profile.
Three days of posting from a brand-new X account that started with 0 followers. Real numbers:
7 timeline posts: 9 impressions each on average
2 posts in X Communities: 46 and 13
Likes: 0
Small numbers, but communities are how a new account gets read by non-followers.