feat(claude): add rate_limits support for subscription usage#7428
feat(claude): add rate_limits support for subscription usage#7428AbdelrahmanHafez wants to merge 1 commit intoJanDeDobbeleer:mainfrom
Conversation
Add support for the rate_limits field from Claude Code's statusline JSON, which provides 5-hour and 7-day usage window percentages for subscribers. New template properties: FiveHourPercent, SevenDayPercent, HasRateLimits, HasFiveHourLimit, HasSevenDayLimit.
There was a problem hiding this comment.
Pull request overview
Adds support in the Claude Code segment for the new rate_limits statusline JSON field (Claude Code ≥ 2.1.80) to expose subscriber 5-hour and 7-day usage percentages and related template helpers.
Changes:
- Extend Claude status JSON model with
ClaudeRateLimits/ClaudeRateWindowand expose new template-facing helpers (FiveHourPercent,SevenDayPercent,Has*). - Add unit tests covering percentage rounding/capping behavior and
HasRateLimits. - Update segment documentation to describe the new properties and nested structures.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/segments/claude.go |
Adds rate limit structs, JSON field, and template methods for 5h/7d usage and availability checks. |
src/segments/claude_test.go |
Adds tests for the new percentage helpers and HasRateLimits. |
website/docs/segments/cli/claude.mdx |
Documents new template properties and the RateLimits / RateWindow structures. |
Comments suppressed due to low confidence (1)
website/docs/segments/cli/claude.mdx:115
- This section now only mentions
TokenUsagePercentas aPercentage, but the segment also exposesFiveHourPercentandSevenDayPercentasPercentagevalues. Update the wording so users know these properties support the same.Gauge/.GaugeUsed/.Stringmethods.
### Percentage Methods
The `TokenUsagePercent` property is a `Percentage` type that provides additional functionality:
| percent := int(c.RateLimits.SevenDay.UsedPercentage + 0.5) | ||
| if percent > 100 { | ||
| return 100 | ||
| } | ||
|
|
There was a problem hiding this comment.
Same as FiveHourPercent: after rounding, clamp the result to the full valid range (0-100) so templates using .SevenDayPercent.String can’t emit negative values if unexpected data is received.
| // HasFiveHourLimit returns true when the 5-hour rate limit window is available. | ||
| func (c *Claude) HasFiveHourLimit() bool { | ||
| return c.RateLimits != nil && c.RateLimits.FiveHour != nil | ||
| } |
There was a problem hiding this comment.
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.
|
|
||
| // HasRateLimits returns true when rate limit data is available. | ||
| func (c *Claude) HasRateLimits() bool { | ||
| return c.RateLimits != nil |
There was a problem hiding this comment.
HasRateLimits currently returns true whenever the rate_limits object is present, even if both windows are nil (e.g. JSON contains rate_limits: {}), which would make {{ if .HasRateLimits }} blocks render 0% values. Consider defining this as “at least one window is available” (e.g., HasFiveHourLimit || HasSevenDayLimit) to match the intent of conditional display.
| return c.RateLimits != nil | |
| return c.HasFiveHourLimit() || c.HasSevenDayLimit() |
| percent := int(c.RateLimits.FiveHour.UsedPercentage + 0.5) | ||
| if percent > 100 { | ||
| return 100 | ||
| } | ||
|
|
There was a problem hiding this comment.
The float-to-int conversion here doesn’t clamp the lower bound and can yield negative percentages if used_percentage is ever < 0 (which then affects Percentage.String() output). Consider clamping to the full valid range (0-100) after rounding, similar to TokenUsagePercent.
|
#7412 supersedes this PR, closing this one. |
Prerequisites
Description
Adds support for the
rate_limitsfield from Claude Code's statusline JSON (added in Claude Code 2.1.80). This provides 5-hour and 7-day usage window percentages for Claude.ai subscribers.New structs:
ClaudeRateLimitswith optionalFiveHourandSevenDaywindowsClaudeRateWindowwithUsedPercentage(float64) andResetsAt(unix epoch seconds)New template properties:
.FiveHourPercent/.SevenDayPercent- returnPercentagetype (supports.String(),.Gauge(),.GaugeUsed()).HasRateLimits/.HasFiveHourLimit/.HasSevenDayLimit- for conditional displayExample template usage:
The
rate_limitsfield is optional in the JSON and each window (five_hour,seven_day) is independently optional, so existing configs are unaffected.