Skip to content

Recipe — Use OpenAI through the Gateway

This recipe walks through standing up an AI Wrapper project so existing OpenAI-using apps can route through PromptGate without code changes.

End state: your apps call <your-gateway>/api/<uuid>/v1/chat/completions with the OpenAI SDK unchanged, and you get token-gated access, observability, rate limits, budgets, and the option to swap to a different provider later.

  • PromptGate installed and running (Installation)
  • An OpenAI API key (sk-…)
  • Logged into the admin UI

Top-right user menu → Credentials+ New credential:

  • Name: OpenAI Production
  • Provider: OpenAI
  • Secret: paste your sk-…

Save. The key is encrypted with APP_KEY at rest.

Sidebar → Projects+ New project:

  • Name: OpenAI Proxy
  • Type: AI Wrapper
  • Environment: prod

Click your new project to enter it.

Project sidebar → Providers:

  • Tick Enabled on the OpenAI row.
  • Pick CredentialOpenAI Production.

Save.

(You can do this for as many providers as you want — each one bound to its credential. The wrapper will route requests by provider:model accordingly.)

Step 4 — (Optional) Add a friendly alias

Section titled “Step 4 — (Optional) Add a friendly alias”

Project sidebar → Aliases+ New alias:

  • Alias: fast
  • Provider: openai
  • Model: gpt-4o-mini

Now clients can use model: "fast" instead of model: "openai:gpt-4o-mini". Swap the alias’s underlying model later without changing client code.

Project sidebar → API Tokens+ New token:

  • Name: Production app
  • Environment: live
  • Scopes: chat

Save. Copy the plaintext (pg_live_…) — it’s only shown once.

Step 6 — Point the OpenAI SDK at PromptGate

Section titled “Step 6 — Point the OpenAI SDK at PromptGate”

The wrapper’s URL is your gateway URL + /api/<UUID>/v1:

PG_BASE=https://gateway.your-domain.com/api/8e3f-…/v1
import os
from openai import OpenAI
client = OpenAI(
base_url=os.environ['PG_BASE'], # PromptGate's wrapper URL
api_key=os.environ['PG_TOKEN'], # The pg_live_... token
)
resp = client.chat.completions.create(
model="promptgate:fast", # Or "openai:gpt-4o-mini" / "openai:gpt-4o"
messages=[{"role": "user", "content": "Hello!"}],
)
print(resp.choices[0].message.content)
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: process.env.PG_BASE,
apiKey: process.env.PG_TOKEN,
});
const resp = await client.chat.completions.create({
model: 'promptgate:fast',
messages: [{ role: 'user', content: 'Hello!' }],
});
console.log(resp.choices[0].message.content);
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
base_url=os.environ['PG_BASE'],
api_key=os.environ['PG_TOKEN'],
model="promptgate:fast",
)
print(llm.invoke("Hello!").content)
import { createOpenAI } from '@ai-sdk/openai';
import { generateText } from 'ai';
const promptgate = createOpenAI({
baseURL: process.env.PG_BASE,
apiKey: process.env.PG_TOKEN,
});
const { text } = await generateText({
model: promptgate('promptgate:fast'),
prompt: 'Hello!',
});

Trigger a request, then watch:

  • Live Logs — request appears within 2s with status, latency, tokens.
  • Metrics — KPIs and charts update.
  • Audit Logauth.token.used events.
  • ✅ Provider key out of every app — replace it once in PromptGate.
  • ✅ Token-gated access — issue per-app tokens, revoke independently.
  • ✅ Observability — every request logged, rolled up, exportable.
  • ✅ Rate limits + budgets available (configure on the alias level via AI Endpoints for fine grain, or on the wrapper-wide via roadmap).
  • ✅ Provider portability — swap fast from openai:gpt-4o-mini to groq:llama-3.1-8b-instant in 5 seconds. Clients never know.
  • Add a guardrail (PII filter mask mode) so customer data never leaves your gateway: Guardrails.
  • Add rate limits per minute / per hour: Rate Limits.
  • Add monthly budget: Budgets.
  • Add Anthropic + Mistral as additional aliases for cheap fallback.

Next: Multi-provider AI Wrapper.


© Akyros Labs LLC. All rights reserved.