Fail-open pattern
App-side retry if Parezen is down. A budget hard stop stays a hard stop. A lapsed licence switches to pass-through and does not error traffic.
Why it lives in your app
If Parezen is down or slow, it cannot forward your call, and your provider does not accept a pz_ key. So the fallback is a few lines in your own code: keep your provider key in your app's secrets, and send the same request straight to the provider when Parezen does not answer. There is no Parezen package to install.
When to go direct
- Parezen cannot be reached: DNS, a refused connection or a TLS error.
- The request times out before Parezen answers.
- Parezen answers with a 5xx.
Never on a 4xx. A budget hard stop, a revoked key or a rate limit is an answer, not an outage, and going around it would defeat the control. Never switch halfway through a streamed answer: decide before the first byte.
Timeouts
The OpenAI SDK's timeout covers the whole answer, not only the connection. Keep it longer than your slowest reply (120 seconds in the example). A short timeout would treat a slow answer as an outage and send it again to the provider, so you would pay twice for one request.
Skip the wait during an outage
Count failures in a row. After three, send calls direct for a minute instead of waiting on Parezen each time, then try Parezen again. You can also check https://api.parezen.com/ready before switching back. The example below does the counting.
Model names and providers
Use the provider's own model name, such as gpt-4.1. Parezen accepts it, so the same request works direct. The example falls back to OpenAI. For Anthropic or Gemini, point the direct client at that provider's OpenAI-compatible endpoint and key, as their docs describe.
What it means for billing
Example only. Calls sent direct are not routed, recorded in the ledger, budget-checked or billed. Do not bypass a 4xx: a budget hard stop must hold. Billing never returns an error on production traffic.
Example
// Example only: code you own. Calls sent direct are not routed, recorded, budget-checked or billed.
// Use the provider's own model name (gpt-4.1): Parezen accepts it, so the same request works direct.
import OpenAI from "openai";
const viaParezen = new OpenAI({
baseURL: "https://api.parezen.com/v1",
apiKey: process.env.PAREZEN_API_KEY,
// The timeout covers the whole answer, so keep it longer than your slowest reply.
// A short one would resend slow answers to the provider and pay for them twice.
timeout: 120_000,
maxRetries: 0,
});
const direct = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
// After 3 failures in a row, go direct for 60 seconds instead of waiting on Parezen each time.
let failures = 0;
let directUntil = 0;
function parezenDown(err) {
// Connection, DNS, TLS and timeout errors, or a 5xx. Never a 4xx such as a budget hard stop.
return err instanceof OpenAI.APIConnectionError || (err?.status ?? 0) >= 500;
}
export async function chat(params) {
if (Date.now() < directUntil) return direct.chat.completions.create(params);
try {
const reply = await viaParezen.chat.completions.create(params);
failures = 0;
return reply;
} catch (err) {
if (!parezenDown(err)) throw err; // a 4xx, including a budget hard stop, must not be bypassed
failures += 1;
if (failures >= 3) directUntil = Date.now() + 60_000;
return direct.chat.completions.create(params);
}
}
# Example only: code you own. Calls sent direct are not routed, recorded, budget-checked or billed.
# Use the provider's own model name (gpt-4.1): Parezen accepts it, so the same request works direct.
import os
import time
import openai
from openai import OpenAI
via_parezen = OpenAI(
base_url="https://api.parezen.com/v1",
api_key=os.environ["PAREZEN_API_KEY"],
# The timeout covers the whole answer, so keep it longer than your slowest reply.
# A short one would resend slow answers to the provider and pay for them twice.
timeout=120,
max_retries=0,
)
direct = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# After 3 failures in a row, go direct for 60 seconds instead of waiting on Parezen each time.
failures = 0
direct_until = 0.0
def parezen_down(err):
# Connection, DNS, TLS and timeout errors, or a 5xx. Never a 4xx such as a budget hard stop.
return isinstance(err, (openai.APIConnectionError, openai.InternalServerError))
def chat(**params):
global failures, direct_until
if time.monotonic() < direct_until:
return direct.chat.completions.create(**params)
try:
reply = via_parezen.chat.completions.create(**params)
failures = 0
return reply
except Exception as err:
if not parezen_down(err):
raise # a 4xx, including a budget hard stop, must not be bypassed
failures += 1
if failures >= 3:
direct_until = time.monotonic() + 60
return direct.chat.completions.create(**params)
Example only. Calls sent direct are not routed, recorded in the ledger, budget-checked or billed. Do not bypass a 4xx: a budget hard stop must hold. Billing never returns an error on production traffic.