OpenRouter Responses

Interact with OpenRouter models using the OpenAI Responses API. OpenRouter's Responses API (currently in beta) provides OpenAI-compatible access to multiple AI models through a unified interface.

Requirements

Set the OPENROUTER_API_KEY environment variable:

1export OPENROUTER_API_KEY=your-api-key

Key Features

  • Unified Interface: Access multiple AI providers through a single API
  • Fallback Model Routing: Automatically try alternative models if the primary fails
  • Stateless API: Each request is independent (no server-side state persisted)
  • Reasoning Support: Enable reasoning for supported models

Parameters

ParameterTypeDefaultDescription
idstr"openai/gpt-oss-20b"The ID of the OpenRouter model to use
namestr"OpenRouterResponses"The name of the model
providerstr"OpenRouter"The provider of the model
api_keyOptional[str]NoneThe API key (defaults to OPENROUTER_API_KEY env var)
base_urlstr"https://openrouter.ai/api/v1"The base URL for the OpenRouter API
modelsOptional[List[str]]NoneList of fallback model IDs for automatic retry
storeOptional[bool]FalseWhether to store responses

Usage

Basic Usage

1from kern.agent import Agent
2from kern.models.openrouter import OpenRouterResponses
3
4agent = Agent(
5 model=OpenRouterResponses(id="openai/gpt-oss-20b"),
6 markdown=True,
7)
8
9agent.print_response("Share a 2 sentence horror story")

With Reasoning

1from kern.agent import Agent
2from kern.models.openrouter import OpenRouterResponses
3
4agent = Agent(
5 model=OpenRouterResponses(
6 id="openai/gpt-oss-20b",
7 reasoning={"enabled": True},
8 ),
9 markdown=True,
10)
11
12agent.print_response("Share a 2 sentence horror story")

Fallback Model Routing

If the primary model fails due to rate limits, timeouts, or unavailability, OpenRouter automatically tries the fallback models in order:

1from kern.agent import Agent
2from kern.models.openrouter import OpenRouterResponses
3
4agent = Agent(
5 model=OpenRouterResponses(
6 id="openai/gpt-oss-20b",
7 models=[
8 "openai/gpt-oss-20b",
9 "openai/gpt-4o",
10 ],
11 ),
12 markdown=True,
13)
14
15agent.print_response("Write a haiku about coding", stream=True)

Developer Resources