Index of every page on this site: llms.txt
All skills
jeremylongshore avatar

/openrouter-openai-compat

@0d57af8
Run it yourself

Migrate from OpenAI to OpenRouter with minimal code changes. Use when switching to OpenRouter or maintaining dual compatibility. Triggers: 'openrouter openai compatible', 'openrouter drop-in', 'openai to openrouter', 'openrouter migration'.

Use this Skill: https://skilld.dev/gh/jeremylongshore/tons-of-skills-marketplace/openrouter-openai-compat

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesexamples.md

≈868 tokens on demand. Your agent reads this file only when SKILL.md points to it.

OpenAI Compatibility Examples

Python — Drop-In Replacement

import os
from openai import OpenAI

# BEFORE: Direct OpenAI
# client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# model = "gpt-3.5-turbo"

# AFTER: OpenRouter (2 lines changed)
client = OpenAI(
    base_url="https://openrouter.ai/api/v1",           # Changed
    api_key=os.environ["OPENROUTER_API_KEY"],           # Changed
    default_headers={
        "HTTP-Referer": "https://your-app.com",        # Optional: for analytics
        "X-Title": "Your App Name",                     # Optional: for analytics
    },
)
model = "openai/gpt-3.5-turbo"                          # Changed: add provider prefix

# Everything below is IDENTICAL to OpenAI SDK usage
response = client.chat.completions.create(
    model=model,
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is the capital of France?"},
    ],
    max_tokens=100,
    temperature=0.7,
)

print(response.choices[0].message.content)
print(f"Model: {response.model}")
print(f"Tokens: {response.usage.total_tokens}")

TypeScript — Switching Between OpenAI and OpenRouter

import OpenAI from "openai";

function createClient(provider: "openai" | "openrouter"): OpenAI {
  if (provider === "openai") {
    return new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
  }
  return new OpenAI({
    baseURL: "https://openrouter.ai/api/v1",
    apiKey: process.env.OPENROUTER_API_KEY,
  });
}

function modelId(base: string, provider: "openai" | "openrouter"): string {
  // OpenRouter requires provider prefix; OpenAI does not
  if (provider === "openrouter" && !base.includes("/")) {
    return `openai/${base}`;
  }
  return base;
}

// Identical code works with both providers
async function chat(prompt: string, provider: "openai" | "openrouter" = "openrouter") {
  const client = createClient(provider);
  const model = modelId("gpt-3.5-turbo", provider);

  const res = await client.chat.completions.create({
    model,
    messages: [{ role: "user", content: prompt }],
    max_tokens: 100,
  });

  return res.choices[0].message.content;
}

// Use OpenRouter (accesses 100+ models)
const result = await chat("Hello!", "openrouter");
console.log(result);

// Or use OpenAI directly (same code path)
// const result = await chat("Hello!", "openai");

Python — Access Non-OpenAI Models with Same SDK

# The key benefit: use the OpenAI SDK to access ANY provider

# Anthropic Claude
response = client.chat.completions.create(
    model="anthropic/claude-3.5-sonnet",
    messages=[{"role": "user", "content": "Hello from Claude via OpenAI SDK!"}],
    max_tokens=100,
)
print(f"Claude says: {response.choices[0].message.content}")

# Google Gemini
response = client.chat.completions.create(
    model="google/gemini-pro",
    messages=[{"role": "user", "content": "Hello from Gemini via OpenAI SDK!"}],
    max_tokens=100,
)
print(f"Gemini says: {response.choices[0].message.content}")

# Meta Llama (free)
response = client.chat.completions.create(
    model="meta-llama/llama-3-8b-instruct",
    messages=[{"role": "user", "content": "Hello from Llama via OpenAI SDK!"}],
    max_tokens=100,
)
print(f"Llama says: {response.choices[0].message.content}")

Tons of Skills by Intent Solutions | jeremylongshore.com

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts2mo3 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    The skill provides legitimate documentation and code examples for migrating from the OpenAI SDK to OpenRouter. No security issues were detected.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: LOW · No issues

Signed by skilld at 0d57af8. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub yesterday.

Activeupdated 3 months ago
What it can do
Reads files Edits files Runs commands
version
1.20.0
author
Jeremy Longshore <jeremy@intentsolutions.io>
tags
[
  "saas",
  "openrouter",
  "api",
  "migration",
  "openai"
]
All 6 allowed tools
ReadWriteEditGrepBash(python3:*)Bash(node:*)
Other metadata
compatibility
Designed for Claude Code

README badge

README badge for jeremylongshore/tons-of-skills-marketplace/openrouter-openai-compat