feat(PBI-66): wekelijkse sync van model_prices via Anthropic /v1/models (#167)

Nieuw script `npm run db:sync-model-prices` haalt de actuele Claude 4.x
modellijst op bij de Anthropic API en upsert prijzen in `model_prices`.
Anthropic biedt geen prijs-API, dus prijzen blijven onderhouden in een
PRICE_TABLE constante in het script. Cache-tier-prijzen worden afgeleid
via vaste multipliers (read 0.1x, write 1.25x). Nieuwe Claude 4.x modellen
worden gedetecteerd en gelogd als warning zodat duidelijk is wanneer de
tabel handmatig moet worden bijgewerkt.

- scripts/sync-model-prices.ts: idempotent upsert, --dry-run, retry op 5xx
- ANTHROPIC_API_KEY als optional env-var (.env.example, lib/env.ts)
- scripts/README.md: gebruiksinstructies + edge cases
- docs/plans/sync-model-prices.md: ontwerpdocument

Verificatie: `npm run lint`, `vitest` (563/563), TypeScript clean.
Echt gedraaid tegen DB: 3 created (eerste run) -> 3 unchanged (tweede run).

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Janpeter Visser 2026-05-08 09:38:33 +02:00 committed by GitHub
parent 8a6b2d2cb3
commit eaabec8471
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 443 additions and 1 deletions

View file

@ -1,4 +1,57 @@
# API Test Scripts
# Scripts
## sync-model-prices.ts
Wekelijks handmatig draaibaar script dat de tabel `model_prices` synchroniseert. Haalt de actuele Claude 4.x modellijst op via `GET /v1/models` (Anthropic API) en upsert de prijzen vanuit een hardcoded `PRICE_TABLE` in het script. Anthropic biedt geen prijs-API; bij elke prijswijziging update je de tabel in [`scripts/sync-model-prices.ts`](./sync-model-prices.ts).
### Prerequisites
| What | How |
|---|---|
| `ANTHROPIC_API_KEY` in `.env.local` | Genereer op [console.anthropic.com](https://console.anthropic.com/) → API Keys. Free Evaluation tier is voldoende — `/v1/models` is een gratis metadata-call. |
| `DATABASE_URL` in `.env.local` | Standaard Scrum4Me-setup. |
### Gebruik
```bash
# Eerst droog draaien — toont wat er zou gebeuren, schrijft niets
npm run db:sync-model-prices -- --dry-run
# Echt synchroniseren
npm run db:sync-model-prices
```
### Output
```
Fetching /v1/models from Anthropic API...
→ 12 models received, 4 match Claude 4.x filter
Syncing prices:
✓ claude-opus-4-7 (unchanged)
✓ claude-sonnet-4-6 (unchanged)
✓ claude-haiku-4-5-20251001 (unchanged)
⚠ claude-sonnet-4-9 (geen prijs in PRICE_TABLE — ...)
Result: 0 created, 0 updated, 3 unchanged, 1 skipped
```
### Bij een nieuw model (`⚠ skipped`)
1. Open de Anthropic [pricing-pagina](https://platform.claude.com/docs/en/about-claude/pricing).
2. Voeg het model toe aan `PRICE_TABLE` in [`scripts/sync-model-prices.ts`](./sync-model-prices.ts):
```ts
'claude-sonnet-4-9': { input: 3.0, output: 15.0 },
```
3. Draai het script opnieuw.
### Edge cases
- **API geeft 401**: controleer `ANTHROPIC_API_KEY`.
- **API geeft 5xx**: script doet 1× retry met 2s delay, daarna falen.
- **Model in DB maar niet meer in API**: wordt niet verwijderd — alleen gelogd, zodat oude `claude_jobs` rijen kostberekening blijven hebben.
---
## test-api.sh