-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat(claude): add rate_limits support for subscription usage #7428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -21,6 +21,7 @@ type ClaudeData struct { | |||||
| SessionID string `json:"session_id"` | ||||||
| ContextWindow ClaudeContextWindow `json:"context_window"` | ||||||
| Cost ClaudeCost `json:"cost"` | ||||||
| RateLimits *ClaudeRateLimits `json:"rate_limits,omitempty"` | ||||||
| } | ||||||
|
|
||||||
| // ClaudeModel represents the AI model information | ||||||
|
|
@@ -59,6 +60,18 @@ type ClaudeCurrentUsage struct { | |||||
| CacheReadInputTokens int `json:"cache_read_input_tokens"` | ||||||
| } | ||||||
|
|
||||||
| // ClaudeRateLimits represents Claude.ai subscription usage limits | ||||||
| type ClaudeRateLimits struct { | ||||||
| FiveHour *ClaudeRateWindow `json:"five_hour,omitempty"` | ||||||
| SevenDay *ClaudeRateWindow `json:"seven_day,omitempty"` | ||||||
| } | ||||||
|
|
||||||
| // ClaudeRateWindow represents a single rate limit window | ||||||
| type ClaudeRateWindow struct { | ||||||
| UsedPercentage float64 `json:"used_percentage"` | ||||||
| ResetsAt int64 `json:"resets_at"` | ||||||
| } | ||||||
|
|
||||||
| const ( | ||||||
| thousand = 1000.0 | ||||||
| million = 1000000.0 | ||||||
|
|
@@ -173,3 +186,48 @@ func (c *Claude) FormattedTokens() string { | |||||
|
|
||||||
| return fmt.Sprintf("%.1fM", float64(currentTokens)/million) | ||||||
| } | ||||||
|
|
||||||
| // FiveHourPercent returns the 5-hour rate limit usage as a Percentage. | ||||||
| // Returns 0 when rate limit data is not available. | ||||||
| func (c *Claude) FiveHourPercent() text.Percentage { | ||||||
| if c.RateLimits == nil || c.RateLimits.FiveHour == nil { | ||||||
| return 0 | ||||||
| } | ||||||
|
|
||||||
| percent := int(c.RateLimits.FiveHour.UsedPercentage + 0.5) | ||||||
| if percent > 100 { | ||||||
| return 100 | ||||||
| } | ||||||
|
|
||||||
| return text.Percentage(percent) | ||||||
| } | ||||||
|
|
||||||
| // SevenDayPercent returns the 7-day rate limit usage as a Percentage. | ||||||
| // Returns 0 when rate limit data is not available. | ||||||
| func (c *Claude) SevenDayPercent() text.Percentage { | ||||||
| if c.RateLimits == nil || c.RateLimits.SevenDay == nil { | ||||||
| return 0 | ||||||
| } | ||||||
|
|
||||||
| percent := int(c.RateLimits.SevenDay.UsedPercentage + 0.5) | ||||||
| if percent > 100 { | ||||||
| return 100 | ||||||
| } | ||||||
|
|
||||||
|
Comment on lines
+212
to
+216
|
||||||
| return text.Percentage(percent) | ||||||
| } | ||||||
|
|
||||||
| // HasRateLimits returns true when rate limit data is available. | ||||||
| func (c *Claude) HasRateLimits() bool { | ||||||
| return c.RateLimits != nil | ||||||
|
||||||
| return c.RateLimits != nil | |
| return c.HasFiveHourLimit() || c.HasSevenDayLimit() |
Copilot
AI
Mar 27, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are unit tests for HasRateLimits, but none for the newly added HasFiveHourLimit / HasSevenDayLimit helpers. Adding simple table-driven tests would guard the template-facing API behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The float-to-int conversion here doesn’t clamp the lower bound and can yield negative percentages if
used_percentageis ever < 0 (which then affectsPercentage.String()output). Consider clamping to the full valid range (0-100) after rounding, similar toTokenUsagePercent.