Skip to main content

Module anthropic

Module anthropic 

Source
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]] provider token.

Constantsยง

CLAUDE_CODE_OAUTH_TOKEN_ENV
The variable the flat token key is stamped onto for an Anthropic account (minted by claude 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/messages with the minimal probe call: the cheapest haiku model, max_tokens 1, 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/messages on the official host, or an override with any trailing slash trimmed. Production always passes None: 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 env carries, 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.