quota

package
v0.94.0-rc.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2026 License: AGPL-3.0 Imports: 27 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package quota reads the subscription quota a provider's own account API reports: the rolling windows a plan is metered in, and how much of each one is spent. It is not what a session cost — that is internal/pricing — and not how full a context window is — that is internal/terminal's usage readout.

Five of the eight providers are read here. Claude Code, Codex and Cursor CLI each poll an undocumented endpoint with the token their own CLI already wrote to disk; opencode reads its Go subscription with the API key `opencode auth login` stored for it. Antigravity keeps its Google login in the OS keyring where nothing outside it can mint a token, so its own CLI is asked instead (antigravity.go). oh-my-pi, Crush and Kiro CLI report no plan anywhere lich can read, and opencode without a Go key has none to report.

Which account is read is a question about one session, never about lich. A session can be spawned from a binary the user configured — a wrapper that exports its own CLAUDE_CONFIG_DIR, or an OAuth token of its own — and the account it spends is then not the one lich's own environment names. So the reading is taken against the environment of the process running in that session's PTY (Account, wired to internal/terminal). A session lich cannot read that from, yet knows runs a binary it did not choose, is reported as StatusUnknown: a gauge drawn for the wrong account is worse than no gauge.

That login is also named where the provider will say: Claude's own profile route, and the OIDC id token Codex writes beside its access token. Neither is a second reading — both ride the cache the windows do. A provider that names nobody reports an empty Plan.Account, which is the gauge lich has always drawn, not a failure.

lich reads those credentials and never writes them. Token rotation belongs to the CLI that owns the login: a refresh whose write-back fails spends the stored refresh token and logs the user out of their agent, which is a far worse outcome than a stale gauge. An expired token is reported as signed out.

Index

Constants

View Source
const (
	StatusOK = "ok"
	// StatusSignedOut is "there is no usable login here" — no credentials file,
	// or the provider rejected the token in it. Both are answered by running the
	// provider's own login, so they are one state, not two.
	StatusSignedOut = "signed-out"
	StatusError     = "error"
	// StatusUnknown withholds readings when the session environment or its
	// selected credential source cannot be read. Never borrow another login.
	StatusUnknown = "unknown"
)

A plan's Status. Anything other than StatusOK carries no windows: the reading failed, and the window says so rather than drawing an empty gauge.

View Source
const NoAccountTokenLogin = "token-login"

NoAccountTokenLogin is the one reason an empty Account has a name: the session's login is a long-lived OAuth token, whose scope keeps the profile route out of reach, so lich never asks who it belongs to. Every other unnamed account is a provider that names nobody, which needs no reason of its own.

Variables

This section is empty.

Functions

func ForgetClaudeLogin

func ForgetClaudeLogin(string) error

ForgetClaudeLogin is the macOS Keychain's alone: elsewhere Claude Code keeps the login in the config directory, which goes with it.

Types

type Account

type Account struct {
	Env  map[string]string
	Read bool
}

Account is what a lich session's own process says about the account it spends. Env is that process's environment — where a wrapper binary puts a config dir or a token of its own — and Read is false when lich could not read it at all. An unreadable session never borrows the machine-wide login.

type Plan

type Plan = api.QuotaPlan

type ReportedLimit

type ReportedLimit struct {
	Kind        string
	PercentUsed float64
	ResetsAt    string
}

ReportedLimit is one rate-limit window as a Claude Code chat read it off its stream-json (terminal.ChatUsage): its kind, the share of it spent (0–100, past 100 on an exceeded spend limit) and when it resets, RFC 3339.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service reads plan quota, caching one reading per account.

func New

func New() *Service

New returns a Service pointed at the live endpoints.

func (*Service) CodexProfilePlan

func (s *Service) CodexProfilePlan(_ context.Context, opts api.ProfilePlanOptions) (Plan, error)

CodexProfilePlan is ProfilePlan for a Codex profile (internal/codexprofile): Personal is the machine's own reading, any other profile is read from its own CODEX_HOME with nothing in front of its login. Plan.Account names the login's email, which is how Settings tells two Codex profiles apart.

func (*Service) HasQuotaLeft added in v0.87.0

func (s *Service) HasQuotaLeft(provider, slug string) bool

HasQuotaLeft answers whether a profile of provider can take a turn now: its plan reads, names at least one window, and none of them is spent or locked. A plan that does not read (signed out, an error, an account lich may not ask about) or reads with no windows is not known to have any, and answers false: a session moved there on a guess lands on a login screen or on the same limit, and waiting for the reset it already knows is the better bet. Only Claude Code and Codex have per-profile readings; any other provider answers false.

func (*Service) Plans

func (s *Service) Plans(_ context.Context, opts api.QuotaPlansOptions) ([]Plan, error)

Plans answers plans over RPC.

func (*Service) ProfilePlan

func (s *Service) ProfilePlan(_ context.Context, opts api.ProfilePlanOptions) (Plan, error)

ProfilePlan is the Claude Code plan of one profile (internal/claudeprofile), read the way a session spawned in it would be: Personal from lich's own environment, which is the machine-wide reading Settings shows, and any other profile from its own directory with no token in front of it. Settings shows one per profile. It is cached like a session's reading, under the account it reads, for the reason Plans gives.

func (*Service) ReportClaude

func (s *Service) ReportClaude(sessionID string, limits []ReportedLimit)

ReportClaude takes the rate limits a Claude Code session reported for the account it spends. Only a long-lived token login uses them: its other reading costs a request against the plan it measures, while a credentials login's usage route also names the plan, the account and the model-scoped caps. A cached reading for that account is updated at once, so the gauge does not wait out the cache for a figure it already has.

func (*Service) SetBinary

func (s *Service) SetBinary(fn func() string)

SetBinary wires the answer to "which Claude Code binary is installed", read once for its version (usageAgent). Startup wiring; unset looks claude up on PATH.

func (*Service) SetEnabled

func (s *Service) SetEnabled(fn func(provider string) bool)

SetEnabled wires the answer to "did the user turn this provider on". A provider that is off is never read: some readers run the provider's CLI, and a signed-out agy answers by opening an interactive login. Startup wiring, called once before the service serves; unset reads every provider.

func (*Service) SetSessions

func (s *Service) SetSessions(fn Sessions)

SetSessions wires the answer to "which account does this session spend". Startup wiring, called once before the service serves.

func (*Service) UseReset

func (s *Service) UseReset(ctx context.Context, opts api.UseResetOptions) (api.UseResetResult, error)

UseReset spends one reset of a grant of the profile's account, and answers what Claude said. Nothing about it is retried or refreshed: a spend cannot be undone, so a failure is reported as it is, and the token is only ever read.

The request id of an attempt is kept until the provider gives an answer, so a retry after an error carries the id of the attempt that may have gone through, and the server counts the two as one.

type Sessions

type Sessions func(sessionID string) Account

Sessions answers what a session's process says about the account it spends. main wires it to the terminal service; a Service without one reads only lich's own environment, which is the machine-wide question Settings asks.

type Window

type Window = api.QuotaWindow

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL