AGENT SKILL

portkey

Portkey AI gateway — unified LLM API, load balancing, fallbacks, caching, guardrails, observability

License 许可
Apache-2.0
最近更新
2026-11-11
Stars
★ 13
Forks
1

01 前言 Front Matter

name
portkey
see_also
[ "litellm", "langfuse", "ollama-patterns" ]
triggers
[ "portkey", "portkey gateway", "llm gateway portkey", "portkey fallback", "portkey load balance", "portkey cache", "portkey guardrails", "unified llm api", "portkey virtual key", "portkey config" ]
description
Portkey AI gateway — unified LLM API, load balancing, fallbacks, caching, guardrails, observability
do_not_use_for
[ "single model calls without routing — use litellm directly", "local model serving — use ollama", "evaluation — use ragas/deepeval" ]

03 SKILL.md 正文

Portkey — AI Gateway

Setup & Virtual Keys

from portkey_ai import Portkey

# Virtual keys abstract provider credentials
client = Portkey(
    api_key="pk-...",                   # Portkey API key
    virtual_key="anthropic-vk-...",     # Virtual key for provider
)

# Drop-in OpenAI replacement
response = client.chat.completions.create(
    model="claude-sonnet-4-6",
    messages=[{"role": "user", "content": "Hello"}],
)
print(response.choices[0].message.content)

Configs — Gateway Routing

from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL
import openai

# Config-based routing — define in Portkey dashboard or inline
config = {
    "strategy": {
        "mode": "fallback",             # fallback | loadbalance | single
    },
    "targets": [
        {
            "virtual_key": "anthropic-vk",
            "override_params": {"model": "claude-sonnet-4-6"},
            "weight": 0.7,
        },
        {
            "virtual_key": "openai-vk",
            "override_params": {"model": "gpt-4o"},
            "weight": 0.3,
        },
    ],
}

# Use with openai client (Portkey as proxy)
openai_client = openai.OpenAI(
    api_key="pk-...",
    base_url=PORTKEY_GATEWAY_URL,
    default_headers=createHeaders(
        api_key="pk-...",
        config=config,
    ),
)

Load Balancing

lb_config = {
    "strategy": {"mode": "loadbalance"},
    "targets": [
        {"virtual_key": "openai-vk-1", "weight": 50},
        {"virtual_key": "openai-vk-2", "weight": 30},
        {"virtual_key": "openai-vk-3", "weight": 20},
    ],
}

client = Portkey(api_key="pk-...", config=lb_config)

Fallback with Retry

fallback_config = {
    "strategy": {"mode": "fallback"},
    "targets": [
        {
            "virtual_key": "primary-vk",
            "override_params": {"model": "claude-opus-4-7"},
            "retry": {"attempts": 2, "on_status_codes": [429, 500, 502, 503]},
        },
        {
            "virtual_key": "backup-vk",
            "override_params": {"model": "gpt-4o"},
        },
    ],
}

client = Portkey(api_key="pk-...", config=fallback_config)
response = client.chat.completions.create(
    model="claude-opus-4-7",            # overridden by config
    messages=[{"role": "user", "content": "Complex task"}],
)

Caching

cache_config = {
    "cache": {
        "mode": "semantic",             # simple | semantic
        "max_age": 3600,                # seconds
    },
    "virtual_key": "openai-vk",
}

client = Portkey(api_key="pk-...", config=cache_config)
# Second identical (or semantically similar) call served from cache
r1 = client.chat.completions.create(model="gpt-4o", messages=[...])
r2 = client.chat.completions.create(model="gpt-4o", messages=[...])
print(r2.extensions.cache_status)      # HIT

Guardrails

# Input/output guardrails — defined in Portkey dashboard
guardrail_config = {
    "virtual_key": "anthropic-vk",
    "guardrails": {
        "input_guardrails": ["no-pii", "topic-restriction"],
        "output_guardrails": ["no-harmful-content", "length-check"],
        "on_fail": "block",             # block | warn | replace
    },
}

client = Portkey(api_key="pk-...", config=guardrail_config)
try:
    response = client.chat.completions.create(
        model="claude-sonnet-4-6",
        messages=[{"role": "user", "content": user_input}],
    )
except Exception as e:
    # Guardrail blocked the request
    print(f"Blocked: {e}")

Observability & Metadata

client = Portkey(
    api_key="pk-...",
    virtual_key="anthropic-vk",
)

response = client.with_options(
    metadata={
        "user_id": "user-123",
        "session_id": "sess-456",
        "environment": "production",
        "_prompt": "rag-v2",            # prompt name for dashboard grouping
    }
).chat.completions.create(
    model="claude-sonnet-4-6",
    messages=[{"role": "user", "content": query}],
)
# View traces at app.portkey.ai/logs

Prompt Templates

# Fetch and render prompts from Portkey dashboard
from portkey_ai import Portkey

client = Portkey(api_key="pk-...")
prompt = client.prompts.completions.create(
    prompt_id="pp-my-prompt-id",
    variables={"context": doc_context, "question": user_query},
)
print(prompt.choices[0].message.content)

Anti-Fake-Pass Checks

  • Virtual keys are created in Portkey dashboard — they proxy provider API keys server-side
  • config can be a dict (inline) or a string config ID from the dashboard
  • strategy.mode: "fallback" tries targets in order on error — not round-robin
  • Semantic cache requires sentence-transformer similarity — first call always misses
  • Guardrails must be created in dashboard first — referencing by name only
  • with_options() returns a new client instance — does not mutate the original

04 同仓库更多 Skills

Horonomos-laps
Horonomos-laps/Yana-AI

openai--sentry--sentry

Use when the user asks to inspect Sentry issues or events, summarize recent production errors, or pull basic Sentry health data via the Sentry API; perform read-only queries with the bundled script and require `SENTRY_AUTH_TOKEN`.

★ 13
Horonomos-laps
Horonomos-laps/Yana-AI

ito-basket-compare

Compare Itô prediction-market baskets against a user's knowledge base, portfolio notes, financial context, watchlist, or research thesis. Use for read-only basket comparison and gap analysis without investment advice or live trading.

★ 13
Horonomos-laps
Horonomos-laps/Yana-AI

implementing-network-traffic-baselining

Build network traffic baselines from NetFlow/IPFIX data using Python pandas for statistical analysis, z-score anomaly detection, and hourly/daily traffic pattern profiling

★ 13
Horonomos-laps
Horonomos-laps/Yana-AI

detecting-evasion-techniques-in-endpoint-logs

Detects defense evasion techniques used by adversaries in endpoint logs including log tampering, timestomping, process injection, and security tool disabling. Use when investigating suspicious endpoint behavior, building detection rules for evasion tactics, or conducting threat hunting for stealthy adversary activity. Activates for requests involving evasion detection, defense evasion analysis, log tampering detection, or MITRE ATT&CK TA0005.

★ 13
Horonomos-laps
Horonomos-laps/Yana-AI

implementing-microsegmentation-with-guardicore

Implementing microsegmentation using Akamai Guardicore Segmentation to map application dependencies, create granular network policies, visualize east-west traffic flows, and enforce least-privilege communication between workloads across data centers and cloud.

★ 13
Horonomos-laps
Horonomos-laps/Yana-AI

ramdisk-tmpfs-patterns

暂无简介

★ 13