openrouter
openrouter
¶
OpenRouter HTTP backend for multi-model access via OpenAI-compatible API.
Enables Marianne to use any model available on OpenRouter (free and paid) through a single HTTP backend. Uses the OpenAI-compatible chat completions endpoint at https://openrouter.ai/api/v1/chat/completions.
Key design decisions: - Extends Backend ABC with HttpxClientMixin for lazy httpx client lifecycle. - Model is specified per-request, allowing per-sheet instrument overrides. - Rate limit detection from HTTP 429 status and Retry-After header. - Token usage extracted from the standard OpenAI usage response field. - Free-tier model support (no cost for many models).
Security: API keys are NEVER logged. The key is read from environment and passed only in the Authorization header. The logging infrastructure uses SENSITIVE_PATTERNS to automatically redact fields containing 'api_key', 'token', 'secret', etc.
Classes¶
OpenRouterBackend
¶
OpenRouterBackend(model=_DEFAULT_MODEL, api_key_env='OPENROUTER_API_KEY', max_tokens=16384, temperature=0.7, timeout_seconds=300.0, base_url=_OPENROUTER_BASE_URL)
Bases: HttpxClientMixin, Backend
Run prompts via the OpenRouter API (OpenAI-compatible).
Provides direct HTTP access to 300+ models including free-tier options. Uses HttpxClientMixin for lazy, connection-pooled httpx client lifecycle.
Example usage::
backend = OpenRouterBackend(
model="minimax/minimax-m1-80k",
api_key_env="OPENROUTER_API_KEY",
)
result = await backend.execute("Explain quicksort")
Initialize OpenRouter backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Model ID (e.g., 'minimax/minimax-m1-80k', 'google/gemma-4'). |
_DEFAULT_MODEL
|
api_key_env
|
str
|
Environment variable containing API key. |
'OPENROUTER_API_KEY'
|
max_tokens
|
int
|
Maximum tokens for response. |
16384
|
temperature
|
float
|
Sampling temperature (0.0-2.0). |
0.7
|
timeout_seconds
|
float
|
Maximum time for API request. |
300.0
|
base_url
|
str
|
OpenRouter API base URL (without endpoint path). |
_OPENROUTER_BASE_URL
|
Source code in src/marianne/backends/openrouter.py
Attributes¶
Functions¶
from_config
classmethod
¶
Create backend from a BackendConfig.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
object
|
A BackendConfig instance (typed as object to avoid circular import — BackendConfig lives in core.config). |
required |
Returns:
| Type | Description |
|---|---|
OpenRouterBackend
|
Configured OpenRouterBackend instance. |
Source code in src/marianne/backends/openrouter.py
apply_overrides
¶
Apply per-sheet overrides for the next execution.
Source code in src/marianne/backends/openrouter.py
clear_overrides
¶
Restore original backend parameters after per-sheet execution.
Source code in src/marianne/backends/openrouter.py
set_preamble
¶
set_prompt_extensions
¶
set_output_log_path
¶
Set base path for real-time output logging.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | None
|
Base path for log files (without extension), or None to disable. |
required |
Source code in src/marianne/backends/openrouter.py
execute
async
¶
Execute a prompt via the OpenRouter API.
Sends a chat completion request to OpenRouter's OpenAI-compatible endpoint and returns the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The prompt to send. |
required |
timeout_seconds
|
float | None
|
Per-call timeout override. Logged but not enforced (httpx client timeout from init is used). |
None
|
Returns:
| Type | Description |
|---|---|
ExecutionResult
|
ExecutionResult with API response and metadata. |
Source code in src/marianne/backends/openrouter.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | |
health_check
async
¶
Check if the OpenRouter API is reachable and authenticated.
Uses the /models endpoint (lightweight, no token consumption) to verify connectivity and authentication.
Returns:
| Type | Description |
|---|---|
bool
|
True if healthy, False otherwise. |
Source code in src/marianne/backends/openrouter.py
availability_check
async
¶
Check if the backend can be initialized without consuming API quota.
Verifies that the API key is present and the httpx client can be created. Does NOT make any HTTP requests.