Skip to main content

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 RunConfig for Runner.run(...) and Runner.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)

  1. Create or open your project and install dependencies:
uv add openai-agents openrouter-oai-agentsdk
  1. Set your OpenRouter API key:
export OPENROUTER_API_KEY="sk-or-v1-..."
  1. Create main.py:
import asyncio

from openai.types.responses import ResponseTextDeltaEvent
from openai.types.shared import Reasoning

from agents import Agent, ModelSettings, Runner
from openrouter_oai_agentsdk import create_openrouter_run_config


def agent_settings(*, thinking: bool) -> ModelSettings:
    return ModelSettings(reasoning=None if thinking else Reasoning(effort="none"))


agent = Agent(
    name="Math Tutor",
    instructions=(
        "You provide help with math problems. "
        "Explain your reasoning at each step and include examples."
    ),
    model="z-ai/glm-5",
    model_settings=agent_settings(thinking=False),
)


async def main() -> None:
    result = Runner.run_streamed(
        agent,
        "How to calculate the fall rate for something?",
        run_config=create_openrouter_run_config(),
    )
    async for event in result.stream_events():
        if event.type == "raw_response_event" and isinstance(
            event.data, ResponseTextDeltaEvent
        ):
            print(event.data.delta, end="", flush=True)
    print()


if __name__ == "__main__":
    asyncio.run(main())
  1. Run it:
uv run python main.py

How this works:

  • create_openrouter_run_config() reads environment variables and returns a RunConfig.
  • Runner.run_streamed(..., run_config=...) uses OpenRouter through that provider.
  • Agent(model="z-ai/glm-5") picks the specific model for this run.

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 one
  • OPENROUTER_BASE_URL: defaults to https://openrouter.ai/api/v1
  • OPENROUTER_HTTP_REFERER: app/site URL for OpenRouter attribution
  • OPENROUTER_X_TITLE: app title for OpenRouter attribution
  • OPENROUTER_DISABLE_TRACING: true by default

Common errors

  • OPENROUTER_API_KEY is not set: export OPENROUTER_API_KEY before running.
  • No model provided: set Agent(model=...) or set OPENROUTER_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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openrouter_oai_agentsdk-0.1.1.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openrouter_oai_agentsdk-0.1.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file openrouter_oai_agentsdk-0.1.1.tar.gz.

File metadata

  • Download URL: openrouter_oai_agentsdk-0.1.1.tar.gz
  • Upload date:
  • Size: 6.5 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

Hashes for openrouter_oai_agentsdk-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d42115265b71dd8021cbe3672fbabc50a775681cfd41d7408d50fca0418e6b22
MD5 b5623eed891a12f169dcce7cf446b96b
BLAKE2b-256 7e4321a749f67b2f83b5dbc67e9919f17ba4f5d7d11f4dd909a08a0b91e8612e

See more details on using hashes here.

File details

Details for the file openrouter_oai_agentsdk-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: openrouter_oai_agentsdk-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.7 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

Hashes for openrouter_oai_agentsdk-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 af5285bb80d76e55ae765bcbfc353b5c39b2cecc691577196db9f54e13e50da4
MD5 69a2e9b0c4198efe32ac2d677bea0fe3
BLAKE2b-256 17acf2ff8ab34eb26a2b59486cc09746ff5e641a3a0066369eefeb96db96a906

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page