usage

package
v0.24.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 2, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package usage fetches Claude subscription plan usage from Anthropic's OAuth usage endpoint — the same data the Claude Code CLI shows via /usage (5-hour session window, weekly window, and pay-as-you-go "extra usage").

This endpoint is NOT part of Anthropic's documented public API; it is the undocumented endpoint the Claude Code CLI uses, reconstructed from community reverse-engineering. Treat its shape as best-effort and degrade gracefully if it changes or disappears.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Extra

type Extra struct {
	IsEnabled     bool     `json:"is_enabled"`
	MonthlyLimit  *float64 `json:"monthly_limit"`
	UsedCredits   *float64 `json:"used_credits"`
	Utilization   *float64 `json:"utilization"`
	Currency      string   `json:"currency"`
	DecimalPlaces *int     `json:"decimal_places"`
}

Extra describes the pay-as-you-go ("extra usage") state. Pointer fields are null when extra usage is disabled or the value is not reported.

MonthlyLimit and UsedCredits are reported in the currency's MINOR units (e.g. cents), with DecimalPlaces giving the scale — so 2219 credits at 2 decimal places is 22.19. Use UsedAmount / MonthlyLimitAmount to get major units.

func (Extra) CurrencySymbol

func (e Extra) CurrencySymbol() string

CurrencySymbol maps the ISO currency code to a display symbol, falling back to the code itself (or "$" when unreported).

func (Extra) MonthlyLimitAmount

func (e Extra) MonthlyLimitAmount() (amount float64, ok bool)

MonthlyLimitAmount returns the extra-usage cap in major currency units. ok is false when the value is not reported.

func (Extra) UsedAmount

func (e Extra) UsedAmount() (amount float64, ok bool)

UsedAmount returns the extra-usage spend in major currency units. ok is false when the value is not reported.

type Poller

type Poller struct {
	// contains filtered or unexported fields
}

Poller caches usage snapshots and refreshes them from the network at most once per minInterval. Concurrent callers single-flight through one fetch. Safe for concurrent use.

func NewPoller

func NewPoller(tokenFn TokenFunc) *Poller

NewPoller creates a Poller. tokenFn supplies the OAuth token on each refresh.

func (*Poller) Get

func (p *Poller) Get(ctx context.Context) (*Snapshot, error)

Get returns the latest usage snapshot, hitting the network only when the cached value is older than minInterval. It returns (nil, nil) when usage tracking is unavailable (no OAuth token). On a transient fetch error it serves the last good snapshot when one exists; otherwise it returns the error.

type Snapshot

type Snapshot struct {
	FiveHour       *Window   `json:"five_hour"`
	SevenDay       *Window   `json:"seven_day"`
	SevenDayOpus   *Window   `json:"seven_day_opus"`
	SevenDaySonnet *Window   `json:"seven_day_sonnet"`
	Extra          Extra     `json:"extra_usage"`
	FetchedAt      time.Time `json:"fetched_at"`
}

Snapshot is a point-in-time view of plan usage. Window pointers are nil when the corresponding window is not reported (e.g. per-model weekly windows with no usage yet).

func Fetch

func Fetch(ctx context.Context, client *http.Client, token string) (*Snapshot, error)

Fetch retrieves a usage snapshot using the given OAuth access token.

type TokenFunc

type TokenFunc func(ctx context.Context) (token string, ok bool, err error)

TokenFunc returns the current Anthropic OAuth access token. ok is false when no OAuth credential is in use (e.g. a plain API key), in which case usage tracking is unavailable and the poller stays inert.

type Window

type Window struct {
	Utilization float64   `json:"utilization"`
	ResetsAt    time.Time `json:"resets_at"`
}

Window is a single rate-limit window (e.g. the 5-hour session window or the weekly window). Utilization is a percentage in [0, 100].

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL