OpenRouter
Documentation for OpenRouter.
OpenRouter.DEEPSEEK_MODEL_MAPOpenRouter.SILICONFLOW_MODEL_MAPOpenRouter.AIMessageOpenRouter.AbstractLLMStreamOpenRouter.AbstractRequestSchemaOpenRouter.AbstractResponseSchemaOpenRouter.AbstractSchemaOpenRouter.AbstractStreamChunkOpenRouter.AnthropicSchemaOpenRouter.ChatCompletionAnthropicSchemaOpenRouter.ChatCompletionResponseSchemaOpenRouter.ChatCompletionSchemaOpenRouter.GeminiSchemaOpenRouter.HttpStreamCallbackOpenRouter.HttpStreamHooksOpenRouter.ModelConfigOpenRouter.ModelConfigOpenRouter.ResponseSchemaOpenRouter.RunInfoOpenRouter.StreamChunkOpenRouter.TokenCountsOpenRouter.ToolOpenRouter.ToolMessageOpenRouter.ToolMessageOpenRouter.GeminiConfigOpenRouter._aigen_coreOpenRouter._list_models_unfilteredOpenRouter.acc_tokensOpenRouter.acc_tokensOpenRouter.add_custom_modelOpenRouter.add_modelOpenRouter.add_providerOpenRouter.aigenOpenRouter.aigenOpenRouter.aigen_rawOpenRouter.anthropic_model_transformOpenRouter.atlascloud_model_transformOpenRouter.build_headersOpenRouter.build_messagesOpenRouter.build_messagesOpenRouter.build_messagesOpenRouter.build_native_models_urlOpenRouter.build_payloadOpenRouter.build_payloadOpenRouter.build_payloadOpenRouter.build_response_bodyOpenRouter.build_response_bodyOpenRouter.build_response_bodyOpenRouter.build_response_bodyOpenRouter.build_urlOpenRouter.build_urlOpenRouter.build_urlOpenRouter.calculate_costOpenRouter.calculate_costOpenRouter.calculate_costOpenRouter.callbackOpenRouter.callbackOpenRouter.cerebras_model_transformOpenRouter.cohere_model_transformOpenRouter.configure_stream_callback!OpenRouter.convert_toolsOpenRouter.create_stub_endpointOpenRouter.create_stub_endpoint_zero_pricingOpenRouter.echo_handlerOpenRouter.extract_chunksOpenRouter.extract_configOpenRouter.extract_contentOpenRouter.extract_contentOpenRouter.extract_contentOpenRouter.extract_contentOpenRouter.extract_contentOpenRouter.extract_contentOpenRouter.extract_contentOpenRouter.extract_finish_reasonOpenRouter.extract_imagesOpenRouter.extract_imagesOpenRouter.extract_provider_from_modelOpenRouter.extract_reasoningOpenRouter.extract_reasoning_from_chunkOpenRouter.extract_reasoning_from_chunkOpenRouter.extract_responseOpenRouter.extract_tokensOpenRouter.extract_tool_callsOpenRouter.extract_ttft_msOpenRouter.fetch_native_modelsOpenRouter.fireworks_model_transformOpenRouter.get_argumentsOpenRouter.get_inference_elapsedOpenRouter.get_provider_auth_headerOpenRouter.get_provider_auth_headerOpenRouter.get_provider_base_urlOpenRouter.get_provider_env_var_nameOpenRouter.get_provider_infoOpenRouter.get_provider_schemaOpenRouter.get_total_elapsedOpenRouter.google_model_transformOpenRouter.groq_model_transformOpenRouter.handle_error_messageOpenRouter.is_doneOpenRouter.is_doneOpenRouter.is_known_providerOpenRouter.is_startOpenRouter.is_startOpenRouter.is_startOpenRouter.is_startOpenRouter.list_aliasesOpenRouter.list_config_parametersOpenRouter.list_embeddings_modelsOpenRouter.list_embeddings_models_rawOpenRouter.list_endpointsOpenRouter.list_endpoints_rawOpenRouter.list_known_providersOpenRouter.list_modelsOpenRouter.list_models_rawOpenRouter.list_native_modelsOpenRouter.list_provider_endpointsOpenRouter.list_providersOpenRouter.list_providersOpenRouter.list_providers_rawOpenRouter.list_schema_parametersOpenRouter.minimax_model_transformOpenRouter.mistral_model_transformOpenRouter.moonshotai_model_transformOpenRouter.needs_tool_executionOpenRouter.normalize_messagesOpenRouter.ollama_model_transformOpenRouter.openai_model_transformOpenRouter.parse_embedding_modelsOpenRouter.parse_endpointsOpenRouter.parse_modelsOpenRouter.print_contentOpenRouter.print_contentOpenRouter.print_contentOpenRouter.remove_custom_modelOpenRouter.remove_modelOpenRouter.remove_providerOpenRouter.resolve_model_aliasOpenRouter.sambanova_model_transformOpenRouter.set_provider!OpenRouter.streamed_request!OpenRouter.strip_provider_prefixOpenRouter.swap_echo_port!OpenRouter.together_model_transformOpenRouter.transform_model_nameOpenRouter.with_echo_serverOpenRouter.xai_model_transform
OpenRouter.DEEPSEEK_MODEL_MAP — Constant
deepseek_model_transform(model_id::String)::StringTransform model IDs for DeepSeek. Currently returns unchanged.
OpenRouter.SILICONFLOW_MODEL_MAP — Constant
siliconflow_model_transform(model_id::String)::StringMap OpenRouter model IDs to SiliconFlow's native IDs (case and prefix differences). Extend this map as SiliconFlow adds or renames models.
OpenRouter.AIMessage — Method
AIMessage(schema::AbstractRequestSchema, result::Dict; endpoint=nothing, elapsed=-1.0)Construct an AIMessage by extracting all fields from raw API result. If endpoint is provided, cost is calculated from token usage.
OpenRouter.AbstractLLMStream — Type
AbstractLLMStreamAbstract type for LLM stream callbacks.
Must have fields:
out: Output stream (e.g.,stdoutor pipe)schema: Request schema determining API formatchunks: List of receivedAbstractStreamChunkchunksverbose: Whether to print verbose informationkwargs: Custom keyword arguments
OpenRouter.AbstractRequestSchema — Type
Abstract base type for request schemas.
OpenRouter.AbstractResponseSchema — Type
Abstract base type for response schemas.
OpenRouter.AbstractSchema — Type
Abstract base type for all API schemas.
OpenRouter.AbstractStreamChunk — Type
AbstractStreamChunkAbstract type for stream chunks.
Must have fields:
event: The event namedata: The data chunkjson: The JSON object ornothingif chunk doesn't contain JSON
OpenRouter.AnthropicSchema — Type
Anthropic Claude-style request schema.
OpenRouter.ChatCompletionAnthropicSchema — Type
ChatCompletion schema with Anthropic-style token semantics. prompt_tokens is already non-cached (additive with cache_read).
OpenRouter.ChatCompletionResponseSchema — Type
Standard OpenAI-compatible response schema.
OpenRouter.ChatCompletionSchema — Type
Standard OpenAI-compatible chat completion request schema. This is the default schema used by most providers.
OpenRouter.GeminiSchema — Type
Google Gemini-style request schema.
OpenRouter.HttpStreamCallback — Type
HttpStreamCallbackHTTP-based streaming callback that prints content to output stream. When streaming completes, builds response body from chunks as if it was a normal API response.
OpenRouter.HttpStreamHooks — Type
HttpStreamHooksA stream callback that combines token counting with customizable hooks for various events.
OpenRouter.ModelConfig — Type
Configuration for model calls, including provider/model slug and call parameters.
Supported Parameters by Schema
ChatCompletionSchema (OpenAI-compatible)
Common parameters supported by most OpenAI-compatible providers:
temperature::Float64: Sampling temperature (0.0-2.0, default varies by model)max_tokens::Int: Maximum tokens to generatetop_p::Float64: Nucleus sampling threshold (0.0-1.0)frequency_penalty::Float64: Penalize frequent tokens (-2.0 to 2.0)presence_penalty::Float64: Penalize present tokens (-2.0 to 2.0)stop::Union{String, Vector{String}}: Stop sequencesn::Int: Number of completions to generatestream::Bool: Enable streaming (handled automatically by streamcallback)logprobs::Bool: Include log probabilitiestop_logprobs::Int: Number of top log probabilities to returnseed::Int: Random seed for deterministic samplingresponse_format::Dict: Structured output format (e.g.,Dict("type" => "json_object"))
AnthropicSchema
Anthropic-specific parameters:
max_tokens::Int: Maximum tokens to generate (required, default 1000)temperature::Float64: Sampling temperature (0.0-1.0)top_p::Float64: Nucleus sampling (0.0-1.0)top_k::Int: Top-k samplingstop_sequences::Vector{String}: Stop sequencescache::Symbol: Prompt caching mode (:system,:tools,:last,:all,:all_but_last)metadata::Dict: Request metadata
GeminiSchema
Google Gemini-specific parameters:
temperature::Float64: Sampling temperaturetop_p::Float64/topP::Float64: Nucleus samplingtop_k::Int/topK::Int: Top-k samplingmax_output_tokens::Int/maxOutputTokens::Int: Maximum output tokenspresence_penalty::Float64/presencePenalty::Float64: Presence penaltyfrequency_penalty::Float64/frequencyPenalty::Float64: Frequency penaltyresponse_mime_type::String/responseMimeType::String: Output MIME typeresponse_schema::Dict/responseSchema::Dict: Output schemaresponse_json_schema::Dict/responseJsonSchema::Dict: JSON schemastop_sequences::Vector{String}/stopSequences::Vector{String}: Stop sequencesthinkingConfig::Dict: Thinking/reasoning configurationthinkingLevel::Int: Reasoning depth levelthinkingBudget::Int: Token budget for reasoninginclude_thoughts::Bool: Include reasoning in response
candidateCount::Int: Number of response candidatesseed::Int: Random seedresponseLogprobs::Bool: Include log probabilitieslogprobs::Int: Number of log probabilities
ResponseSchema (OpenAI Response API)
For gpt-5 and o-series models:
max_completion_tokens::Int: Maximum tokens in completionreasoning_effort::String: Reasoning effort level ("low", "medium", "high")temperature::Float64: Sampling temperaturetop_p::Float64: Nucleus samplingmodalities::Vector{String}: Output modalities (e.g., ["text", "audio"])audio::Dict: Audio output configuration
Examples
# OpenAI-compatible config
config = ModelConfig("openai:openai/gpt-5.1";
temperature=0.7,
max_tokens=1000,
top_p=0.9
)
# Anthropic with caching
config = ModelConfig("anthropic:anthropic/claude-sonnet-4.5";
max_tokens=2000,
temperature=0.8,
cache=:all
)
# Gemini with thinking
config = ModelConfig("google-ai-studio:google/gemini-2.5-flash";
temperature=0.7,
maxOutputTokens=2000,
thinkingConfig=Dict(
:thinkingLevel => 2,
:include_thoughts => true
)
)
# Modify config later
config.kwargs = merge(config.kwargs, (temperature=0.9,))OpenRouter.ModelConfig — Method
ModelConfig(slug::String; schema=nothing, kwargs...)Create a ModelConfig with the given slug and optional parameters.
Example
config = ModelConfig("openai:openai/gpt-5.1"; temperature=0.7, max_tokens=1000)
response = aigen("Hello", config)OpenRouter.ResponseSchema — Type
ResponseSchema <: AbstractRequestSchemaSchema for OpenAI's Responses API (v1/responses endpoint). Used by newer models like gpt-5.1 and o-series models.
OpenRouter.RunInfo — Type
RunInfo(; creation_time=time(), inference_start=nothing, last_message_time=nothing, stop_sequence=nothing, ttft_ms=nothing)Tracks run statistics and metadata during the streaming process.
Fields
creation_time: When the callback was createdinference_start: When the model started processinglast_message_time: Timestamp of the last received messagestop_sequence: The sequence that caused the generation to stop (if any). For OpenAI this can be:- A specific stop sequence provided in the chunk's delta.stop_sequence
- "stop" if finish_reason is "stop"
ttft_ms: Time to first token in milliseconds (from provider's sla_metrics if available)
Timing Methods
get_total_elapsed(info): Get total elapsed time since callback creationget_inference_elapsed(info): Get elapsed time for inference phase only
OpenRouter.StreamChunk — Type
StreamChunkA chunk of streaming data. A message is composed of multiple chunks.
Fields
event: The event namedata: The data chunkjson: The JSON object ornothingif chunk doesn't contain JSON
OpenRouter.TokenCounts — Type
TokenCountsUniversal token counting struct. Fields are NON-OVERLAPPING for correct cost calculation.
Fields (non-overlapping)
prompt_tokens::Int: Cache misses - input tokens NOT served from cache (charged at full price)input_cache_read::Int: Cache hits - input tokens served from cache (charged at cache price)completion_tokens::Int: Output tokenstotal_tokens::Int: Sum of all input + output tokensinput_cache_write::Int: Tokens written to cache (Anthropic)internal_reasoning::Int: Reasoning/thinking tokens (Gemini, DeepSeek R1)input_audio_cache::Int: Audio tokens cached
Cost calculation
Total input = prompttokens + inputcacheread (no double counting) Cost = prompttokens × fullprice + inputcacheread × cacheprice
OpenRouter.Tool — Type
ToolSchema-agnostic tool definition. Automatically converted to the right format based on the API schema (OpenAI vs Anthropic vs Gemini).
Example
tool = Tool(
name = "create_file",
description = "Create a file with the given content",
parameters = Dict(
"type" => "object",
"properties" => Dict(
"path" => Dict("type" => "string", "description" => "File path"),
"content" => Dict("type" => "string", "description" => "File content")
),
"required" => ["path", "content"]
)
)OpenRouter.ToolMessage — Method
Create a ToolMessage from a tool_call dict and string result.
OpenRouter.ToolMessage — Method
Create a ToolMessage by running fn(args::Dict{String,Any}) with the tool_call's parsed arguments.
OpenRouter.GeminiConfig — Method
GeminiConfig(model_id::String; kwargs...)Convenience constructor for Gemini models with common parameter validation.
Gemini-specific Parameters
temperature::Float64: Sampling temperature (0.0-2.0)topP::Float64: Nucleus sampling (0.0-1.0)topK::Int: Top-k samplingmaxOutputTokens::Int: Maximum output tokensthinkingConfig::NamedTuple: Thinking configuration with fields:thinkingLevel::String: Reasoning depth ("low" or "high") - only for Pro modelsthinkingBudget::Int: Token budget for reasoning - for non-Pro modelsinclude_thoughts::Bool: Include reasoning in response
Examples
# Basic Gemini config
config = GeminiConfig("google/gemini-2.5-flash";
temperature=0.7,
maxOutputTokens=2000
)
# Pro model with thinking level
config = GeminiConfig("google/gemini-2.5-pro";
temperature=0.8,
maxOutputTokens=3000,
thinkingConfig=(
thinkingLevel="high",
include_thoughts=true
)
)
# Non-Pro model with thinking budget
config = GeminiConfig("google/gemini-2.5-flash-thinking";
thinkingConfig=(
thinkingBudget=1000,
include_thoughts=true
)
)
response = aigen("Explain quantum entanglement", config)OpenRouter._aigen_core — Method
Core function that handles both streaming and non-streaming API calls.
OpenRouter._list_models_unfiltered — Function
_list_models_unfiltered(api_key::String = get(ENV, "OPENROUTER_API_KEY", ""))::Vector{OpenRouterModel}Internal helper: return all models without any provider-based filtering, using the raw API. Used by update_db to avoid recursive use of the cache.
OpenRouter.acc_tokens — Method
Accumulate tokens according to schema-specific logic.
Schema-specific behavior
AnthropicSchema: Replaces values (Anthropic sends cumulative counts)- Other schemas: Adds values (most providers send deltas)
OpenRouter.acc_tokens — Method
Accumulate tokens for ChatCompletion schema. Detects cumulative counts (DeepSeek style) vs delta counts and handles accordingly.
OpenRouter.add_custom_model — Function
add_custom_model(id::String, name::String, description::String="Custom model",
context_length::Union{Int,Nothing}=nothing,
pricing::Union{Pricing,Nothing}=nothing,
architecture::Union{Architecture,Nothing}=nothing)Add a custom model to the local cache.
Example
add_custom_model("echo/100tps", "Echo 100 TPS", "Fast echo model for testing", 8192)
add_custom_model("local/llama3", "Local Llama 3", "Self-hosted Llama 3", 4096)OpenRouter.add_model — Function
add_model(id::String, name::String, description::String="Custom model",
context_length::Union{Int,Nothing}=nothing,
pricing::Union{Pricing,Nothing}=nothing,
architecture::Union{Architecture,Nothing}=nothing)Add a model to the local cache.
Example
add_model("echo/100tps", "Echo 100 TPS", "Fast echo model for testing", 8192)
add_model("ollama/llama3", "Local Llama 3", "Self-hosted Llama 3", 4096)OpenRouter.add_provider — Function
add_provider(name::String, base_url::String, auth_header_format::String="Bearer",
api_key_env_var::Union{String,Nothing}=nothing,
default_headers::Dict{String,String}=Dict{String,String}(),
model_name_transform::Union{Function,Nothing}=nothing,
schema::AbstractRequestSchema=ChatCompletionSchema(),
notes::String="Custom provider")Add a provider to the registry. Warns if overwriting an existing provider.
Example
add_provider("echo", "http://localhost:8080/v1", "Bearer", "ECHO_API_KEY")
add_provider("ollama", "http://localhost:11434/v1", "Bearer")OpenRouter.aigen — Method
aigen(prompt, provider_model::String; ...)
aigen(prompt, config::ModelConfig; ...)Generate text using a specific provider and model, or a ModelConfig.
Arguments
prompt: The input prompt (String or Vector of message dicts)provider_model::String: Format "Provider:model/slug" (e.g., "Together:moonshotai/kimi-k2-thinking")config::ModelConfig: Model configuration with slug and parameters
Keyword Arguments
schema::Union{AbstractRequestSchema, Nothing}: Request schema to use (auto-detected if not provided)api_key::Union{String, Nothing}: Provider-specific API key (auto-detected from env if not provided)sys_msg: System message/instructionstreamcallback::Union{Nothing, AbstractLLMStream}: Stream callback for real-time processingkwargs...: Additional API parameters
Returns
AIMessage: Generated response with metadata (cost, tokens, etc.)
Example
# Using string slug
response = aigen("Write a haiku about Julia programming", "Together:moonshotai/kimi-k2-thinking")
# Using ModelConfig
config = ModelConfig("openai:openai/gpt-5.1"; temperature=0.7, max_tokens=1000)
response = aigen("Hello", config)
# Using system message
response = aigen("Hello", "Anthropic:claude-3-sonnet"; sys_msg="You are a helpful assistant")
# Using streaming
using OpenRouter
callback = HttpStreamCallback(; out=stdout)
response = aigen("Count to 10", "anthropic:anthropic/claude-haiku-4.5"; streamcallback=callback)OpenRouter.aigen — Method
aigen(; prompt, model, kwargs...)Kwarg-only version of aigen. ```
OpenRouter.aigen_raw — Method
aigen_raw(prompt, provider_model::String;
schema::Union{AbstractRequestSchema, Nothing} = nothing,
api_key::Union{String, Nothing} = nothing,
sys_msg = nothing,
streamcallback::Union{Nothing, AbstractLLMStream} = nothing,
kwargs...)Generate text using a specific provider and model, returning raw API response and parsing components.
This function is useful for:
- Testing equivalence between streaming and non-streaming responses
- Debugging API response formats
- Custom response processing
Returns
NamedTuple: Contains(result, schema, provider_info, model_id, provider_endpoint, elapsed)
Example
# Compare streaming vs non-streaming raw responses
raw_stream = aigen_raw("Hello", "anthropic:claude-3-sonnet"; streamcallback=HttpStreamCallback())
raw_normal = aigen_raw("Hello", "anthropic:claude-3-sonnet")OpenRouter.anthropic_model_transform — Method
anthropic_model_transform(model_id::String)::StringTransform model IDs for Anthropic. Removes anthropic/ prefix and replaces dots with dashes. Also handles special cases and version matching.
OpenRouter.atlascloud_model_transform — Method
atlascloud_model_transform(model_id::String)::StringTransform model IDs for AtlasCloud. Maps DeepSeek models to the provider's deepseek-ai/... namespace.
OpenRouter.build_headers — Method
Build complete headers for a provider request.
OpenRouter.build_messages — Method
Build messages array for AnthropicSchema.
Returns a tuple: (messages, system_content)
OpenRouter.build_messages — Method
Build messages array for ChatCompletionSchema.
OpenRouter.build_messages — Method
Build contents array for GeminiSchema.
Returns a tuple: (contents, system_instruction)
OpenRouter.build_native_models_url — Method
build_native_models_url(provider_info::ProviderInfo)::StringBuild the models endpoint URL for a provider's native API.
OpenRouter.build_payload — Function
Build the request payload for AnthropicSchema.
OpenRouter.build_payload — Function
Build the request payload for ChatCompletionSchema.
OpenRouter.build_payload — Function
Build the request payload for GeminiSchema.
OpenRouter.build_response_body — Method
build_response_body(schema::AnthropicSchema, cb::AbstractLLMStream; verbose::Bool = false, kwargs...)Build response body from chunks to mimic standard Anthropic API response. Supports text and tool_use content blocks.
OpenRouter.build_response_body — Method
build_response_body(schema::ChatCompletionSchema, cb::AbstractLLMStream; verbose::Bool = false, kwargs...)Build response body from chunks to mimic standard ChatCompletion API response.
Note: Limited functionality. Does NOT support refusals, logprobs.
OpenRouter.build_response_body — Method
build_response_body(schema::GeminiSchema, cb::AbstractLLMStream; verbose::Bool = false, kwargs...)Build response body from chunks to mimic standard Gemini API response.
OpenRouter.build_response_body — Method
build_response_body(schema::ResponseSchema, cb::AbstractLLMStream; verbose::Bool = false, kwargs...)Build response body from chunks. Optimized to find the final response.completed object immediately (O(1) effectively), with a fallback reconstruction for interrupted streams.
OpenRouter.build_url — Function
Build the URL for ChatCompletionSchema.
OpenRouter.build_url — Function
Build the URL for AnthropicSchema.
OpenRouter.build_url — Function
Build the URL for GeminiSchema (handles model parameter substitution and streaming).
OpenRouter.calculate_cost — Function
Calculate cost for a given endpoint and token usage. Unwraps .pricing. Warns if cost cannot be determined (e.g. missing pricing).
OpenRouter.calculate_cost — Method
Calculate cost based on pricing and token usage.
OpenRouter.calculate_cost — Method
Calculate cost for a given endpoint and token usage. Unwraps .pricing. Warns if cost cannot be determined (e.g. missing pricing).
OpenRouter.callback — Method
callback(cb::AbstractLLMStream, chunk::AbstractStreamChunk; kwargs...)Process chunk and print it. Wrapper for:
- extract content from chunk using
extract_content - print content to output stream using
print_content
OpenRouter.callback — Method
callback(cb::AbstractLLMStream, chunk::AbstractStreamChunk; kwargs...)Process chunk and print it. Wrapper for:
- extract content from chunk using
extract_content - print content to output stream using
print_content
OpenRouter.cerebras_model_transform — Method
cerebras_model_transform(model_id::String)::StringTransform model IDs for Cerebras.
OpenRouter.cohere_model_transform — Method
cohere_model_transform(model_id::String)::StringTransform model IDs for Cohere. Currently returns unchanged.
OpenRouter.configure_stream_callback! — Method
configure_stream_callback!(cb::AbstractLLMStream, schema::AbstractRequestSchema, provider_info::ProviderInfo, provider_endpoint::ProviderEndpoint)Configure stream callback with schema and provider information. For HttpStreamHooks, also sets up pricing for accurate cost calculation.
OpenRouter.convert_tools — Method
convert_tools(schema, tools)Convert tools to schema-specific format. Supports:
Toolstructs (converted per schema)Vector{Tool}(each converted)Dict/Vector{Dict}(passed through as-is for backward compat)
OpenRouter.create_stub_endpoint — Method
create_stub_endpoint(provider_name, model_id; pricing=nothing)Create a stub ProviderEndpoint for local/non-OpenRouter providers.
OpenRouter.create_stub_endpoint_zero_pricing — Method
Create stub with zero pricing (for Ollama etc. where we want cost tracking).
OpenRouter.echo_handler — Method
Route request to appropriate response based on endpoint.
OpenRouter.extract_chunks — Method
extract_chunks(schema::AbstractRequestSchema, blob::AbstractString;
spillover::AbstractString = "", verbose::Bool = false, kwargs...)Extract chunks from received SSE blob. Correctly implements SSE spec field parsing.
OpenRouter.extract_config — Method
Extract slug and merge kwargs from ModelConfig with call-time kwargs. Call-time kwargs take precedence over config kwargs.
OpenRouter.extract_content — Method
Extract response content for AnthropicSchema.
OpenRouter.extract_content — Method
extract_content(schema::AnthropicSchema, chunk::AbstractStreamChunk;
include_thinking::Bool = true, kwargs...)Extract content from Anthropic chunk.
OpenRouter.extract_content — Method
Extract response content for ChatCompletionSchema.
OpenRouter.extract_content — Method
extract_content(schema::ChatCompletionSchema, chunk::AbstractStreamChunk; kwargs...)Extract content from ChatCompletion chunk.
OpenRouter.extract_content — Method
Extract response content for GeminiSchema.
OpenRouter.extract_content — Method
extract_content(schema::GeminiSchema, chunk::StreamChunk; kwargs...)Extract regular (non-reasoning) content from Gemini chunk.
OpenRouter.extract_content — Method
extract_content(schema::ResponseSchema, chunk::AbstractStreamChunk; kwargs...)Extract content from Response API chunk. Only extracts 'delta' to ensure stream consumers don't print duplicate content (since 'done' events contain the full text).
OpenRouter.extract_finish_reason — Method
Extract finish reason from API response based on schema.
OpenRouter.extract_images — Method
Extract generated images from Gemini API response. Returns Vector{String} of base64 data URLs or nothing if no images.
OpenRouter.extract_images — Method
Extract generated images from Response API output. Returns Vector{String} of base64 data URLs or nothing if no images.
OpenRouter.extract_provider_from_model — Method
extract_provider_from_model(model_name::String) -> StringExtract provider name from model name in format "provider:author/model_id" or fallback to "openai".
Examples
extract_provider_from_model("openai:openai/gpt-4") # => "openai"
extract_provider_from_model("anthropic:anthropic/claude-3-5-sonnet") # => "anthropic"
extract_provider_from_model("cerebras:meta-llama/llama-3.1-8b") # => "cerebras"
extract_provider_from_model("gpt-4") # => "openai" (fallback)OpenRouter.extract_reasoning — Method
Extract reasoning content from API response based on schema. Returns nothing if schema doesn't support reasoning or no reasoning found.
OpenRouter.extract_reasoning_from_chunk — Method
extract_reasoning_from_chunk(schema::ChatCompletionSchema, chunk::AbstractStreamChunk)Extract reasoning_content from ChatCompletion chunk (DeepSeek style).
OpenRouter.extract_reasoning_from_chunk — Method
extract_reasoning_from_chunk(schema::GeminiSchema, chunk::StreamChunk)Extract reasoning/thinking content from Gemini chunk (parts with "thought": true).
OpenRouter.extract_response — Method
Extract full response for ChatCompletionResponseSchema.
OpenRouter.extract_tokens — Method
Extract token usage information from API response based on schema. Returns TokenCounts struct with standardized field names.
OpenRouter.extract_tool_calls — Method
Extract tool calls from API response based on schema. Returns nothing if no tool calls found.
OpenRouter.extract_ttft_ms — Method
Extract ttftms from slametrics (DeepSeek/OpenRouter style).
OpenRouter.fetch_native_models — Method
fetch_native_models(provider_info::ProviderInfo, api_key::String)::Vector{Dict}Fetch models directly from a provider's native API. Returns raw model data as returned by the provider.
OpenRouter.fireworks_model_transform — Method
fireworks_model_transform(model_id::String)::StringTransform model IDs for Fireworks. Currently returns unchanged.
OpenRouter.get_arguments — Method
Parse the arguments from a tool_call dict, handling both JSON string and already-parsed Dict.
OpenRouter.get_inference_elapsed — Method
get_inference_elapsed(info::RunInfo)Get elapsed time for inference (time between first inference and last message). Returns time in seconds or nothing if inference hasn't started.
OpenRouter.get_provider_auth_header — Method
Build an auth header pair (name => value) for a provider + API key, or nothing if provider is unknown.
OpenRouter.get_provider_auth_header — Method
Build an auth header pair (name => value) for a ProviderInfo + API key.
OpenRouter.get_provider_base_url — Method
Get just the base URL for a provider slug, or nothing if unknown.
OpenRouter.get_provider_env_var_name — Method
Return the configured API key env var name for a provider, or nothing.
OpenRouter.get_provider_info — Method
Get the provider info for a given slug, or nothing if unknown.
OpenRouter.get_provider_schema — Method
Get the appropriate schema for a provider info and model.
OpenRouter.get_total_elapsed — Method
get_total_elapsed(info::RunInfo)Get total elapsed time since callback creation. Returns time in seconds or nothing if no messages received.
OpenRouter.google_model_transform — Method
google_model_transform(model_id::String)::StringTransform model IDs for Google. Removes google/ prefix.
OpenRouter.groq_model_transform — Method
groq_model_transform(model_id::String)::StringTransform OpenRouter model IDs to Groq-specific model IDs. Handles various model mappings for Groq's native API.
Examples
groq_model_transform("moonshotai/kimi-k2-0905") # => "moonshotai/kimi-k2-instruct-0905"
groq_model_transform("other/model") # => "other/model"OpenRouter.handle_error_message — Method
handle_error_message(chunk::AbstractStreamChunk; kwargs...)Handle error messages from streaming response. Always throws on error.
OpenRouter.is_done — Method
is_done(schema::ChatCompletionSchema, chunk::AbstractStreamChunk; kwargs...)Check if streaming is done for ChatCompletion format. Checks for finish_reason in choices or [DONE] marker.
OpenRouter.is_done — Method
is_done(schema::ResponseSchema, chunk::AbstractStreamChunk; kwargs...)Check if streaming is done for Response API format.
OpenRouter.is_known_provider — Method
Check if this provider slug is known.
OpenRouter.is_start — Method
is_start(schema::AnthropicSchema, chunk::AbstractStreamChunk; kwargs...)Check if streaming has started for Anthropic format.
OpenRouter.is_start — Method
is_start(schema::ChatCompletionSchema, chunk::AbstractStreamChunk; kwargs...)Check if streaming has started for ChatCompletion format.
OpenRouter.is_start — Method
is_start(schema::GeminiSchema, chunk::AbstractStreamChunk; kwargs...)Check if streaming has started for Gemini format. Gemini doesn't have explicit start events, so we check for first content with reasoning (thought=true).
OpenRouter.is_start — Method
is_start(schema::ResponseSchema, chunk::AbstractStreamChunk; kwargs...)Check if streaming has started for Response API format.
OpenRouter.list_aliases — Method
list_aliases()::Dict{String, String}List all available model aliases.
OpenRouter.list_config_parameters — Method
list_config_parameters(config::ModelConfig)List parameters currently set in a ModelConfig.
Example
config = ModelConfig("openai:openai/gpt-5.1"; temperature=0.7, max_tokens=1000)
list_config_parameters(config)OpenRouter.list_embeddings_models — Function
list_embeddings_models(api_key::String = get(ENV, "OPENROUTER_API_KEY", ""))::Vector{OpenRouterEmbeddingModel}Return parsed embedding models list as Julia structs. Uses OPENROUTERAPIKEY environment variable by default.
OpenRouter.list_embeddings_models_raw — Function
list_embeddings_models_raw(api_key::String = get(ENV, "OPENROUTER_API_KEY", ""))::StringReturn raw JSON string of embedding models list. Uses OPENROUTERAPIKEY environment variable by default.
OpenRouter.list_endpoints — Function
list_endpoints(model_id::String, api_key::String = get(ENV, "OPENROUTER_API_KEY", ""))::ModelProvidersReturn parsed endpoints for a specific model from OpenRouter as Julia struct. Model ID should be in format "author/slug" (e.g., "moonshotai/kimi-k2-thinking"). Uses OPENROUTERAPIKEY environment variable by default.
OpenRouter.list_endpoints_raw — Function
list_endpoints_raw(model_id::String, api_key::String = get(ENV, "OPENROUTER_API_KEY", ""))::StringReturn raw JSON string of endpoints for a specific model from OpenRouter. Model ID should be in format "author/slug" (e.g., "moonshotai/kimi-k2-thinking"). Uses OPENROUTERAPIKEY environment variable by default.
OpenRouter.list_known_providers — Method
List all known provider slugs.
OpenRouter.list_models — Function
list_models(provider_filter::Union{String, Nothing} = nothing, api_key::String = get(ENV, "OPENROUTER_API_KEY", ""))::Vector{OpenRouterModel}Return parsed model list as Julia structs, optionally filtered by provider. Uses OPENROUTERAPIKEY environment variable by default.
OpenRouter.list_models_raw — Function
list_models_raw(api_key::String = get(ENV, "OPENROUTER_API_KEY", ""))::StringReturn raw JSON string of model list. Uses OPENROUTERAPIKEY environment variable by default.
OpenRouter.list_native_models — Function
list_native_models(provider_slug::String, api_key::String = "")::Vector{Dict}List models using a provider's native API. Returns raw model data as returned by the provider.
Example
models = list_native_models("cerebras")
models = list_native_models("openai", "your-api-key")OpenRouter.list_provider_endpoints — Function
list_provider_endpoints(provider_filter::String, api_key::String = get(ENV, "OPENROUTER_API_KEY", ""))::Vector{ProviderEndpoint}Return all ProviderEndpoint entries hosted by the given provider.
This uses the cached model database with endpoints; it will fetch endpoints as needed on first call.
Example:
groq_eps = list_provider_endpoints("groq")
for ep in groq_eps
println(ep.provider_name, " ", ep.name, " (", ep.model_name, ")")
endOpenRouter.list_providers — Function
list_providers(model_id::String, api_key::String = get(ENV, "OPENROUTER_API_KEY", ""))::ModelProvidersReturn parsed providers for a specific model as Julia struct. Model ID should be in format "author/slug" (e.g., "moonshotai/kimi-k2-thinking"). Uses OPENROUTERAPIKEY environment variable by default.
OpenRouter.list_providers — Method
list_providers()List all registered providers.
OpenRouter.list_providers_raw — Function
list_providers_raw(model_id::String, api_key::String = get(ENV, "OPENROUTER_API_KEY", ""))::StringReturn raw JSON string of providers for a specific model. ...
OpenRouter.list_schema_parameters — Method
list_schema_parameters(schema::Type{<:AbstractRequestSchema})
list_schema_parameters(schema::AbstractRequestSchema)List common parameters supported by a schema type.
Example
list_schema_parameters(ChatCompletionSchema)
list_schema_parameters(AnthropicSchema)
list_schema_parameters(GeminiSchema)OpenRouter.minimax_model_transform — Method
minimax_model_transform(model_id::String)::StringTransform model IDs for Minimax. Currently returns unchanged.
OpenRouter.mistral_model_transform — Method
mistral_model_transform(model_id::String)::StringTransform model IDs for Mistral. Currently returns unchanged.
OpenRouter.moonshotai_model_transform — Method
moonshotai_model_transform(model_id::String)::StringTransform model IDs for MoonshotAI. Currently returns unchanged.
OpenRouter.needs_tool_execution — Method
needs_tool_execution(info::RunInfo)Check if the run was terminated because the model is requested tool execution (with stop_sequence).
OpenRouter.normalize_messages — Method
Normalize prompt + sys_msg into a flat vector of AbstractMessage.
Accepted prompt forms:
- String => one UserMessage
- AbstractMessage => wrapped in a vector
- Vector of items => each element may be
- AbstractMessage
- String => treated as UserMessage
- anything else => treated as UserMessage with that content
OpenRouter.ollama_model_transform — Method
ollama_model_transform(model_id::String)::StringTransform model IDs for Ollama. Currently returns unchanged.
OpenRouter.openai_model_transform — Method
openai_model_transform(model_id::String)::StringTransform model IDs for OpenAI. Removes openai/ prefix and handles specific mappings.
OpenRouter.parse_embedding_models — Method
parse_embedding_models(json_str::String)::Vector{OpenRouterEmbeddingModel}Parse OpenRouter embedding models JSON response into Julia structs.
OpenRouter.parse_endpoints — Method
parse_endpoints(json_str::String)::ModelProvidersParse model providers JSON response into Julia struct.
OpenRouter.parse_models — Method
parse_models(json_str::String)::Vector{OpenRouterModel}Parse OpenRouter models JSON response into Julia structs.
OpenRouter.print_content — Method
print_content(out::Channel, text::AbstractString; kwargs...)Print content to Channel.
OpenRouter.print_content — Method
print_content(out::IO, text::AbstractString; kwargs...)Print content to IO output stream.
OpenRouter.print_content — Method
print_content(out::Nothing, text::Any; kwargs...)Do nothing if output stream is nothing.
OpenRouter.remove_custom_model — Method
remove_custom_model(id::String)Remove a custom model from the local cache.
OpenRouter.remove_model — Method
remove_model(id::String)Remove a model from the local cache.
OpenRouter.remove_provider — Method
remove_provider(name::String)Remove a provider from the registry.
OpenRouter.resolve_model_alias — Method
resolve_model_alias(model_id::String)::StringResolve a model alias to the full provider:model format. If the input is not an alias, returns it unchanged.
Example
resolve_model_alias("gemf") # Returns "google-ai-studio:google/gemini-2.5-flash-preview-09-2025"
resolve_model_alias("anthropic:claude-3-sonnet") # Returns unchangedOpenRouter.sambanova_model_transform — Method
sambanova_model_transform(model_id::String)::StringTransform model IDs for SambaNova. Currently returns unchanged.
OpenRouter.set_provider! — Function
set_provider!(name::String, base_url::String, auth_header_format::String="Bearer",
api_key_env_var::Union{String,Nothing}=nothing,
default_headers::Dict{String,String}=Dict{String,String}(),
model_name_transform::Union{Function,Nothing}=nothing,
schema::AbstractRequestSchema=ChatCompletionSchema(),
notes::String="Custom provider")Set/override a provider in the registry. Use this when intentionally overwriting an existing provider.
OpenRouter.streamed_request! — Method
streamed_request!(cb::AbstractLLMStream, url, headers, input; kwargs...)End-to-end wrapper for POST streaming requests. Modifies callback object (cb.chunks) in-place and returns response object.
OpenRouter.strip_provider_prefix — Method
strip_provider_prefix(model_id::AbstractString, provider::AbstractString)::AbstractStringRemove provider prefix from model ID if present. Helper function for model transformations.
Examples
strip_provider_prefix("openai/gpt-4", "openai") # => "gpt-4"
strip_provider_prefix("gpt-4", "openai") # => "gpt-4"
strip_provider_prefix("google/gemini-pro", "google") # => "gemini-pro"OpenRouter.swap_echo_port! — Method
Swap echo provider ports from from to to.
OpenRouter.together_model_transform — Method
together_model_transform(model_id::String)::StringTransform model IDs for Together. Currently returns unchanged.
OpenRouter.transform_model_name — Method
Transform model name according to provider-specific rules.
OpenRouter.with_echo_server — Function
Run function with echo server on given port.
OpenRouter.xai_model_transform — Method
xai_model_transform(model_id::String)::StringTransform model IDs for xAI. Removes x-ai/ prefix and handles specific mappings.