- [Overview](#overview)
OpenRouter Pipe Class API
Table of Contents
- Overview
- Authentication
- Model Configuration
- Inference Settings
- Sampling Parameters
- Reasoning
- Function Calling
- Provider Options
- Response Format
- Streaming
- Tracing Behavior
- Error Mapping
Overview
The OpenRouterPipe class provides a TPipe abstraction for interacting with OpenRouter’s unified API. It supports 300+ models from various providers with OpenAI-compatible request/response formats and OpenRouter-specific extensions.
class OpenRouterPipe : Pipe()
Key Features:
- Access to 300+ models from multiple providers
- OpenAI-compatible
/v1/chat/completionsendpoint - Built-in support for reasoning models
- Provider preference routing
- Request caching and service tiers
- Streaming via Server-Sent Events
Authentication
setApiKey(key: String): OpenRouterPipe
Sets the OpenRouter API key for authentication.
@param keyThe API key from openrouter.ai@returnThis pipe instance for fluent chaining
val pipe = OpenRouterPipe()
.setApiKey("sk-or-v1-...")
Note: The API key can also be set via the OPENROUTER_API_KEY environment variable or openrouterEnv.setApiKey(). If not set before init(), the pipe will attempt to resolve from these sources.
setHttpReferer(referer: String): OpenRouterPipe
Sets the HTTP Referer header for site traffic tracking.
@param refererThe referer URL to send with requests@returnThis pipe instance for fluent chaining
setOpenRouterTitle(title: String): OpenRouterPipe
Sets the OpenRouter Title header to identify your application.
@param titleThe application title@returnThis pipe instance for fluent chaining
Model Configuration
setModel(model: String): OpenRouterPipe
Sets the model identifier. Inherited from [Pipe] base class.
Model examples:
anthropic/claude-3.5-sonnetopenai/gpt-4ogoogle/gemini-proopenrouter/free(free model routing)
setBaseUrl(url: String): OpenRouterPipe
Sets a custom base URL for the API endpoint.
@param urlBase URL (defaults tohttps://openrouter.ai/api/v1)@returnThis pipe instance for fluent chaining
Inference Settings
setTemperature(temperature: Double): OpenRouterPipe
Sets the sampling temperature. Inherited from [Pipe] base class.
@param temperatureValue between 0.0 and 2.0 (default: 0.7)
setMaxTokens(maxTokens: Int): OpenRouterPipe
Sets the maximum number of tokens to generate. Inherited from [Pipe] base class.
setTopP(topP: Double): OpenRouterPipe
Sets the nucleus sampling threshold. Inherited from [Pipe] base class.
Sampling Parameters
setMinP(p: Double): OpenRouterPipe
Sets the MinP sampling parameter for minimum probability threshold.
@param pMinP value between 0.0 and 1.0@returnThis pipe instance for fluent chaining
setTopA(a: Double): OpenRouterPipe
Sets the TopA sampling parameter.
@param aTopA value between 0.0 and 1.0@returnThis pipe instance for fluent chaining
setOpenRouterTopK(k: Int): OpenRouterPipe
Sets the TopK sampling parameter. Note: Not available for OpenAI models.
@param kTopK value between 1 and 255@returnThis pipe instance for fluent chaining
setOpenRouterRepetitionPenalty(penalty: Double): OpenRouterPipe
Sets the repetition penalty. This is distinct from setFrequencyPenalty().
@param penaltyRepetition penalty between 0.0 and 2.0@returnThis pipe instance for fluent chaining
setFrequencyPenalty(penalty: Double): OpenRouterPipe
Sets the frequency penalty for reducing repetition.
@param penaltyFrequency penalty between -2.0 and 2.0@returnThis pipe instance for fluent chaining
Reasoning
setReasoningEffort(effort: String): OpenRouterPipe
Sets reasoning effort for reasoning-capable models. Convenience method that creates [ReasoningConfig] with only effort.
@param effortReasoning effort: “xhigh”, “high”, “medium”, “low”, “minimal”, or “none”@returnThis pipe instance for fluent chaining
setReasoningConfig(config: env.ReasoningConfig): OpenRouterPipe
Sets the full reasoning configuration for reasoning-capable models.
@param configReasoning configuration with effort, maxTokens, exclude, enabled@returnThis pipe instance for fluent chaining
val pipe = OpenRouterPipe()
.setReasoningConfig(
env.ReasoningConfig(
effort = "high",
maxTokens = 8192,
enabled = true
)
)
Function Calling
setTools(tools: List<env.ToolDefinition>): OpenRouterPipe
Sets the tool definitions for function calling.
@param toolsList of tool definitions@returnThis pipe instance for fluent chaining
setToolChoice(choice: String): OpenRouterPipe
Sets the tool choice mode for function calling.
@param choiceTool choice: “auto”, “none”, or “required”@returnThis pipe instance for fluent chaining
setParallelToolCalls(enabled: Boolean): OpenRouterPipe
Sets whether to enable parallel function calling.
@param enabledTrue to enable parallel calls (default: true)@returnThis pipe instance for fluent chaining
Provider Options
setProviderPreferences(prefs: env.ProviderPreferences): OpenRouterPipe
Sets provider routing preferences to control which providers handle requests.
@param prefsProvider preferences configuration@returnThis pipe instance for fluent chaining
val pipe = OpenRouterPipe()
.setProviderPreferences(
env.ProviderPreferences(
order = listOf("openai", "anthropic"),
allow_fallbacks = true
)
)
setServiceTier(tier: String): OpenRouterPipe
Sets the service tier for request priority.
@param tierService tier: “auto”, “default”, “flex”, “priority”, or “scale”@returnThis pipe instance for fluent chaining
setPlugins(plugins: List<env.Plugin>): OpenRouterPipe
Sets OpenRouter plugins for extended functionality (web search, file parsing, etc.).
@param pluginsList of plugins to enable@returnThis pipe instance for fluent chaining
setOpenRouterUser(userId: String): OpenRouterPipe
Sets the end-user identifier for abuse detection. Note: Renamed from user to avoid collision with base class property.
@param userIdUser identifier@returnThis pipe instance for fluent chaining
Response Format
setResponseFormat(type: String, schema: kotlinx.serialization.json.JsonObject? = null): OpenRouterPipe
Sets the response format for structured output.
@param typeFormat type: “text”, “json_object”, or “json_schema”@param schemaOptional JSON schema for json_schema type@returnThis pipe instance for fluent chaining
setStructuredOutputs(enabled: Boolean): OpenRouterPipe
Sets whether to enable structured outputs via json_schema.
@param enabledTrue to enable structured outputs@returnThis pipe instance for fluent chaining
setVerbosity(level: String): OpenRouterPipe
Sets the response verbosity level.
@param levelVerbosity: “low”, “medium”, “high”, or “max”@returnThis pipe instance for fluent chaining
setLogprobs(enabled: Boolean): OpenRouterPipe
Sets whether to return log probabilities.
@param enabledTrue to return log probabilities@returnThis pipe instance for fluent chaining
setTopLogprobs(count: Int): OpenRouterPipe
Sets the number of top log probabilities to return.
@param countNumber of top log probabilities (0-20)@returnThis pipe instance for fluent chaining
Caching
setCacheControl(ttl: String): OpenRouterPipe
Sets cache control with TTL for Anthropic-style caching.
@param ttlCache TTL (e.g., “5m”, “1h”, “24h”)@returnThis pipe instance for fluent chaining
setSessionId(id: String): OpenRouterPipe
Sets the session ID for request grouping/observability.
@param idSession identifier@returnThis pipe instance for fluent chaining
Streaming
setStreamingEnabled(enabled: Boolean): OpenRouterPipe
Enables or disables streaming mode.
@param enabledTrue to enable streaming@returnThis pipe instance for fluent chaining
setStreamingCallback(callback: suspend (String) -> Unit): OpenRouterPipe
Registers a callback for streaming response chunks. Automatically enables streaming mode.
@param callbackSuspendable callback receiving text chunks@returnThis pipe instance for fluent chaining
Tracing Behavior
Non-streaming requests:
- Captures
inputTokens,outputTokens,totalTokensfrom API response - Records
responseLengthof the generated text - Includes
modelname from response - Sets
apiTypeto “ChatAPI”
Streaming requests:
- Captures
responseLengthof accumulated text - Traces streaming start and completion
- Sets
streamingflag to true
All requests trace:
provider(“OpenRouter”)baseUrlmodelpromptLengthstreamingenabled/disabled
Error Mapping
OpenRouterPipe maps API errors to [P2PError] types:
| Error Type | HTTP Code | P2PError |
|---|---|---|
| Authentication failure | 401 | P2PError.auth |
| Invalid API key | 400 | P2PError.prompt |
| Invalid request | 400 | P2PError.prompt |
| Rate limit | 429 | P2PError.transport |
| Server error | 5xx | P2PError.transport |
| Network timeout | - | P2PError.transport |