Skip to content

feat(claude): add rate_limits support for subscription usage#7428

Closed
AbdelrahmanHafez wants to merge 1 commit intoJanDeDobbeleer:mainfrom
AbdelrahmanHafez:feat/claude-rate-limits
Closed

feat(claude): add rate_limits support for subscription usage#7428
AbdelrahmanHafez wants to merge 1 commit intoJanDeDobbeleer:mainfrom
AbdelrahmanHafez:feat/claude-rate-limits

Conversation

@AbdelrahmanHafez
Copy link
Copy Markdown

Prerequisites

  • I have read and understood the contributing guide.
  • The commit message follows the conventional commits guidelines.
  • Tests for the changes have been added (for bug fixes / features).
  • Docs have been added/updated (for bug fixes / features).

Description

Adds support for the rate_limits field 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:

  • ClaudeRateLimits with optional FiveHour and SevenDay windows
  • ClaudeRateWindow with UsedPercentage (float64) and ResetsAt (unix epoch seconds)

New template properties:

  • .FiveHourPercent / .SevenDayPercent - return Percentage type (supports .String(), .Gauge(), .GaugeUsed())
  • .HasRateLimits / .HasFiveHourLimit / .HasSevenDayLimit - for conditional display

Example template usage:

{{ if .HasRateLimits }} 5h:{{ .FiveHourPercent.String }}%{{ if .HasSevenDayLimit }} 7d:{{ .SevenDayPercent.String }}%{{ end }} {{ end }}

The rate_limits field is optional in the JSON and each window (five_hour, seven_day) is independently optional, so existing configs are unaffected.

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.
Copilot AI review requested due to automatic review settings March 27, 2026 23:44
@AbdelrahmanHafez AbdelrahmanHafez marked this pull request as ready for review March 27, 2026 23:48
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 / ClaudeRateWindow and 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 TokenUsagePercent as a Percentage, but the segment also exposes FiveHourPercent and SevenDayPercent as Percentage values. Update the wording so users know these properties support the same .Gauge/.GaugeUsed/.String methods.
### Percentage Methods

The `TokenUsagePercent` property is a `Percentage` type that provides additional functionality:

Comment on lines +212 to +216
percent := int(c.RateLimits.SevenDay.UsedPercentage + 0.5)
if percent > 100 {
return 100
}

Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +225 to +228
// 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
}
Copy link

Copilot AI Mar 27, 2026

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.

Copilot uses AI. Check for mistakes.

// HasRateLimits returns true when rate limit data is available.
func (c *Claude) HasRateLimits() bool {
return c.RateLimits != nil
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
return c.RateLimits != nil
return c.HasFiveHourLimit() || c.HasSevenDayLimit()

Copilot uses AI. Check for mistakes.
Comment on lines +197 to +201
percent := int(c.RateLimits.FiveHour.UsedPercentage + 0.5)
if percent > 100 {
return 100
}

Copy link

Copilot AI Mar 27, 2026

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_percentage is ever < 0 (which then affects Percentage.String() output). Consider clamping to the full valid range (0-100) after rounding, similar to TokenUsagePercent.

Copilot uses AI. Check for mistakes.
@AbdelrahmanHafez
Copy link
Copy Markdown
Author

#7412 supersedes this PR, closing this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants