On this page
Fetches Claude API usage data, caches responses with TTL and exponential backoff, and converts between API, cache, and display formats.
#internal/cache
#internal/cache
#CacheTTL
const CacheTTL = 60 * time.SecondCache TTL constants.
#ErrorCacheTTL
const ErrorCacheTTL = 60 * time.Second#MaxErrorCacheTTL
const MaxErrorCacheTTL = 5 * time.Minute#CombinedTimeout
const CombinedTimeout = 7 * time.Second#ErrAuth
var ErrAuth = errors.New("authentication error (401/403)")ErrAuth is returned when the usage API responds with 401 or 403.
#NowMs
var NowMs = func() int64 {NowMs returns the current time in unix milliseconds. Exported for testing.
#UsageResponse
type UsageResponse structUsageResponse is the parsed JSON from the usage API.
#WindowUsage
type WindowUsage structWindowUsage represents a single usage window from the API.
#ExtraUsage
type ExtraUsage structExtraUsage represents extra/overage usage from the API.
#CacheData
type CacheData structCacheData is persisted to .statusline-cache.json.
#CachedWindow
type CachedWindow structCachedWindow stores a usage window in the cache.
#CachedExtra
type CachedExtra structCachedExtra stores extra usage in the cache.
#UsageResult
type UsageResult structUsageResult is what GetUsageData returns to callers.
#WindowResult
type WindowResult structWindowResult represents a single usage window for display.
#ExtraResult
type ExtraResult structExtraResult represents extra usage for display.
#FetchUsageFromAPI
func FetchUsageFromAPI(accessToken string) (*UsageResponse, error)FetchUsageFromAPI calls the usage API with the given access token. Returns ErrAuth for 401/403, generic error for other failures.
#ReadCache
func ReadCache(claudeDir string) *CacheDataReadCache reads the cache file from claudeDir. Returns nil on any error.
#WriteCache
func WriteCache(claudeDir string, data *CacheData) errorWriteCache atomically writes cache data to the cache file.
#IsCacheValid
func IsCacheValid(cache *CacheData, now int64, forceRefresh bool) boolIsCacheValid determines whether the cached data can be used without refresh.
#GetUsageData
func GetUsageData(claudeDir string, forceRefresh bool) *UsageResultGetUsageData is the main entry point: reads cache, fetches if needed, returns results.
#ParseWindowFromMap
func ParseWindowFromMap(m map[string]interface{}) (percent *float64, resetAtMs *int64)ParseWindowFromMap extracts used_percentage and resets_at from a rate limit window map (five_hour or seven_day). Returns nil for either value if the key is missing or has the wrong type. resets_at is converted from unix seconds to unix milliseconds. Does NOT apply to extra_usage (different shape).
#WriteUsageFromStdin
func WriteUsageFromStdin(claudeDir string, rateLimits map[string]interface{}) errorWriteUsageFromStdin writes rate limit data from stdin directly to cache, bypassing the API. This is the "newer Claude Code" path where rate_limits are provided in the stdin JSON.