Expand description
Composable pieces the backends build probes and snapshots from:
the windowed /api/oauth/usage round-trip, the payload-to-window
mappers, and the timestamp parsing the snapshot mappers share.
Re-exportsยง
use std::collections::HashMap;๐use std::time::Duration;๐use std::time::SystemTime;๐use reqwest::header::ACCEPT;๐use reqwest::header::AUTHORIZATION;๐use reqwest::header::CONTENT_TYPE;๐use reqwest::header::HeaderMap;๐use reqwest::header::HeaderValue;๐use reqwest::header::USER_AGENT;๐use serde_json::Value;๐use forge_primitives::usage::oauth::OauthExtraUsage;๐use forge_primitives::usage::oauth::OauthUsage;๐use forge_primitives::usage::oauth::OauthUsageError;๐use forge_primitives::usage::oauth::OauthUsageWindow;๐use forge_primitives::usage::ExtraUsage;๐use forge_primitives::usage::UsageSnapshot;๐use forge_primitives::usage::UsageSourceKind;๐use forge_primitives::usage::UsageWindow;๐
Structsยง
- Base
UrlCredential - The base-url credential an accountโs
envcarries: theANTHROPIC_BASE_URLendpoint and theANTHROPIC_AUTH_TOKENbearer beside it. - Missing
Base - The read of a base-url credential that found no usable
ANTHROPIC_BASE_URL.
Constantsยง
- OAUTH_
BETA_ ๐HEADER - OAUTH_
TIMEOUT - Per-call timeout the probes bake into their client.
- OAUTH_
USAGE_ ๐URL - TAG ๐
- The width of the token piece a mask matches on: long enough that it is key material rather than a scheme, short enough to survive a truncation.
Functionsยง
- anthropic_
windowed_ probe - One round-trip against
/api/oauth/usagewith the windowed request shape: ACCEPT + content-type + the oauth-beta header +claude-code/<version>UA + Bearer. Status classification: 200 / 401 / 403-with-oauth_scope_insufficient-body / 429 + Retry-After / other. The client and UA arrive injected; building them is the host portโs job. - base_
url_ credential - The base-url credential shared by every base-url provider: the
stamped
ANTHROPIC_BASE_URL(trimmed, empty after trim = missing) andANTHROPIC_AUTH_TOKEN, absent = empty bearer - a localhost proxy ignores the bearer and the probe must still fire. - days_
from_ ๐civil - Days since 1970-01-01 (Howard Hinnantโs civil-from-days inverse).
- is_
scope_ ๐refusal - Whether a 403 body is the usage endpointโs scope refusal rather than
an auth failure. Keyed on the bodyโs
error.details.error_code- the verified live shape for a valid setup token; a revoked one answers 401authentication_error, so the two never share a class. - map_
extra_ ๐usage - map_
window ๐ - mask_
quoted_ ๐runs - Every double-quoted run containing a window of the token masked; an unterminated quote takes its tail with it rather than printing a value whose end nobody found. A runโs spaces stay inside it, which the bare-word pass over it cannot see.
- oauth_
headers ๐ - parse_
iso8601_ ๐timestamp - Hand-rolled ISO-8601 / RFC-3339 datetime parser. Subset:
YYYY-MM-DD[T| ]HH:MM[:SS[.fffffffff]][Z|+HH:MM|-HH:MM]. ReturnsNoneon any field-level parse failure. - parse_
retry_ after - RFC 7231 section 7.1.3
Retry-Afteraccepts either delta-seconds ("120") or an HTTP-date ("Wed, 21 Oct 2015 07:28:00 GMT"). Anthropic emits the integer form today, but the spec leaves the HTTP-date form open and proxies / CDNs in the path may swap shapes. Try the integer form first; fall back to httpdate parsing and compute the delta fromnow. - parse_
timestamp_ value - Parse a JSON value (string OR number) as a
SystemTime. Accepts: - redacted_
body - A body as a log fieldโs suffix (
": ...", capped at 200 chars), with the requestโs credential masked. - redacted_
message - Text with the credential masked, for a field that is not a body - a serde message beside one, which can quote a value it was decoding.
- snapshot_
from_ payload_ lenient - Map a windowed payload to a snapshot with every window treated as
independently optional, never requiring
five_hour. This is the base-url and token pathโs mapper: an alternate-endpoint proxy emits each window on its own ({},{five_hour},{seven_day},{five_hour, seven_day}), and a missingfive_houris a valid steady state - the cold start before the first upstream request, and the post-5h-reset window where the proxy drops the key entirely - not a malformed response.{}maps to all-None (n/a bars); an out-of-contract error-shaped 200 lands here as n/a too rather than erroring. Infallible - there is no window this can reject. - split_
time_ ๐and_ offset - system_
time_ from_ epoch - Convert a non-negative epoch integer to
SystemTime. Values>= 1e12are interpreted as milliseconds; smaller values as seconds. Negative values fail. - usage_
url ๐ - The
/api/oauth/usageendpoint URL. Defaults to the hardcoded Anthropic host; abase_urloverride (an accountโsANTHROPIC_BASE_URL) redirects the probe to an alternate endpoint serving the sameOauthUsageshape. Any trailing slash on the override is trimmed sohttp://host/andhttp://hostbehave identically.