Portkey — AI Gateway for Production LLM Apps
You are an expert in Portkey, the AI gateway that sits between your app and LLM providers. You help developers add caching, fallbacks, load balancing, request retries, guardrails, semantic caching, budget limits, and observability to LLM calls — using a single unified API that works with 200+ models from OpenAI, Anthropic, Google, and open-source providers.
Core Capabilities
import Portkey from "portkey-ai";
const portkey = new Portkey({
apiKey: process.env.PORTKEY_API_KEY,
config: {
strategy: { mode: "fallback" }, // Auto-fallback on errors
targets: [
{
provider: "openai", api_key: process.env.OPENAI_KEY,
override_params: { model: "gpt-4o" },
weight: 0.7,
},
{
provider: "anthropic", api_key: process.env.ANTHROPIC_KEY,
override_params: { model: "claude-sonnet-4-20250514" },
weight: 0.3,
},
],
cache: { mode: "semantic", max_age: 3600 }, // Semantic caching
retry: { attempts: 3, on_status_codes: [429, 500, 503] },
},
});
// Use like OpenAI SDK — Portkey handles routing, caching, fallbacks
const response = await portkey.chat.completions.create({
messages: [{ role: "user", content: "Explain microservices" }],
max_tokens: 1024,
});
// Guardrails
const guarded = new Portkey({
apiKey: process.env.PORTKEY_API_KEY,
config: {
before_request_hooks: [{ type: "guardrail", id: "no-pii" }],
after_request_hooks: [{ type: "guardrail", id: "no-hallucination" }],
},
});
// Budget limits
// Set in Portkey dashboard: max $100/day per API key
Installation
npm install portkey-ai
# or
pip install portkey-ai
Best Practices
- OpenAI SDK compatible — Drop-in replacement; change import and add config; existing code works
- Fallbacks — Route to backup provider when primary fails; 99.99% effective uptime
- Semantic caching — Cache similar (not just identical) queries; 40-60% cache hit rate typical
- Load balancing — Split traffic across providers by weight; optimize cost vs quality
- Retry with backoff — Auto-retry on 429/500/503; configurable attempts and status codes
- Guardrails — PII detection, content moderation, hallucination checks; pre and post request
- Budget limits — Set per-key spending caps; prevent runaway costs from bugs or abuse
- Observability — Dashboard shows latency, cost, tokens, errors per provider; no additional SDK
1---2name: portkey3description: >-4 You are an expert in Portkey, the AI gateway that sits between your app and5 LLM providers. You help developers add caching, fallbacks, load balancing,6 request retries, guardrails, semantic caching, budget limits, and7 observability to LLM calls — using a single unified API that works with 200+8 models from OpenAI, Anthropic, Google, and open-source providers.9license: Apache-2.010compatibility: ''11metadata:12 source: https://skillmd.com/skills/eliferjunior/portkey13 author: terminal-skills14 version: 1.0.015 category: AI & Machine Learning16 tags:17 - llm18 - gateway19 - observability20 - routing21 - caching22 - guardrails23 - production24---2526# Portkey — AI Gateway for Production LLM Apps2728You are an expert in Portkey, the AI gateway that sits between your app and LLM providers. You help developers add caching, fallbacks, load balancing, request retries, guardrails, semantic caching, budget limits, and observability to LLM calls — using a single unified API that works with 200+ models from OpenAI, Anthropic, Google, and open-source providers.2930## Core Capabilities3132```typescript33import Portkey from "portkey-ai";3435const portkey = new Portkey({36 apiKey: process.env.PORTKEY_API_KEY,37 config: {38 strategy: { mode: "fallback" }, // Auto-fallback on errors39 targets: [40 {41 provider: "openai", api_key: process.env.OPENAI_KEY,42 override_params: { model: "gpt-4o" },43 weight: 0.7,44 },45 {46 provider: "anthropic", api_key: process.env.ANTHROPIC_KEY,47 override_params: { model: "claude-sonnet-4-20250514" },48 weight: 0.3,49 },50 ],51 cache: { mode: "semantic", max_age: 3600 }, // Semantic caching52 retry: { attempts: 3, on_status_codes: [429, 500, 503] },53 },54});5556// Use like OpenAI SDK — Portkey handles routing, caching, fallbacks57const response = await portkey.chat.completions.create({58 messages: [{ role: "user", content: "Explain microservices" }],59 max_tokens: 1024,60});6162// Guardrails63const guarded = new Portkey({64 apiKey: process.env.PORTKEY_API_KEY,65 config: {66 before_request_hooks: [{ type: "guardrail", id: "no-pii" }],67 after_request_hooks: [{ type: "guardrail", id: "no-hallucination" }],68 },69});7071// Budget limits72// Set in Portkey dashboard: max $100/day per API key73```7475## Installation7677```bash78npm install portkey-ai79# or80pip install portkey-ai81```8283## Best Practices84851. **OpenAI SDK compatible** — Drop-in replacement; change import and add config; existing code works862. **Fallbacks** — Route to backup provider when primary fails; 99.99% effective uptime873. **Semantic caching** — Cache similar (not just identical) queries; 40-60% cache hit rate typical884. **Load balancing** — Split traffic across providers by weight; optimize cost vs quality895. **Retry with backoff** — Auto-retry on 429/500/503; configurable attempts and status codes906. **Guardrails** — PII detection, content moderation, hallucination checks; pre and post request917. **Budget limits** — Set per-key spending caps; prevent runaway costs from bugs or abuse928. **Observability** — Dashboard shows latency, cost, tokens, errors per provider; no additional SDK