OpenRouter API client for PhoenixKit AI system.
Provides functions for interacting with the OpenRouter API, including:
- API key validation
- Model discovery (fetching available models)
- Building request headers
OpenRouter API Reference
- Base URL: https://openrouter.ai/api/v1
- Authentication: Bearer token in Authorization header
- Optional headers: HTTP-Referer, X-Title (for rankings)
Usage Examples
# Validate an API key
case PhoenixKit.Modules.AI.OpenRouterClient.validate_api_key("sk-or-v1-...") do
{:ok, %{credits: credits}} -> IO.puts("Valid! Credits: #{credits}")
{:error, reason} -> IO.puts("Invalid: #{reason}")
end
# Fetch available models
{:ok, models} = PhoenixKit.Modules.AI.OpenRouterClient.fetch_models("sk-or-v1-...")
Summary
Functions
Returns the base URL for OpenRouter API.
Builds HTTP headers for OpenRouter API requests.
Builds headers from an Account struct's settings.
Builds headers from an Endpoint struct's provider_settings.
Extracts the model name without provider prefix.
Extracts the provider name from a model ID.
Fetches embedding models from OpenRouter.
Fetches embedding models grouped by provider.
Fetches details for a specific model by ID.
Fetches available models from OpenRouter.
Fetches models by type and groups them by provider.
Fetches models and groups them by provider.
Gets the effective max tokens for a model.
Converts a provider slug to a human-friendly name.
Formats a model for display in a select dropdown.
Checks if a model supports a specific parameter.
Validates an OpenRouter API key by making a request to the /models endpoint.
Functions
Returns the base URL for OpenRouter API.
Builds HTTP headers for OpenRouter API requests.
Options
:http_referer- Site URL for rankings:x_title- Site title for rankings:include_usage- Include detailed usage/cost info (default: true for completions)
Builds headers from an Account struct's settings.
Builds headers from an Endpoint struct's provider_settings.
Extracts the model name without provider prefix.
Extracts the provider name from a model ID.
Examples
iex> extract_provider("anthropic/claude-3-opus")
"Anthropic"
iex> extract_provider("openai/gpt-4")
"OpenAI"
Fetches embedding models from OpenRouter.
Note: Embedding models are fetched from a hardcoded list as OpenRouter doesn't return them from the /models endpoint. The actual embedding request goes to /api/v1/embeddings.
Returns {:ok, models} with a list of known embedding models.
Fetches embedding models grouped by provider.
Fetches details for a specific model by ID.
Returns {:ok, model} or {:error, reason}.
Fetches available models from OpenRouter.
Returns {:ok, models} where models is a list of model objects,
or {:error, reason} on failure.
Options
:model_type- Filter by model type::text,:vision,:image_gen,:all(default::all):http_referer- Site URL for rankings:x_title- Site title for rankings
Model Object Structure
Each model has:
id- Model identifier (e.g., "anthropic/claude-3-opus")name- Display namedescription- Model descriptionpricing- Pricing information (prompt/completion costs)context_length- Maximum context windowarchitecture- Model architecture details
Fetches models by type and groups them by provider.
Model Types
:text- Text/chat completion models (text->text):vision- Vision/multimodal models (text+image->text):image_gen- Image generation models (text+image->text+image):all- All models without filtering
Examples
{:ok, grouped} = fetch_models_by_type(api_key, :vision)
Fetches models and groups them by provider.
Options
:model_type- Filter by model type::text,:vision,:image_gen,:all(default::text)
Returns a map where keys are provider names and values are lists of models.
Gets the effective max tokens for a model.
Returns the model's max_completion_tokens if available, otherwise falls back to a percentage of context_length.
Converts a provider slug to a human-friendly name.
OpenRouter model IDs use slugs like "arcee-ai/model-name". This function converts the slug portion to a human-readable provider name.
Examples
iex> humanize_provider("openai")
"OpenAI"
iex> humanize_provider("meta-llama")
"Meta Llama"
iex> humanize_provider("arcee-ai")
"Arcee AI"
Formats a model for display in a select dropdown.
Returns {label, value} tuple.
Checks if a model supports a specific parameter.
Examples
iex> model_supports_parameter?(model, "temperature")
true
iex> model_supports_parameter?(model, "tools")
false
Validates an OpenRouter API key by making a request to the /models endpoint.
Returns {:ok, %{valid: true}} on success, {:error, reason} on failure.