MCPcopy Create free account
hub / github.com/arctic-cli/interface / fetchAnthropicUsage

Function fetchAnthropicUsage

packages/arctic/src/provider/usage.ts:589–658  ·  view source on GitHub ↗
(input: {
    provider: Provider.Info
  })

Source from the content-addressed store, hash-verified

587 }
588
589 async function fetchAnthropicUsage(input: {
590 provider: Provider.Info
591 }): Promise<Omit<Record, "providerID" | "providerName" | "fetchedAt">> {
592 let token: string | undefined
593 const auth = await Auth.get(input.provider.id)
594
595 if (auth?.type === "api") {
596 token = auth.key
597 } else if (auth?.type === "oauth") {
598 token = await ensureAnthropicAccessToken(auth, input.provider.id)
599 } else if (auth?.type === "wellknown") {
600 token = auth.token || auth.key
601 }
602
603 if (!token) {
604 token = input.provider.key ?? input.provider.options?.apiKey
605 }
606
607 if (!token) {
608 throw new Error("Anthropic authentication is required. Set an OAuth token or API key for provider 'anthropic'.")
609 }
610
611 const response = await globalThis.fetch(ANTHROPIC_OAUTH_USAGE_URL, {
612 headers: {
613 Authorization: `Bearer ${token}`,
614 Accept: "application/json",
615 "Content-Type": "application/json",
616 "anthropic-beta": "oauth-2025-04-20",
617 "User-Agent": "Arctic",
618 },
619 })
620
621 if (!response.ok) {
622 if (response.status === 401 || response.status === 403) {
623 throw new Error("Authorization failed. Please check your Anthropic OAuth token.")
624 }
625 throw new Error(`Failed to fetch Anthropic usage: ${response.statusText}`)
626 }
627
628 const payload = (await response.json().catch(() => null)) as any
629 if (!payload || typeof payload !== "object") {
630 return {}
631 }
632
633 const fiveHour = payload.five_hour
634 const sevenDay = payload.seven_day
635
636 const rows = [formatAnthropicUsageRow("5-hour", fiveHour), formatAnthropicUsageRow("7-day", sevenDay)].filter(
637 (row): row is string => Boolean(row),
638 )
639
640 const planType = rows.length > 0 ? `Claude subscription\n${rows.map((row) => `- ${row}`).join("\n")}` : undefined
641
642 const primary = resolveAnthropicLimit(fiveHour, "5h")
643 const secondary = resolveAnthropicLimit(sevenDay, "Weekly")
644
645 const limitReached = [primary?.usedPercent, secondary?.usedPercent].some(
646 (value) => typeof value === "number" && value >= 100,

Callers

nothing calls this directly

Calls 5

formatAnthropicUsageRowFunction · 0.85
resolveAnthropicLimitFunction · 0.85
jsonMethod · 0.65
getMethod · 0.45

Tested by

no test coverage detected