planshare

package
v0.183.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 5 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 planshare turns a cost at API list price into what that work was of the user's subscription: plan dollars and a share of the weekly limit.

Every cost Caprock shows is the API-list-price equivalent of the tokens it recorded. On a flat plan (Pro, Max, a Team seat) that is not a bill, and the owner asked to see, beside it, what the work was in subscription terms (2026-10-25):

$50 at API · ≈$5 of plan · ≈8% of a week

Both are derived figures, labelled estimates on screen (rule 6), and both are per sign-in — the user's own Claude Code sign-in, each added subscription (ADR-048), Codex's — because each has its own plan and its own limit. This package computes the two rates per sign-in, once, in the daemon; a figure is priced by multiplying each of its parts (the cost each sign-in ran) by its sign-in's rate. The screens do that multiplication through one shared function so the wording and the arithmetic cannot drift.

Plan dollars. factor = the plan's price over the trailing PlanDays / the API-equivalent spend recorded on that sign-in over the same days. The trailing window rather than the calendar month, so the factor does not jump on the first. A sign-in recorded for fewer than PlanDays days divides that much of the price by that much spend. A metered plan has no factor (its API figure is the bill), nor has a sign-in with no plan stated or a price of 0: Caprock never guesses one.

Share of a week. Claude Code's status line (and Codex's transcripts) report the weekly window's used percentage; the history keeps those samples. percent-per-API-dollar = the points used in one weekly window / the API spend recorded on that sign-in over the same stretch. See Calibrate for which window and which stretch.

Use outside Caprock — claude.ai chats, another machine — draws on the same plan and the same limit without being recorded here, so both rates run a little high. The screens say so in the figure's explanation.

Index

Constants

View Source
const (
	MinWeekPoints = 10.0
	MinWeekUSD    = 1.0
)

MinWeekPoints and MinWeekUSD are the least a weekly stretch must hold to calibrate on: 10 percentage points used (the status line reports whole percentages, so fewer would leave a rounding error over a twentieth of the figure) and a dollar of recorded spend.

View Source
const (
	NoteMetered = "metered" // billed per token: the API figure is the bill
	NoteNoPlan  = "no_plan" // no plan stated, or a price of 0
	NoteNoSpend = "no_spend"
)

Plan notes: why a sign-in has no plan factor.

View Source
const (
	NoteNoWindow  = "no_window"  // no weekly window ever observed
	NoteNotEnough = "not_enough" // windows observed, none with enough data
)

Week notes: why a sign-in has no weekly rate.

View Source
const PlanDays = 30

PlanDays is the trailing period the plan factor is measured over.

View Source
const ResetDrop = 20.0

ResetDrop is how far the used percentage must fall below the stretch's highest so far to be read as a reset inside the window (Anthropic resets limits early now and then: on the owner's machine a week fell from 66% to 0% on its fifth day, 2026-10-09). Smaller falls are samples arriving out of order from two sessions — 63% after 61% after 56% — and are ignored.

View Source
const WeekLookbackMs = 35 * 24 * 3600 * 1000

WeekLookbackMs is how far back a completed window may have ended and still calibrate: limits and prices change, and a stale week would price today's work at an old rate.

View Source
const WeekMs = 7 * 24 * 3600 * 1000

WeekMs is the weekly window's length.

Variables

This section is empty.

Functions

This section is empty.

Types

type Plan

type Plan struct {
	Kind        string // "flat", "metered", "" (not stated)
	Label       string
	USDPerMonth float64
}

Plan is how a sign-in is paid for, as the user stated it.

type PlanRate

type PlanRate struct {
	Label       string  `json:"label,omitempty"`
	USDPerMonth float64 `json:"usd_per_month"`
	// Days is how many of the trailing PlanDays the sign-in has been
	// recorded for (PlanDays unless its first session is newer).
	Days float64 `json:"days"`
	// FeeUSD is the price over those days: USDPerMonth × Days / PlanDays.
	FeeUSD float64 `json:"fee_usd"`
	// APIUSD is the recorded API-equivalent spend over the same days.
	APIUSD float64 `json:"api_usd"`
	From   int64   `json:"from"` // unix ms
	To     int64   `json:"to"`   // unix ms
	// Factor is plan dollars per API dollar: FeeUSD / APIUSD.
	Factor float64 `json:"factor"`
}

PlanRate is a sign-in's plan factor and what it was computed from.

func Factor

func Factor(p Plan, apiUSD float64, from, now, firstAt int64) (*PlanRate, string)

Factor computes a sign-in's plan factor from its plan, its recorded spend over [from, now) and when it was first seen, or names why there is none. from is now − PlanDays; firstAt 0 means unknown (taken as before from).

type Rate

type Rate struct {
	Agent string `json:"agent"`
	// Subscription is the id sessions record: "" for the own sign-in, an
	// added subscription's id, or "deepseek".
	Subscription string `json:"subscription"`
	Label        string `json:"label"`
	// PlanKind is the plan that stands for this sign-in: its own, else the
	// main plan for Claude Code; "" when none is stated.
	PlanKind string    `json:"plan_kind"`
	Plan     *PlanRate `json:"plan,omitempty"`
	PlanNote string    `json:"plan_note,omitempty"`
	Week     *WeekRate `json:"week,omitempty"`
	WeekNote string    `json:"week_note,omitempty"`
}

Rate is one sign-in's two rates, as GET /v1/plan-share lists them.

type Response

type Response struct {
	ComputedAt int64  `json:"computed_at"`
	PlanDays   int    `json:"plan_days"`
	Rates      []Rate `json:"rates"`
}

Response is GET /v1/plan-share.

func Compute

func Compute(ctx context.Context, q store.Querier, signIns []SignIn, now time.Time) (Response, error)

Compute is both rates for every sign-in, from the store as of now: one grouped read of the trailing PlanDays' spend, one of first sessions, and per sign-in with a weekly window its samples and a spend sum per stretch tried.

type Sample

type Sample struct {
	Ts       int64   // unix ms observed
	Pct      float64 // used percentage, 0–100
	ResetsAt int64   // unix seconds
}

Sample is one observation of a weekly window.

type SignIn

type SignIn struct {
	Agent, Subscription, Label string
	// Plan is the plan that stands for it (its own, else the main plan for
	// Claude Code).
	Plan Plan
	// Window is the rate_limit key of its weekly window ("seven_day",
	// "codex_seven_day", "sub:<id>:seven_day"); "" when it has none.
	Window string
}

SignIn is one sign-in to compute rates for.

type Stretch

type Stretch struct {
	ResetsAt   int64
	Completed  bool
	From, To   int64
	UsedPoints float64
}

Stretch is one window's measured stretch, before its spend is known.

func Stretches

func Stretches(samples []Sample, now int64) []Stretch

Stretches lists, from samples of one weekly window, the stretches to try in order: completed windows that ended within WeekLookbackMs, newest first, then the current window. A stretch with fewer than MinWeekPoints is left out.

Within a window (the samples whose reset is the same, give or take an hour — Codex reports its reset to the second and it wanders), samples are read oldest first: the stretch starts at the window's start with 0% used, and starts again at a sample ResetDrop or more below the highest so far (an early reset), with that sample's percentage as its base. It ends at the first sample at its highest percentage — usage only grows inside a window, so a later lower sample is a stale one, and work after the limit was reached adds spend and no percentage.

type WeekRate

type WeekRate struct {
	// ResetsAt is the calibrated window's end (unix seconds).
	ResetsAt int64 `json:"resets_at"`
	// Completed is whether that window has ended; false is the current
	// one, used only while no completed window qualifies.
	Completed bool `json:"completed"`
	// From and To bound the measured stretch (unix ms): the window's start,
	// or the first sample after a reset inside it, to the first sample at
	// the stretch's highest percentage.
	From int64 `json:"from"`
	To   int64 `json:"to"`
	// UsedPoints is the percentage points used over the stretch.
	UsedPoints float64 `json:"used_points"`
	// APIUSD is the recorded spend on the sign-in over the stretch.
	APIUSD float64 `json:"api_usd"`
	// PctPerUSD is UsedPoints / APIUSD: a week's percent per API dollar.
	PctPerUSD float64 `json:"pct_per_usd"`
}

WeekRate is a sign-in's share of a week per API dollar and what it was calibrated on.

func Calibrate

func Calibrate(samples []Sample, now int64, spend func(from, to int64) (float64, error)) (*WeekRate, string, error)

Calibrate picks the first stretch whose recorded spend reaches MinWeekUSD and returns its rate. spend(from, to) is the sign-in's recorded spend in [from, to). With no samples at all the note is NoteNoWindow; with samples but no qualifying stretch, NoteNotEnough.

Jump to

Keyboard shortcuts

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