OpenRouter

Use OpenRouter unified API with Kern agents.

OpenRouter is a platform for providing endpoints for Large Language models.

Authentication

Set your OPENROUTER_API_KEY environment variable. Get your key from here.

1export OPENROUTER_API_KEY=***
1setx OPENROUTER_API_KEY ***

Example

Use OpenRouter with your Agent:

1from kern.agent import Agent
2from kern.models.openrouter import OpenRouter
3
4agent = Agent(
5 model=OpenRouter(id="gpt-5-mini"),
6 markdown=True
7)
8
9# Print the response in the terminal
10agent.print_response("Share a 2 sentence horror story.")

Params

ParameterTypeDefaultDescription
idstr"openai/gpt-4o-mini"The id of the OpenRouter model to use
namestr"OpenRouter"The name of the model
providerstr"OpenRouter"The provider of the model
api_keyOptional[str]NoneThe API key for OpenRouter (defaults to OPENROUTER_API_KEY env var)
base_urlstr"https://openrouter.ai/api/v1"The base URL for the OpenRouter API
app_nameOptional[str]"kern"Application name for OpenRouter request headers

OpenRouter also supports the params of OpenAI.

Responses API

OpenRouter also supports the OpenAI Responses API (currently in beta). Use OpenRouterResponses for this interface:

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")

Fallback Model Routing

OpenRouterResponses supports automatic fallback to alternative models if the primary model fails:

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=["openai/gpt-oss-20b", "openai/gpt-4o"],
8 ),
9 markdown=True,
10)
11
12agent.print_response("Write a haiku about coding", stream=True)

See OpenRouterResponses reference for full parameters.

Prompt caching

Prompt caching will happen automatically using our OpenRouter model, when the used provider supports it. In other cases you can activate it via the cache_control header. You can read more about prompt caching with OpenRouter in their docs.