Documentation
¶
Overview ¶
Package usage reads Codex rate-limit status from the ChatGPT backend.
Index ¶
Constants ¶
const ( RedeemReset = "reset" RedeemNothingToReset = "nothing_to_reset" RedeemNoCredit = "no_credit" RedeemAlreadyRedeemed = "already_redeemed" )
Outcomes of redeeming a usage limit reset.
const DefaultBaseURL = "https://chatgpt.com/backend-api"
DefaultBaseURL is the ChatGPT backend used by the Codex CLI.
Variables ¶
ErrUnauthorized means the access token was rejected.
Functions ¶
This section is empty.
Types ¶
type AdditionalRateLimit ¶
type AdditionalRateLimit struct {
LimitName string `json:"limit_name"`
MeteredFeature string `json:"metered_feature"`
RateLimit *RateLimit `json:"rate_limit"`
}
AdditionalRateLimit is a separately metered limit, e.g. for one model.
type Client ¶
Client fetches usage from the ChatGPT backend.
func (*Client) FetchResetCredits ¶
func (c *Client) FetchResetCredits(ctx context.Context, creds Credentials) (*ResetCredits, error)
FetchResetCredits lists the usage limit resets a workspace has earned.
func (*Client) Redeem ¶
func (c *Client) Redeem(ctx context.Context, creds Credentials, requestID, creditID string) (*RedeemResult, error)
Redeem spends a usage limit reset, clearing the workspace's current limits. requestID is an idempotency key: retrying with the same ID never spends a second reset. An empty creditID lets the server pick the reset.
type Credentials ¶
Credentials authorize a usage request for one workspace.
type Credits ¶
type Credits struct {
HasCredits bool `json:"has_credits"`
Unlimited bool `json:"unlimited"`
Balance flexString `json:"balance"`
}
Credits is the purchased-credit balance.
type RateLimit ¶
type RateLimit struct {
Allowed bool `json:"allowed"`
LimitReached bool `json:"limit_reached"`
PrimaryWindow *Window `json:"primary_window"`
SecondaryWindow *Window `json:"secondary_window"`
}
RateLimit is the state of a limit and its windows.
type RedeemResult ¶
RedeemResult is the outcome of redeeming a usage limit reset.
type ResetCredit ¶
type ResetCredit struct {
ID string `json:"id"`
ResetType string `json:"reset_type"`
Status string `json:"status"` // available, redeeming or redeemed
GrantedAt string `json:"granted_at"`
ExpiresAt string `json:"expires_at"` // RFC 3339; empty if it never expires
Title string `json:"title"`
Description string `json:"description"`
}
ResetCredit is one earned usage limit reset.
type ResetCredits ¶
type ResetCredits struct {
Credits []ResetCredit `json:"credits"`
AvailableCount int64 `json:"available_count"`
TotalEarnedCount int64 `json:"total_earned_count"`
}
ResetCredits is the list of usage limit resets from /wham/rate-limit-reset-credits. Redeeming one clears the current limits.
func (*ResetCredits) Available ¶
func (r *ResetCredits) Available() []ResetCredit
Available returns the redeemable credits, soonest to expire first.
type Response ¶
type Response struct {
PlanType string `json:"plan_type"`
RateLimit *RateLimit `json:"rate_limit"`
CodeReviewRateLimit *RateLimit `json:"code_review_rate_limit"`
AdditionalRateLimits []AdditionalRateLimit `json:"additional_rate_limits"`
Credits *Credits `json:"credits"`
SpendControl *struct {
Reached bool `json:"reached"`
} `json:"spend_control"`
RateLimitReachedType *struct {
Type string `json:"type"`
} `json:"rate_limit_reached_type"`
RateLimitResetCredits *struct {
AvailableCount int64 `json:"available_count"`
} `json:"rate_limit_reset_credits"`
}
Response is the subset of /wham/usage this tool reads. Every nested value may be null or missing.
func (*Response) AvailableResets ¶
AvailableResets is the number of usage limit resets the account can redeem.
type StatusError ¶
StatusError is an unexpected HTTP status from the backend.
func (*StatusError) Error ¶
func (e *StatusError) Error() string
type Window ¶
type Window struct {
UsedPercent float64 `json:"used_percent"`
LimitWindowSeconds int64 `json:"limit_window_seconds"`
ResetAfterSeconds int64 `json:"reset_after_seconds"`
ResetAt int64 `json:"reset_at"`
}
Window is one rate-limit window, e.g. the rolling 5 hours or the week.
func (*Window) Label ¶
Label names the window by its length: "5h", "weekly", "daily", or a duration.
func (*Window) LeftPercent ¶
LeftPercent is the share of the window still available, clamped to 0–100.