async function fetchWithBackoff(url, opts, attempt = 0) {
const r = await fetch(url, opts)
if (r.status !== 429) return r
const { retry_after = 60 } = await r.json().catch(() => ({}))
if (attempt >= 3) throw new Error('rate_limited')
const wait = (retry_after + Math.random() * 5) * 1000
await new Promise(res => setTimeout(res, wait))
return fetchWithBackoff(url, opts, attempt + 1)
}