MCPcopy Index your code
hub / github.com/Portkey-AI/gateway

github.com/Portkey-AI/gateway @v1.15.2 sqlite

repository ↗ · DeepWiki ↗ · release v1.15.2 ↗
1,851 symbols 3,559 edges 585 files 30 documented · 2%
README

English | 中文 | 日本語

AI Gateway

Route to 250+ LLMs with 1 fast & friendly API

Portkey AI Gateway Demo showing LLM routing capabilities

Docs | Enterprise | Hosted Gateway | Changelog | API Reference

License Discord Twitter npm version Better Stack Badge

Deploy to AWS EC2 Ask DeepWiki

The AI Gateway is designed for fast, reliable & secure routing to 1600+ language, vision, audio, and image models. It is a lightweight, open-source, and enterprise-ready solution that allows you to integrate with any language model in under 2 minutes.

  • [x] Blazing fast (<1ms latency) with a tiny footprint (122kb)
  • [x] Battle tested, with over 10B tokens processed everyday
  • [x] Enterprise-ready with enhanced security, scale, and custom deployments

What can you do with the AI Gateway?

[!TIP] Starring this repo helps more developers discover the AI Gateway 🙏🏻

star-2

Quickstart (2 mins)

1. Setup your AI Gateway

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

The Gateway is running on http://localhost:8787/v1

The Gateway Console is running on http://localhost:8787/public/

Deployment guides:   Portkey Cloud (Recommended)   Docker   Node.js   Cloudflare   Replit   Others...

2. Make your first request

# pip install -qU portkey-ai

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"
)

Supported Libraries:   JS   Python   REST   OpenAI SDKs   Langchain   LlamaIndex   Autogen   CrewAI   More..

On the Gateway Console (http://localhost:8787/public/) you can see all of your local logs in one place.

3. Routing & Guardrails

Configs in the LLM gateway allow you to create routing rules, add reliability and setup guardrails.

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"}]
)

# This would always response with "Bat" as the guardrail denies all replies containing "Apple". The retry config would retry 5 times before giving up.

Request flow through Portkey's AI gateway with retries and guardrails

You can do a lot more stuff with configs in your AI gateway. Jump to examples →

Enterprise Version (Private deployments)

AWS   Azure   GCP   OpenShift   Kubernetes

The LLM Gateway's enterprise version offers advanced capabilities for org management, governance, security and more out of the box. View Feature Comparison →

The enterprise deployment architecture for supported platforms is available here - Enterprise Private Cloud Deployments

Book an enterprise AI gateway demo

Core Features

Reliable Routing

  • Fallbacks: Fallback to another provider or model on failed requests using the LLM gateway. You can specify the errors on which to trigger the fallback. Improves reliability of your application.
  • Automatic Retries: Automatically retry failed requests up to 5 times. An exponential backoff strategy spaces out retry attempts to prevent network overload.
  • Load Balancing: Distribute LLM requests across multiple API keys or AI providers with weights to ensure high availability and optimal performance.
  • Request Timeouts: Manage unruly LLMs & latencies by setting up granular request timeouts, allowing automatic termination of requests that exceed a specified duration.
  • Multi-modal LLM Gateway: Call vision, audio (text-to-speech & speech-to-text), and image generation models from multiple providers — all using the familiar OpenAI signature
  • Realtime APIs: Call realtime APIs launched by OpenAI through the integrate websockets server.

Security & Accuracy

Cost Management

  • Smart caching: Cache responses from LLMs to reduce costs and improve latency. Supports simple and semantic* caching.
  • Usage analytics: Monitor and analyze your AI and LLM usage, including request volume, latency, costs and error rates.
  • Provider optimization*: Automatically switch to the most cost-effective provider based on usage patterns and pricing models.

Collaboration & Workflows

* Available in hosted and enterprise versions

Cookbooks

☄️ Trending

🚨 Latest

View all cookbooks →

Supported Providers

Explore Gateway integrations with 45+ providers and 8+ agent frameworks.

Provider Support Stream
OpenAI
Azure OpenAI
Anyscale
Google Gemini
Anthropic
Cohere
<img s

Extension points exported contracts — how you extend this code

CacheBackend (Interface)
(no doc) [9 implementers]
src/shared/services/cache/types.ts
FullRequestBody (Interface)
* The full structure of the request body. * @interface
src/types/requestBody.ts
InputProps (Interface)
(no doc)
cookbook/integrations/vercel/components/ui/input.tsx
ParameterConfig (Interface)
(no doc)
src/providers/types.ts
Check (Interface)
(no doc)
src/middlewares/hooks/types.ts
RouterContext (Interface)
(no doc)
src/services/conditionalRouter.ts
CreateResponseOptions (Interface)
(no doc)
src/handlers/services/responseService.ts
TestVariable (Interface)
(no doc)
src/tests/resources/testVariables.ts

Core symbols most depended-on inside this repo

get
called by 119
src/shared/services/cache/types.ts
generateInvalidProviderResponseError
called by 97
src/providers/utils.ts
error
called by 87
src/shared/utils/logger.ts
log
called by 81
src/shared/utils/logger.ts
toString
called by 80
plugins/acuvity/helper.ts
generateErrorResponse
called by 63
src/providers/utils.ts
OpenAIErrorResponseTransform
called by 46
src/providers/openai/utils.ts
getValueOrFileContents
called by 42
src/utils/env.ts

Shape

Function 810
Interface 683
Method 273
Class 62
Enum 23

Languages

TypeScript100%

Modules by API surface

src/types/modelResponses.ts97 symbols
src/types/MessagesRequest.ts49 symbols
src/providers/oracle/types/ChatDetails.ts36 symbols
src/providers/open-ai-base/helpers.ts35 symbols
src/shared/services/cache/index.ts32 symbols
src/providers/google-vertex-ai/utils.ts30 symbols
src/providers/cohere/types.ts30 symbols
src/middlewares/hooks/index.ts30 symbols
src/providers/types.ts29 symbols
tests/integration/src/handlers/requestBuilder.ts27 symbols
src/handlers/services/requestContext.ts27 symbols
src/providers/oracle/types/GenericChatResponse.ts26 symbols

Dependencies from manifests, versioned

@ai-sdk/openai0.0.66 · 1×
@aws-crypto/sha256-js5.2.0 · 1×
@cfworker/json-schema4.0.3 · 1×
@cloudflare/workers-types4.20230518.0 · 1×
@hono/node-server1.3.3 · 1×
@hono/node-ws1.2.0 · 1×
@portkey-ai/mustache2.1.3 · 1×
@portkey-ai/vercel-provider1.0.1 · 1×
@radix-ui/react-label2.1.8 · 1×
@radix-ui/react-slot1.2.4 · 1×
@rollup/plugin-json6.1.0 · 1×
@rollup/plugin-terser0.4.4 · 1×

For agents

$ claude mcp add gateway \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact