Module: AgentHarness::Providers::QuotaCheckers::OpenRouter
- Defined in:
- lib/agent_harness/providers/quota_checkers/open_router.rb
Overview
Quota checker for OpenRouter's credit balance API.
OpenRouter is selected by Paid runners via ProviderRuntime overrides
rather than a dedicated provider class, so the check is implemented as
a standalone helper that any provider can call from its own
check_quota implementation when the request env points at OpenRouter.
Constant Summary collapse
- DEFAULT_BASE_URL =
"https://openrouter.ai/api/v1"- DEFAULT_TIMEOUT =
10- USER_AGENT =
"AgentHarness/1.0"- HOST_FRAGMENT =
"openrouter.ai"
Class Method Summary collapse
-
.check(env:, base_url: nil, timeout: DEFAULT_TIMEOUT, logger: nil) ⇒ AgentHarness::QuotaStatus
Query OpenRouter's
/creditsendpoint and return a QuotaStatus. -
.resolve_api_key(env) ⇒ String?
Resolve the API key the OpenRouter quota endpoint should use.
-
.routes_through_open_router?(env) ⇒ Boolean
Detect whether a request env would route through OpenRouter.
Class Method Details
.check(env:, base_url: nil, timeout: DEFAULT_TIMEOUT, logger: nil) ⇒ AgentHarness::QuotaStatus
Query OpenRouter's /credits endpoint and return a QuotaStatus.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/agent_harness/providers/quota_checkers/open_router.rb', line 75 def check(env:, base_url: nil, timeout: DEFAULT_TIMEOUT, logger: nil) api_key = resolve_api_key(env) unless api_key && !api_key.empty? logger&.debug("[AgentHarness::QuotaCheckers::OpenRouter] no API key present in env") return QuotaStatus.unavailable end uri = URI.parse("#{resolve_base_url(env, base_url)}/credits") response = perform_get(uri:, api_key:, timeout:, logger:) return QuotaStatus.unavailable unless response parse_credits_response(response) rescue IOError, SocketError, SystemCallError, Timeout::Error, JSON::ParserError, OpenSSL::SSL::SSLError => e logger&.warn("[AgentHarness::QuotaCheckers::OpenRouter] credit check failed: #{e.}") QuotaStatus.unavailable end |
.resolve_api_key(env) ⇒ String?
Resolve the API key the OpenRouter quota endpoint should use.
Order of precedence: explicit OPENROUTER_API_KEY, then
OPENAI_API_KEY (since OpenRouter accepts it under that name when
routed via the OpenAI-compatible transport).
62 63 64 65 |
# File 'lib/agent_harness/providers/quota_checkers/open_router.rb', line 62 def resolve_api_key(env) env_value(env, "OPENROUTER_API_KEY") || env_value(env, "OPENAI_API_KEY") end |
.routes_through_open_router?(env) ⇒ Boolean
Detect whether a request env would route through OpenRouter.
Used by providers (Codex, Kilocode, etc.) to decide whether to
delegate check_quota to this checker.
44 45 46 47 48 49 50 51 52 |
# File 'lib/agent_harness/providers/quota_checkers/open_router.rb', line 44 def routes_through_open_router?(env) return false if env.nil? values = env.values_at("OPENROUTER_API_KEY", :OPENROUTER_API_KEY) return true if values.any? { |value| value.respond_to?(:to_str) && !value.to_str.empty? } base_url_values = env.values_at("OPENAI_BASE_URL", :OPENAI_BASE_URL) base_url_values.any? { |value| value.to_s.include?(HOST_FRAGMENT) } end |