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 ¶
- type AccountPollers
- type Extra
- type Fetcher
- type MoneyBucket
- type MultiPoller
- func (m *MultiPoller) GetAll(ctx context.Context) (map[string]*Snapshot, map[string]ProviderStatus)
- func (m *MultiPoller) GetProvider(ctx context.Context, provider string) (*Snapshot, error)
- func (m *MultiPoller) ObserveRateLimit(provider, authKind string, fiveHour, sevenDay float64)
- func (m *MultiPoller) StaticProviderStatus(provider string) (ProviderStatus, bool)
- type Plan
- type Poller
- type ProviderStatus
- type Quota
- type Snapshot
- type TokenFunc
- type Window
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AccountPollers ¶ added in v0.49.0
type AccountPollers struct {
// New builds the poller for one account.
New func(key string) *Poller
// contains filtered or unexported fields
}
AccountPollers polls the plan usage of each account of a chain on its own cache, keyed by an opaque account identity. A poller is created on first use and only when somebody asks: nothing here runs on a timer, and the token functions it is built from never refresh a credential.
func (*AccountPollers) Get ¶ added in v0.49.0
Get returns the account's usage snapshot, like Poller.Get.
func (*AccountPollers) Retain ¶ added in v0.49.0
func (a *AccountPollers) Retain(keep []string)
Retain forgets the pollers of accounts that are no longer in keep.
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 ¶
CurrencySymbol maps the ISO currency code to a display symbol, falling back to the code itself (or "$" when unreported).
func (Extra) MonthlyLimitAmount ¶
MonthlyLimitAmount returns the extra-usage cap in major currency units. ok is false when the value is not reported.
func (Extra) UsedAmount ¶
UsedAmount returns the extra-usage spend in major currency units. ok is false when the value is not reported.
type MoneyBucket ¶ added in v0.26.0
type MoneyBucket struct {
ID string `json:"id"`
Label string `json:"label"`
Used *int64 `json:"used_minor,omitempty"`
Limit *int64 `json:"limit_minor,omitempty"`
Remaining *int64 `json:"remaining_minor,omitempty"`
Currency string `json:"currency,omitempty"`
Decimals *int `json:"decimals,omitempty"`
}
MoneyBucket is an amount in minor units. Each pointer distinguishes an actual zero from an unreported value.
type MultiPoller ¶ added in v0.26.0
type MultiPoller struct {
Pollers map[string]*Poller
StaticStatus map[string]ProviderStatus
// Accounts and ServingAccount make Anthropic's snapshot the one of the
// account being spent when the plan has several sign-ins. ServingAccount
// returns its identity and 1-based place in the chain; without it (the
// environment manages the credential, no sign-in) the "anthropic" poller
// answers as before.
Accounts *AccountPollers
ServingAccount func() (key string, position int, ok bool)
// contains filtered or unexported fields
}
MultiPoller serves independent provider caches. A failed provider does not hide healthy providers.
func (*MultiPoller) GetAll ¶ added in v0.26.0
func (m *MultiPoller) GetAll(ctx context.Context) (map[string]*Snapshot, map[string]ProviderStatus)
func (*MultiPoller) GetProvider ¶ added in v0.26.0
func (*MultiPoller) ObserveRateLimit ¶ added in v0.27.0
func (m *MultiPoller) ObserveRateLimit(provider, authKind string, fiveHour, sevenDay float64)
ObserveRateLimit records the latest account-wide plan windows reported by a provider response. Unknown windows (< 0) preserve their previous value, so a partial header set cannot erase useful telemetry from an earlier request. Values are fractions in [0,1], matching core.RateLimit.
func (*MultiPoller) StaticProviderStatus ¶ added in v0.26.0
func (m *MultiPoller) StaticProviderStatus(provider string) (ProviderStatus, bool)
StaticProviderStatus returns a provider state that is known without polling, such as an API-key account whose consumer-plan usage is unsupported.
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 NewPollerWithClient ¶ added in v0.49.0
NewPollerWithClient is NewPoller with the HTTP client the usage endpoint is asked through; tests pass one that never leaves the process.
func NewProviderPoller ¶ added in v0.26.0
NewProviderPoller creates a generic provider poller. NewPoller remains the Anthropic compatibility constructor.
func (*Poller) Get ¶
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 ProviderStatus ¶ added in v0.26.0
type ProviderStatus struct {
Available bool `json:"available"`
AuthKind string `json:"auth_kind,omitempty"`
// Account and AccountID say which account of a chain the snapshot is for
// (its 1-based place and its opaque identity), when there are several.
Account int `json:"account,omitempty"`
AccountID string `json:"account_id,omitempty"`
Reason string `json:"reason,omitempty"`
Error string `json:"error,omitempty"`
}
Status is returned even without a snapshot, so UIs never have to infer an API-key credential from absence. Values: pending, temporarily_unavailable, unsupported.
type Quota ¶ added in v0.26.0
type Quota struct {
ID string `json:"id"`
Label string `json:"label"`
Utilization *float64 `json:"utilization,omitempty"`
PeriodKind string `json:"period_kind,omitempty"`
PeriodStart *time.Time `json:"period_start,omitempty"`
PeriodEnd *time.Time `json:"period_end,omitempty"`
}
Quota is a provider-neutral plan meter. Utilization is nil when the provider reports a period but not its consumption (zero is therefore preserved).
type Snapshot ¶
type Snapshot struct {
Provider string `json:"provider,omitempty"`
AuthKind string `json:"auth_kind,omitempty"`
Plan Plan `json:"plan,omitempty"`
Quotas []Quota `json:"quotas,omitempty"`
Money []MoneyBucket `json:"money,omitempty"`
FetchedAt time.Time `json:"fetched_at"`
Stale bool `json:"stale,omitempty"`
Stability string `json:"stability,omitempty"`
FiveHour *Window `json:"five_hour,omitempty"`
SevenDay *Window `json:"seven_day,omitempty"`
SevenDayOpus *Window `json:"seven_day_opus,omitempty"`
SevenDaySonnet *Window `json:"seven_day_sonnet,omitempty"`
Extra Extra `json:"extra_usage,omitempty"`
}
Snapshot is the generic, provider-qualified usage contract. The legacy Anthropic fields remain during migration for old REST clients and callers.
func FetchXAI ¶ added in v0.26.0
FetchXAI retrieves best-effort consumer plan data. It intentionally accepts only an OAuth token; callers must not invoke it for API-key authentication.
func (*Snapshot) NormalizeAnthropic ¶ added in v0.26.0
func (s *Snapshot) NormalizeAnthropic()
NormalizeAnthropic fills the generic fields while retaining the wire fields consumed by older clients.