Expand description
The Anthropic backend. The accountโs credential is its setup
token, which probes a minimal billed /v1/messages call - the
usage endpoint refuses setup tokens, while a /v1/messages
response carries the anthropic-ratelimit-unified-* windows as
headers, a 429 most of all. An account with no token has no
credential at all.
Re-exportsยง
use std::collections::HashMap;๐use async_trait::async_trait;๐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::json;๐use forge_primitives::usage::oauth::OauthUsageError;๐use forge_primitives::usage::UsageSnapshot;๐use forge_primitives::usage::UsageSourceKind;๐use forge_primitives::usage::UsageWindow;๐use crate::helpers::OAUTH_TIMEOUT;๐use crate::helpers::parse_retry_after;๐use crate::helpers::redacted_body;๐use crate::helpers::system_time_from_epoch;๐use crate::AccountEnv;๐use crate::BillingModel;๐use crate::ProbeError;๐use crate::Provider;๐use crate::ProviderBackend;๐use crate::ProviderHost;๐
Structsยง
- Anthropic
- The Anthropic
[[accounts]] providertoken.
Constantsยง
- CLAUDE_
CODE_ OAUTH_ TOKEN_ ENV - The variable the flat
tokenkey is stamped onto for an Anthropic account (minted byclaude setup-token). Its presence makes the account token-mode: the token is the accountโs only credential. The value is trimmed once where it enters forgeโs config, so every reader sees the same credential. - MESSAGES_
URL ๐ - PROBE_
MODEL ๐ - The token probeโs model: the cheapest haiku-class ID; its retired predecessor 404s, and 404s carry no headers.
- UNIFIED_
PREFIX ๐
Functionsยง
- messages_
headers ๐ - messages_
probe ๐ - One round-trip against
/v1/messageswith the minimal probe call: the cheapest haiku model,max_tokens1, a tiny input - about nine billed tokens. On a 200 the response headers carry the unified windows, so the HeaderMap is the payload; a 429 keeps the windows it carried on the error; every other status classifies exactly like the windowed probe so the loader and poller treat both probes alike. - messages_
url ๐ /v1/messageson the official host, or an override with any trailing slash trimmed. Production always passesNone: the unified headers only ride the official base.- rate_
limited_ ๐snapshot - The windows a 429 carried. A 429 with no unified headers - a reverse proxyโs limit, or a plan without unified limits - has no reading and keeps the bare rate-limited verdict.
- snapshot_
from_ ๐unified_ headers - Map a 200โs unified headers into a snapshot. Lenient by design: a 200 without the unified headers (an API-key bearer rather than a plan credential, or a plan without unified limits) is the barless row the scope refusal used to settle to, not a fetch error.
- token_
bearer - The setup token
envcarries, when non-empty. An empty value stays None: whitespace is the config loaderโs to strip (once, at load), and this reads the credential as the child gets it. - unified_
header_ ๐value - unified_
window ๐ - One unified window off a responseโs headers: utilization arrives on a 0..1 scale and maps into the snapshotโs percentage scale, the reset arrives as an epoch, and the status is the providerโs own verdict on the window. Each header is independently optional.