# Portkey AI Gateway

> Route to hundreds of LLMs through one fast API, with retries, fallbacks, and guardrails

- Category: Evaluation & Production Ops / Gateways & Routing
- Language: TypeScript
- Repository: https://github.com/Portkey-AI/gateway
- Website: https://portkey.ai
- Canonical page: https://ai-tldr.dev/tools/portkey-ai-gateway/

## In short

Portkey AI Gateway is an open-source service that sits between your application and language model providers. It sits in the Gateways & Routing category of the Evaluation & Production Ops tools and is open-source, written in TypeScript. To get started: start the gateway locally.

## Overview

Portkey AI Gateway is an open-source service that sits between your application and language model providers. You send requests to the gateway using one consistent API, and it forwards them to whichever provider you choose - OpenAI, Anthropic, Bedrock, Groq, and many others - so you can switch models without rewriting your client code.

It is built for teams running AI in production. On top of routing, the gateway adds reliability features like automatic retries and fallbacks, plus routing rules and guardrails you configure per request. The README reports it is lightweight (around 122kb) and adds under 1ms of latency.

As an LLM observability and gateway tool, it gives you a single place to watch your traffic. The gateway ships with a local console that shows your logs, and it integrates with the OpenAI SDKs, LangChain, LlamaIndex, and agent frameworks so it slots into existing stacks.

## What it does

- One API that routes to 250+ language, vision, audio, and image models across many providers
- Automatic retries and fallbacks to prevent downtime in production traffic
- Load balancing and conditional routing to scale AI workloads
- Guardrails you attach per request, such as denying responses that contain specified words
- A built-in Gateway Console for viewing local request logs in one place
- Runs locally via npx, or deploys to Docker, Node.js, Cloudflare Workers, and Replit

## Getting started

Run the gateway locally with npx, then send your first request through it using the Portkey Python client. You need Node.js and npm installed.

1. Start the gateway locally

   Run the gateway with npx. It starts on http://localhost:8787/v1, with the console at http://localhost:8787/public/.

```bash
# Run the gateway locally (needs Node.js and npm)
npx @portkey-ai/gateway
```

2. Install the Python client

   Install the portkey-ai package to call the gateway from Python.

```bash
pip install -qU portkey-ai
```

3. Make your first request

   Create a client, point it at a provider, pass that provider's API key, and send a chat completion.

```python
from portkey_ai import Portkey

# OpenAI compatible client
client = Portkey(
    provider="openai",  # or 'anthropic', 'bedrock', 'groq', etc
    Authorization="sk-***"  # the provider API key
)

# Make a request through your AI Gateway
client.chat.completions.create(
    messages=[{"role": "user", "content": "What's the weather like?"}],
    model="gpt-4o-mini"
)
```

4. Add routing and guardrails

   Attach a config to the client to set retry rules and guardrails. This example retries up to 5 times and denies any reply containing the word Apple.

```python
config = {
  "retry": {"attempts": 5},
  "output_guardrails": [{
    "default.contains": {"operator": "none", "words": ["Apple"]},
    "deny": True
  }]
}

# Attach the config to the client
client = client.with_options(config=config)

client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Reply randomly with Apple or Bat"}]
)
```

## When to use it

- Swap between LLM providers (OpenAI, Anthropic, Bedrock, Groq) without changing your application code
- Keep an AI feature running through provider outages using automatic retries and fallbacks
- Apply guardrails to model output, such as blocking responses that contain certain words
- Inspect and debug your LLM request logs from a single local console
