v0.10.5

Balance and Quota

Last updated


Which provider packages expose a Usage function and which UsageRemaining fields each one fills.

Signature


Every package below exports the same function:
  1. func Usage(ctx context.Context, config llmrouter.Config) (llmrouter.UsageRemaining, error)

  1. type UsageRemaining struct {
  2. FiveHour *float64 `json:"five_hour,omitempty"` // percentage left in the 5-hour window
  3. Week *float64 `json:"week,omitempty"` // percentage left in the weekly window
  4. Balance *float64 `json:"balance,omitempty"` // account balance or credits left
  5. Total *float64 `json:"total,omitempty"` // percentage left of the overall quota
  6. }

Each provider sets only the fields it reports; the rest stay nil, so check a field before dereferencing it.

Fields per package


PackageCredentialsFields set
core/openRouterAPIKeyBalance = total_credits - total_usage
core/deepseekAPIKeyBalance = first balance_infos[].total_balance
core/copilotTokenTotal = percent_remaining of chat, or of premium_interactions when the chat entitlement is 0
core/openaiCodexAPIKey, optional AccountIDFiveHour / Week = 100 - used_percent of the primary / secondary rate-limit window
core/grokOauthAPIKeyTotal = remaining credit percentage
core/claudeCodenone (uses the logged-in claude CLI)FiveHour / Week = 100 - the Current session / Current week (all models) percentage from claude -p /usage

The Go package names for core/openRouter, core/openaiCodex, and core/grokOauth are openrouter, openaicodex, and grokoauth. A missing credential returns an error before any request is sent. claudeCode.Usage returns an error when the CLI is missing or its output has no subscription limits. Usage in core/ollamaCloud was removed in v0.9.3; see Removed APIs.

Example


  1. remaining, err := openaicodex.Usage(ctx, llmrouter.Config{APIKey: token.AccessToken, AccountID: token.AccountID})
  2. if err != nil {
  3. return err
  4. }
  5. if remaining.FiveHour != nil && remaining.Week != nil {
  6. fmt.Printf("5h: %.0f%% left, week: %.0f%% left\n", *remaining.FiveHour, *remaining.Week)
  7. }


中文