Plug OpenRouter into the OpenAI Agents SDK with a drop-in model provider and RunConfig.
Project description
openrouter-oai-agentsdk
Use OpenRouter models inside the OpenAI Agents SDK without changing your agent architecture.
This package provides:
- an OpenRouter-backed
ModelProvider - a ready-to-use
RunConfigforRunner.run(...)andRunner.run_streamed(...) - environment-based configuration with optional OpenRouter attribution headers
Install
uv add openrouter-oai-agentsdk
Or:
pip install openrouter-oai-agentsdk
What problem this solves
The OpenAI Agents SDK expects a model provider. This library plugs OpenRouter in as that provider, so you can keep using:
Agent(...)Runner.run(...)/Runner.run_streamed(...)ModelSettings(...)
Instead of wiring your own client/provider every time, you create one run config from this package and pass it to Runner.
Quick start (step by step)
- Create or open your project and install dependencies:
uv add openai-agents openrouter-oai-agentsdk
- Set your OpenRouter API key:
export OPENROUTER_API_KEY="sk-or-v1-..."
- Create
main.py:
from agents import Agent, Runner
from openrouter_oai_agentsdk import create_openrouter_run_config
agent = Agent(
name="Assistant",
instructions="You are a helpful assistant.",
model="openai/gpt-4o-mini",
)
result = Runner.run_sync(
agent,
"In one sentence, what is gravity?",
run_config=create_openrouter_run_config(),
)
print(result.final_output)
- Run it:
uv run python main.py
What each line does:
Agent(...)defines behavior and the model you want.create_openrouter_run_config()wires OpenRouter as the provider.Runner.run_sync(...)executes one request using that provider.result.final_outputprints the agent's final text response.
If you already set OPENROUTER_MODEL, you can omit model=... on Agent(...).
Next step: streaming (optional)
Use Runner.run_streamed(...) only when you want token-by-token output in real time. Start with run_sync first.
Usage patterns
Pattern A: zero-config run config from env
from openrouter_oai_agentsdk import OPENROUTER_RUN_CONFIG
from agents import Runner
# Pass OPENROUTER_RUN_CONFIG directly to Runner calls.
Pattern B: explicit settings in code
from openrouter_oai_agentsdk import OpenRouterSettings, create_openrouter_run_config
settings = OpenRouterSettings(
api_key="sk-or-v1-...",
fallback_model="openai/gpt-5-mini",
http_referer="https://your-app.example",
x_title="Your App Name",
)
run_config = create_openrouter_run_config(settings=settings)
Use this pattern when you want fully explicit config (service containers, tests, multi-tenant apps).
Environment variables
Required:
OPENROUTER_API_KEY: your OpenRouter key (sk-or-v1-...)
Optional:
OPENROUTER_MODEL: fallback model if an agent does not define oneOPENROUTER_BASE_URL: defaults tohttps://openrouter.ai/api/v1OPENROUTER_HTTP_REFERER: app/site URL for OpenRouter attributionOPENROUTER_X_TITLE: app title for OpenRouter attributionOPENROUTER_DISABLE_TRACING:trueby default
Common errors
OPENROUTER_API_KEY is not set: exportOPENROUTER_API_KEYbefore running.No model provided: setAgent(model=...)or setOPENROUTER_MODEL.
Import compatibility
The preferred import is:
from openrouter_oai_agentsdk import OPENROUTER_RUN_CONFIG
Legacy import path is still supported:
from openrouter_provider import OPENROUTER_RUN_CONFIG
Build and publish (maintainers)
Build distributions:
uv build --no-sources
Bump version:
uv version --bump patch
Publish to PyPI:
uv publish --token "$UV_PUBLISH_TOKEN"
Publish to a configured custom index:
uv publish --index testpypi --token "$UV_PUBLISH_TOKEN"
Trusted publishing from GitHub Actions is also supported via .github/workflows/publish.yml.
Verify install/import (without local project shadowing):
uv run --with openrouter-oai-agentsdk --no-project -- \
python -c "import openrouter_oai_agentsdk; print(openrouter_oai_agentsdk.__version__)"
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file openrouter_oai_agentsdk-0.1.2.tar.gz.
File metadata
- Download URL: openrouter_oai_agentsdk-0.1.2.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
be2a8eeaeefb832b1eac4954721ade170de62b1695b7f9511c45b399d1d45d4a
|
|
| MD5 |
99245aeb6322b34c27209227c7c89181
|
|
| BLAKE2b-256 |
3f597c44a0a84ddbc59eac02b8f0dc880b7906903db1a914560e2f4c102ba6d8
|
File details
Details for the file openrouter_oai_agentsdk-0.1.2-py3-none-any.whl.
File metadata
- Download URL: openrouter_oai_agentsdk-0.1.2-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c1f6d24cc7fc7ee95016b9872bdaa4180bdba2c346557bc6d361518d256eec8
|
|
| MD5 |
03b8ab6df15481ea8a885b1f646ae8e9
|
|
| BLAKE2b-256 |
af8c3c10566618d7cc2a1b761b402177ebd49bef518fd5eb55cfc81ae89dd675
|