Skip to main content

Module openrouter

Module openrouter 

Source
Expand description

The OpenRouter backend: the per-key spend probe against {base}/v1/key with the account credit pool from {base}/v1/credits alongside it. The configured base url already ends in /api (that is what the chat API wants), so only the /v1/... tails are appended; appending the documented site-relative paths yields /api/api/..., which 404s.

Re-exportsΒ§

use std::time::SystemTime; πŸ”’
use async_trait::async_trait; πŸ”’
use reqwest::header::ACCEPT; πŸ”’
use reqwest::header::AUTHORIZATION; πŸ”’
use reqwest::header::HeaderMap; πŸ”’
use reqwest::header::HeaderValue; πŸ”’
use serde::de::DeserializeOwned; πŸ”’
use forge_primitives::usage::oauth::OauthUsageError; πŸ”’
use forge_primitives::usage::openrouter::CreditsResponse; πŸ”’
use forge_primitives::usage::openrouter::KeyResponse; πŸ”’
use forge_primitives::usage::ApiSpend; πŸ”’
use forge_primitives::usage::UsageSnapshot; πŸ”’
use forge_primitives::usage::UsageSourceKind; πŸ”’
use crate::helpers::BaseUrlCredential; πŸ”’
use crate::helpers::MissingBase; πŸ”’
use crate::helpers::OAUTH_TIMEOUT; πŸ”’
use crate::helpers::base_url_credential; πŸ”’
use crate::helpers::parse_retry_after; πŸ”’
use crate::helpers::redacted_body; πŸ”’
use crate::helpers::redacted_message; πŸ”’
use crate::AccountEnv; πŸ”’
use crate::BillingModel; πŸ”’
use crate::ProbeError; πŸ”’
use crate::Provider; πŸ”’
use crate::ProviderBackend; πŸ”’
use crate::ProviderHost; πŸ”’

StructsΒ§

Openrouter
The OpenRouter [[accounts]] provider token.

EnumsΒ§

Mapper πŸ”’

FunctionsΒ§

balance_from_credits πŸ”’
choose_mapper πŸ”’
The mapper the account’s env earns, paired with the credential that earns it: the base-url pair earns the spend mapper, and a missing base url carries its error to the probe’s Unmappable surface. Pure so the routing stays unit-pinned - the probe cannot be driven against the network offline.
credits_balance πŸ”’
The account’s remaining credit pool, or None when the fetch or decode fails for any reason, or the 200 carries no data envelope - each logged with its own cause. The balance is auxiliary, and the /v1/key data the panel also renders must stand even when this endpoint is unavailable.
credits_url πŸ”’
OpenRouter’s account-wide credit endpoint, joined by the same rule as key_url.
fetch_json πŸ”’
One authenticated GET, classified like the window probes: 401 / 403 unauthorized, 429 rate-limited (honouring Retry-After), any other status carrying a truncated body suffix. Returns the decoded body alongside the raw bytes so a caller can name an unexpected 200 shape in its own log.
key_probe πŸ”’
One probe for a pay-per-token account: the per-key spend from {base}/v1/key, plus the account’s credit pool from {base}/v1/credits. Shares OauthUsageError with the window probes so the loader and poller classify a 401 / 429 / network failure the same way regardless of billing kind. The key fetch is load-bearing and its error propagates; the credits fetch is auxiliary and its failure only makes the balance absent.
key_url πŸ”’
OpenRouter’s per-key endpoint: {base}/v1/key, trailing slash trimmed so base and base/ behave identically.
snapshot_from_openrouter_key πŸ”’
Map an OpenRouter key payload into a snapshot. Window-free: a pay-per-token key has no plan window and, when uncapped, no denominator, so nothing here synthesises a utilization.