OpenRouterModelRegistry¶
This docs was updated at: 2026-02-23
com.paragon.responses.openrouter.OpenRouterModelRegistry ยท Class
Registry for OpenRouter models with TTL-based caching.
This registry fetches the models list from OpenRouter only once, and caches it for a configurable TTL (default: 1 hour). The cache is refreshed automatically when expired.
Thread-safe for concurrent access.
Usage:
var registry = OpenRouterModelRegistry.builder()
.apiKey("your-api-key")
.build();
Optional cost = registry.calculateCost("openai/gpt-4o", 1000, 500);
Methods¶
builder¶
Creates a new builder for OpenRouterModelRegistry.
fromEnv¶
Creates a registry from environment variables. Uses OPENROUTER_API_KEY for authentication.
getModel¶
Gets a model by its ID.
Parameters
| Name | Description |
|---|---|
modelId |
the model ID (e.g., "openai/gpt-4o") |
Returns
the model if found
getPricing¶
Gets pricing information for a model.
Parameters
| Name | Description |
|---|---|
modelId |
the model ID |
Returns
the pricing if model is found
calculateCost¶
public @NonNull Optional<BigDecimal> calculateCost(
@NonNull String modelId, int inputTokens, int outputTokens)
Calculates the cost for a request with the given model and token counts.
Parameters
| Name | Description |
|---|---|
modelId |
the model ID |
inputTokens |
number of input tokens |
outputTokens |
number of output tokens |
Returns
the calculated cost in USD, or empty if model not found or pricing invalid
invalidateCache¶
Forces a cache refresh on the next access.
getCachedModelCount¶
Returns the number of models in the cache.
isInitialized¶
Returns true if the cache has been initialized.
ensureCacheFresh¶
Ensures the cache is fresh, refreshing if expired.
refreshCache¶
Fetches models from API and updates the cache.
httpClient¶
Sets the HTTP client.
objectMapper¶
Sets the ObjectMapper for JSON parsing.
apiKey¶
Sets the OpenRouter API key.
cacheTtl¶
Sets the cache TTL (time-to-live).
fromEnv¶
Loads configuration from environment variables. Uses OPENROUTER_API_KEY for the API key.
build¶
Builds the registry.