grok 0.1.7: a 429 shows a spent Rate limit window on the card until it lifts (H20 on Discord)
Grok's billing (/v1/billing?format=credits) says nothing of a plan's rate limit: a free account read 0% used, 100 left, while its requests came back 429. The plugin now keeps an account's last 429 (by its CLI home) and usage shows a "Rate limit" window at 100%, aside, before the period's, until the reset the 429 named (Retry-After in seconds or as a date, or a *ratelimit*reset* header), or, with none named, until a request goes through or 5 minutes have passed. magpie's gateway already rests the account; this is what the card says.
This commit is contained in:
@@ -71,3 +71,15 @@ leaves these out:
|
||||
Grok 4.7 until signed in. Signed in, the models are the ones the account
|
||||
lists at `/v1/models` (Responses models only), each with its context
|
||||
window and its reasoning efforts as variants.
|
||||
|
||||
## Usage
|
||||
|
||||
magpie's card shows the credits of the current period
|
||||
(`/v1/billing?format=credits`): weekly for SuperGrok, with on-demand
|
||||
spending beside it when it has a cap.
|
||||
|
||||
Grok's billing says nothing of a plan's rate limit, so a free account can
|
||||
read 0% used while its requests come back 429. After a 429 the card shows
|
||||
a spent "Rate limit" window until the reset it named (`Retry-After`, or a
|
||||
`*ratelimit*reset*` header). When it named none, the window stays until a
|
||||
request goes through, or for 5 minutes.
|
||||
|
||||
+58
-3
@@ -451,6 +451,57 @@ async function usage(key) {
|
||||
return { windows: out }
|
||||
}
|
||||
|
||||
// ---- rate limit -------------------------------------------------------------------
|
||||
|
||||
// Grok's billing says nothing of a plan's rate limit: a free account's
|
||||
// credits read 0% used while every request comes back 429. So the account's
|
||||
// last 429 is kept here, by its CLI home, and usage shows a "Rate limit"
|
||||
// window spent until the reset the 429 named (Retry-After, or a
|
||||
// *ratelimit*reset* header), or, when it named none, until a request goes
|
||||
// through or HOLD_MS has passed. Aside: magpie's gateway already rests an
|
||||
// account that answered 429; this is only what the card says.
|
||||
const HOLD_MS = 5 * 60_000
|
||||
const limited = new Map()
|
||||
|
||||
// resetOf is when a 429 says the limit lifts, in ms since the epoch, or 0.
|
||||
function resetOf(headers, now) {
|
||||
const ra = headers.get("retry-after")
|
||||
if (ra) {
|
||||
const n = Number(ra)
|
||||
if (Number.isFinite(n) && n >= 0) return now + n * 1000
|
||||
const d = Date.parse(ra)
|
||||
if (!Number.isNaN(d)) return d
|
||||
}
|
||||
for (const [k, v] of headers) {
|
||||
if (!/ratelimit.*reset/i.test(k)) continue
|
||||
const n = Number(v)
|
||||
if (!Number.isFinite(n) || n < 0) continue
|
||||
// a time (epoch ms or seconds) or seconds from now
|
||||
if (n > 1e12) return n
|
||||
if (n > 1e9) return n * 1000
|
||||
return now + n * 1000
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// note keeps what a request's answer says of the rate limit.
|
||||
function note(home, res, now = Date.now()) {
|
||||
if (res.status === 429) limited.set(home, { at: now, until: resetOf(res.headers, now) })
|
||||
else if (res.ok) limited.delete(home)
|
||||
}
|
||||
|
||||
// rateWindow is the window a recent 429 makes, or none.
|
||||
function rateWindow(home, now = Date.now()) {
|
||||
const l = limited.get(home)
|
||||
if (!l) return []
|
||||
if (l.until ? l.until <= now : now - l.at >= HOLD_MS) {
|
||||
limited.delete(home)
|
||||
return []
|
||||
}
|
||||
const end = l.until || l.at + HOLD_MS
|
||||
return [{ name: "Rate limit", used: 100, resetsAt: new Date(end).toISOString(), aside: true }]
|
||||
}
|
||||
|
||||
// kept is res saying magpie is to leave the account's sign-in be.
|
||||
function kept(res) {
|
||||
const headers = new Headers(res.headers)
|
||||
@@ -540,7 +591,9 @@ export const GrokAuthPlugin = async ({ client }) => {
|
||||
// sent once, as the built-in sent it: Grok's answer goes on as
|
||||
// it came, the account kept, as the built-in never marked a Grok
|
||||
// account lapsed nor cleared one
|
||||
return kept(await fetch(req.url, { ...init, method: req.method, headers, body }))
|
||||
const res = await fetch(req.url, { ...init, method: req.method, headers, body })
|
||||
note(home, res)
|
||||
return kept(res)
|
||||
},
|
||||
}
|
||||
},
|
||||
@@ -569,7 +622,9 @@ export const GrokAuthPlugin = async ({ client }) => {
|
||||
return { error: e.message, windows: [], signIn: "kept" }
|
||||
}
|
||||
await remember(auth, c)
|
||||
return { ...(await usage(c.key)), signIn: "kept" }
|
||||
const u = await usage(c.key)
|
||||
if (!u.error) u.windows = [...rateWindow(auth.refresh), ...u.windows]
|
||||
return { ...u, signIn: "kept" }
|
||||
},
|
||||
},
|
||||
|
||||
@@ -609,4 +664,4 @@ export const GrokAuthPlugin = async ({ client }) => {
|
||||
}
|
||||
|
||||
// for tests
|
||||
export const _internal = { rewrite, bodyText }
|
||||
export const _internal = { rewrite, bodyText, limited, HOLD_MS }
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-grok-auth",
|
||||
"version": "0.1.6",
|
||||
"version": "0.1.7",
|
||||
"description": "OpenCode provider plugin: Grok (SuperGrok / X Premium+) through the Grok Build CLI's sign-in",
|
||||
"type": "module",
|
||||
"main": "./index.mjs",
|
||||
|
||||
@@ -111,6 +111,60 @@ test("a model list Grok couldn't give fails, not falls back to the configured fe
|
||||
await expect(hooks.provider.models({ id: "grok", models: { "grok-4.7": { id: "grok-4.7" } } }, { auth })).rejects.toThrow()
|
||||
})
|
||||
|
||||
// a free account's billing reads 0% used while its requests are 429'd
|
||||
// (H20 on Discord): the 429 is what the card shows, until it lifts
|
||||
test("a request answered 429 shows a spent Rate limit window until its reset", async () => {
|
||||
const billing = () => Response.json({ config: { currentPeriod: { type: "USAGE_PERIOD_TYPE_WEEKLY", end: "2026-10-12T02:17:59Z" } } })
|
||||
const week = { name: "7 days", used: 0, span: 604800, resetsAt: "2026-10-12T02:17:59Z" }
|
||||
const hooks = await GrokAuthPlugin({ client: { auth: { set: async () => {} } } })
|
||||
const opts = await hooks.auth.loader(async () => auth)
|
||||
const send = async (res) => {
|
||||
globalThis.fetch = async () => res
|
||||
return opts.fetch("https://cli-chat-proxy.grok.com/v1/responses", { method: "POST", body: JSON.stringify({ model: "grok-4.7", input: "hi" }) })
|
||||
}
|
||||
const card = async () => {
|
||||
globalThis.fetch = async () => billing()
|
||||
return (await hooks.auth.usage(async () => auth)).windows
|
||||
}
|
||||
expect(await card()).toEqual([week])
|
||||
|
||||
// Retry-After in seconds
|
||||
const t0 = Date.now()
|
||||
expect((await send(new Response("You've hit the rate limit for your plan.", { status: 429, headers: { "Retry-After": "120" } }))).status).toBe(429)
|
||||
let ws = await card()
|
||||
expect(ws.length).toBe(2)
|
||||
expect(ws[0]).toMatchObject({ name: "Rate limit", used: 100, aside: true })
|
||||
const at = Date.parse(ws[0].resetsAt)
|
||||
expect(at).toBeGreaterThanOrEqual(t0 + 119_000)
|
||||
expect(at).toBeLessThanOrEqual(Date.now() + 121_000)
|
||||
expect(ws[1]).toEqual(week)
|
||||
|
||||
// a request that goes through lifts it
|
||||
await send(Response.json({ ok: true }))
|
||||
expect(await card()).toEqual([week])
|
||||
|
||||
// a reset header in epoch seconds
|
||||
const reset = Math.floor(Date.now() / 1000) + 600
|
||||
await send(new Response("", { status: 429, headers: { "x-ratelimit-reset-requests": String(reset) } }))
|
||||
expect((await card())[0]).toMatchObject({ name: "Rate limit", resetsAt: new Date(reset * 1000).toISOString() })
|
||||
|
||||
// a reset passed is gone; none named holds HOLD_MS from the 429
|
||||
const { limited, HOLD_MS } = _internal
|
||||
limited.get(home).until = Date.now() - 1
|
||||
expect(await card()).toEqual([week])
|
||||
await send(new Response("", { status: 429 }))
|
||||
ws = await card()
|
||||
expect(Date.parse(ws[0].resetsAt) - limited.get(home).at).toBe(HOLD_MS)
|
||||
limited.get(home).at -= HOLD_MS
|
||||
expect(await card()).toEqual([week])
|
||||
|
||||
// another answer that failed leaves it as it was
|
||||
await send(new Response("", { status: 429 }))
|
||||
await send(new Response("", { status: 500 }))
|
||||
expect((await card())[0].name).toBe("Rate limit")
|
||||
limited.clear()
|
||||
})
|
||||
|
||||
test("a body in bytes is reshaped as a string one is", () => {
|
||||
const { rewrite, bodyText } = _internal
|
||||
const chat = { model: "grok-4.7", tools: [{ type: "function", name: "f" }, { type: "custom", name: "apply_patch" }] }
|
||||
|
||||
Reference in New Issue
Block a user