openrouter-upgrade-migration
jeremylongshore/claude-code-plugins-plus-skills
安全地遷移與升級 OpenRouter SDK 版本。適用於更新依賴項或遷移設定時。 可透過「openrouter upgrade」、「openrouter migration」、「update openrouter」、「openrouter breaking changes」等短語觸發此功能。
...展開全部關於openrouter-upgrade-migration
「openrouter-upgrade-migration 」這項技能旨在簡化 OpenRouter SDK 版本的升級與遷移流程。它解決了更新依賴項時常見的挑戰,確保開發人員能夠安全地執行版本更新並管理配置遷移,同時避免引入破壞性變更。 此技能在執行例行更新或因應新版 SDK 中的破壞性變更時特別有用,提供了一套在升級過程中維持相容性的系統化方法。
此技能提供了一套完整的工具,用於管理 SDK 版本升級,從驗證先決條件到確保整合成功。 主要功能包括輕鬆遷移設定、處理破壞性變更,以及在遷移後驗證 API 連線狀態。它與版本控制系統整合良好,可在發生問題時提供回滾功能。透過明確的測試與監控步驟,此技能可確保更新後的 OpenRouter 整合在生產環境中持續如預期般運作。
此技能主要針對使用 OpenRouter 的開發人員和團隊,對於負責管理 SDK 更新或面臨新版本破壞性變更的人員而言,是理想的解決方案。 對於負責維護生產環境系統的 DevOps 工程師而言,此技能同樣大有裨益,因為它能讓他們在追蹤變更的同時,安全地升級依賴項與設定。此工具可協助確保系統穩定性,並在更新或遷移 OpenRouter SDK 版本時將停機時間降至最低。
常見問題
如何觸發「openrouter-upgrade-migration 」技能?
您可以使用「openrouter upgrade」、「openrouter migration」、「update openrouter」或「openrouter breaking changes」等短語來觸發此技能。
使用此技能有哪些先決條件?
使用此技能的先決條件包括:已建立 OpenRouter 整合,並具備版本控制機制以確保可回滾。
此技能是否與所有版本的 OpenRouter SDK 皆相容?
此技能旨在用於升級和遷移 OpenRouter SDK 版本,但在進行操作前,務必先確認與您特定 SDK 版本的相容性。
若遷移失敗該怎麼辦?
若遷移失敗,請參閱位於 '{baseDir}/references/errors.md' 中的錯誤處理文件,以診斷並解決問題。
OpenRouter Upgrade & Migration
Current State
!npm list openai 2>/dev/null | head -5!pip show openai 2>/dev/null | head -5
Overview
Migrating to OpenRouter from a direct provider API (OpenAI, Anthropic) is minimal: change base_url and api_key, add two headers. The OpenAI SDK works natively with OpenRouter. This skill covers migrating from direct APIs, switching between models, upgrading SDK versions, and running comparison tests.
Migration from Direct OpenAI
# BEFORE: Direct OpenAIfrom openai import OpenAIclient = OpenAI(api_key=os.environ["OPENAI_API_KEY"])response = client.chat.completions.create( model="gpt-4o", messages=[{"role": "user", "content": "Hello"}], max_tokens=200,)# AFTER: Via OpenRouter (3 lines changed)from openai import OpenAIclient = OpenAI( base_url="https://openrouter.ai/api/v1", # ← Changed api_key=os.environ["OPENROUTER_API_KEY"], # ← Changed default_headers={ # ← Added "HTTP-Referer": "https://my-app.com", "X-Title": "my-app", },)response = client.chat.completions.create( model="openai/gpt-4o", # ← Add provider prefix messages=[{"role": "user", "content": "Hello"}], max_tokens=200,)
Migration from Direct Anthropic
# BEFORE: Direct Anthropic SDKimport anthropicclient = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])response = client.messages.create( model="claude-3-5-sonnet-20241022", max_tokens=200, messages=[{"role": "user", "content": "Hello"}],)content = response.content[0].text# AFTER: Via OpenRouter (using OpenAI SDK instead of Anthropic SDK)from openai import OpenAIclient = OpenAI( base_url="https://openrouter.ai/api/v1", api_key=os.environ["OPENROUTER_API_KEY"], default_headers={ "HTTP-Referer": "https://my-app.com", "X-Title": "my-app", },)response = client.chat.completions.create( model="anthropic/claude-3.5-sonnet", # OpenRouter model ID messages=[{"role": "user", "content": "Hello"}], max_tokens=200,)content = response.choices[0].message.content # OpenAI response format
TypeScript Migration
// BEFORE: Direct OpenAIimport OpenAI from "openai";const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });// AFTER: Via OpenRouterconst client = new OpenAI({ baseURL: "https://openrouter.ai/api/v1", apiKey: process.env.OPENROUTER_API_KEY, defaultHeaders: { "HTTP-Referer": "https://my-app.com", "X-Title": "my-app", },});// Change model from "gpt-4o" to "openai/gpt-4o"
Migration Checklist
MIGRATION_CHECKLIST = { "config": [ "base_url changed to https://openrouter.ai/api/v1", "API key changed to OPENROUTER_API_KEY (sk-or-v1-...)", "HTTP-Referer and X-Title headers added", "Model IDs prefixed with provider/ (e.g., openai/gpt-4o)", ], "code": [ "All client initialization updated", "Model IDs updated in all routes/configs", "Error handling covers OpenRouter-specific codes (402, 408)", "Streaming still works with new endpoint", "Tool/function calling still works", ], "testing": [ "Same prompts produce comparable quality output", "Latency within acceptable range (expect +50-100ms)", "Token counts match expectations", "Cost tracking updated for OpenRouter pricing", "Fallback chain tested", ], "operations": [ "Credit balance sufficient for expected usage", "Per-key credit limits configured", "Monitoring updated to track OpenRouter metrics", "Alerting on new error codes (402, 408)", "Rollback plan documented", ],}
Model ID Migration Map
| Direct Provider | OpenRouter ID |
|---|---|
gpt-4o | openai/gpt-4o |
gpt-4o-mini | openai/gpt-4o-mini |
o1 | openai/o1 |
claude-3-5-sonnet-20241022 | anthropic/claude-3.5-sonnet |
claude-3-haiku-20240307 | anthropic/claude-3-haiku |
gemini-2.0-flash | google/gemini-2.0-flash-001 |
llama-3.1-8b-instruct | meta-llama/llama-3.1-8b-instruct |
Comparison Test Script
def compare_migration(prompt: str, old_model: str, new_model: str): """Run same prompt through old and new configurations to compare.""" import time # New: OpenRouter or_client = OpenAI( base_url="https://openrouter.ai/api/v1", api_key=os.environ["OPENROUTER_API_KEY"], default_headers={"HTTP-Referer": "https://my-app.com", "X-Title": "migration-test"}, ) start = time.monotonic() or_response = or_client.chat.completions.create( model=new_model, messages=[{"role": "user", "content": prompt}], max_tokens=200, temperature=0, ) or_latency = (time.monotonic() - start) * 1000 return { "openrouter": { "model": or_response.model, "content": or_response.choices[0].message.content[:100], "tokens": or_response.usage.prompt_tokens + or_response.usage.completion_tokens, "latency_ms": round(or_latency), }, }# Testresult = compare_migration( "What is 2+2?", old_model="gpt-4o", new_model="openai/gpt-4o",)print(json.dumps(result, indent=2))
Feature Flag Migration
import osUSE_OPENROUTER = os.environ.get("USE_OPENROUTER", "false").lower() == "true"def get_llm_client(): """Feature flag for gradual migration.""" if USE_OPENROUTER: return OpenAI( base_url="https://openrouter.ai/api/v1", api_key=os.environ["OPENROUTER_API_KEY"], default_headers={"HTTP-Referer": "https://my-app.com", "X-Title": "my-app"}, ) else: return OpenAI(api_key=os.environ["OPENAI_API_KEY"])def get_model_id(model: str) -> str: """Map model IDs based on current backend.""" if USE_OPENROUTER and "/" not in model: MODEL_MAP = {"gpt-4o": "openai/gpt-4o", "gpt-4o-mini": "openai/gpt-4o-mini"} return MODEL_MAP.get(model, f"openai/{model}") return model
Error Handling
| Error | Cause | Fix |
|---|---|---|
| 401 after migration | Using old API key with new base_url | Update to OpenRouter API key (sk-or-v1-...) |
model_not_found | Missing provider prefix | Add openai/ or anthropic/ prefix to model ID |
| Different response format | Switched from Anthropic SDK to OpenAI SDK | Update response parsing: .choices[0].message.content |
| Higher latency | OpenRouter proxy overhead | Expected: +50-100ms; use streaming to mask it |
Enterprise Considerations
- Migration from direct provider to OpenRouter requires only 3 lines of code change
- Use feature flags for gradual migration (10% -> 50% -> 100%)
- Run comparison tests on critical prompts before full migration
- OpenRouter adds ~50-100ms overhead; use streaming to mask perceived latency
- Keep direct provider keys active during migration for quick rollback
- Update monitoring dashboards for OpenRouter-specific metrics (generation_id, provider used)
References
- Examples | Errors
- Quickstart | OpenAI Compatibility
安裝 openrouter-upgrade-migration
請下載並將技能檔案解壓縮至您的 .claude/skills/ 目錄中。
下載 ZIP複製儲存庫並將技能檔案複製到您的專案中。
git clone https://github.com/jeremylongshore/claude-code-plugins-plus-skills/blob/main/plugins/saas-packs/openrouter-pack/skills/openrouter-upgrade-migration/SKILL.md # Copy SKILL.md to your .claude/skills/ directory
複製





首頁
