Python Integration
Integrate Ingate with Python applications using the OpenAI or Anthropic SDK. Authenticate the gateway separately from your upstream provider.
Cloud or self-hosted
https://api.ingateai.com (or your own gateway URL). Use a key issued by that instance.These server-side examples use provider API-key injection: configure the provider credential in Ingate first. Set INGATE_KEY privately in your environment. The SDK's api_key="unused" satisfies its constructor; Ingate replaces upstream auth with the configured provider credential. Never use your gateway key as upstream auth.
For an OpenAI provider configured with auth_passthrough: true, replaceapi_key="unused" with api_key=os.environ["OPENAI_API_KEY"]while retaining X-Ingate-Key. Anthropic API-key passthrough similarly usesANTHROPIC_API_KEY; OAuth Bearer credentials require the SDK'sauth_token option instead of api_key. This does not establish compatibility with a particular subscription client.
OpenAI SDK
Installation
pip install openaiQuickstart: Auto-Detected Provider
Set base_url ending in /v1 and send your Ingate key in X-Ingate-Key. Ingate auto-detects the provider from the request path (/v1/chat/completions → OpenAI), so no provider-selection header is needed.
import os
import openai
client = openai.OpenAI(
base_url="https://api.ingateai.com/v1",
api_key="unused",
default_headers={"X-Ingate-Key": os.environ["INGATE_KEY"]},
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain AI gateways in one paragraph."},
],
max_tokens=200,
)
print(response.choices[0].message.content)Provider auto-detection
/v1/chat/completions→ OpenAI/v1/messages→ Anthropic/v1/embeddings→ OpenAI/api/generate→ Ollama
X-Ingate-Provider when routing to a custom or ambiguous endpoint.Explicit Provider Header
If you want to be explicit, or are using a custom provider alias configured in your Ingate dashboard, pass the X-Ingate-Provider header:
import os
import openai
client = openai.OpenAI(
base_url="https://api.ingateai.com/v1",
api_key="unused",
default_headers={"X-Ingate-Key": os.environ["INGATE_KEY"], "X-Ingate-Provider": "openai"},
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content)Streaming
stream = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Tell me a story"}],
stream=True,
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)Anthropic SDK
Installation
pip install anthropicQuickstart
Use the official Anthropic SDK exactly as normal. Change base_url and pass your Ingate key in default_headers. The provider is auto-detected from /v1/messages.
import os
import anthropic
client = anthropic.Anthropic(
base_url="https://api.ingateai.com",
api_key="unused",
default_headers={"X-Ingate-Key": os.environ["INGATE_KEY"]},
)
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=200,
messages=[
{"role": "user", "content": "Explain AI gateways in one paragraph."},
],
)
print(message.content[0].text)Streaming (Anthropic SDK)
with client.messages.stream(
model="claude-sonnet-4-20250514",
max_tokens=300,
messages=[{"role": "user", "content": "Tell me a story"}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)Cross-Provider Translation
Send OpenAI-format requests but target Anthropic (or vice versa). Add the X-Ingate-Translate header and Ingate handles format conversion automatically.
import os
import openai
# Use the OpenAI SDK to talk to Claude
client = openai.OpenAI(
base_url="https://api.ingateai.com/v1",
api_key="unused",
default_headers={
"X-Ingate-Key": os.environ["INGATE_KEY"],
"X-Ingate-Provider": "anthropic",
"X-Ingate-Translate": "true",
},
)
# Same OpenAI interface. Ingate translates to Anthropic format
response = client.chat.completions.create(
model="claude-sonnet-4-20250514",
messages=[{"role": "user", "content": "Hello, Claude!"}],
)
print(response.choices[0].message.content)When to use translation
User & Session Tracking
Attach user and session identifiers to requests for per-user analytics, rate limiting, and audit trails in your Ingate dashboard.
import os
import openai
client = openai.OpenAI(
base_url="https://api.ingateai.com/v1",
api_key="unused",
default_headers={
"X-Ingate-Key": os.environ["INGATE_KEY"],
"X-Ingate-User-Id": "user_abc123",
"X-Ingate-Session-Id": "sess_xyz789",
},
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "What's the weather like?"}],
)
print(response.choices[0].message.content)You can also set these headers per-request using extra_headers:
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello"}],
extra_headers={
"X-Ingate-User-Id": current_user.id,
"X-Ingate-Session-Id": session.id,
},
)Dashboard visibility
X-Ingate-User-Id appear grouped by user in your Ingate dashboard. Useful for cost attribution, usage analytics, and per-user rate limit configuration.Multi-Provider Helper
A small helper to create clients for different providers. Auto-detection handles most cases, but this is useful when targeting custom provider aliases or enabling translation.
import os
import openai
INGATE_URL = "https://api.ingateai.com/v1"
INGATE_KEY = os.environ["INGATE_KEY"]
def get_client(
provider: str | None = None,
translate: bool = False,
user_id: str | None = None,
session_id: str | None = None,
) -> openai.OpenAI:
headers: dict[str, str] = {"X-Ingate-Key": INGATE_KEY}
if provider:
headers["X-Ingate-Provider"] = provider
if translate:
headers["X-Ingate-Translate"] = "true"
if user_id:
headers["X-Ingate-User-Id"] = user_id
if session_id:
headers["X-Ingate-Session-Id"] = session_id
return openai.OpenAI(
base_url=INGATE_URL,
api_key="unused",
default_headers=headers,
)
# Auto-detected (no provider header needed)
default_client = get_client()
# Explicit provider
openai_client = get_client(provider="openai")
# Anthropic via OpenAI SDK with translation
claude_client = get_client(provider="anthropic", translate=True)
# With user tracking
tracked_client = get_client(user_id="user_abc", session_id="sess_xyz")Async Support
Both the OpenAI and Anthropic SDKs support async. Works identically through Ingate.
import os
import asyncio
import openai
client = openai.AsyncOpenAI(
base_url="https://api.ingateai.com/v1",
api_key="unused",
default_headers={"X-Ingate-Key": os.environ["INGATE_KEY"]},
)
async def main():
response = await client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello from async!"}],
)
print(response.choices[0].message.content)
asyncio.run(main())Error Handling
import os
import openai
client = openai.OpenAI(
base_url="https://api.ingateai.com/v1",
api_key="unused",
default_headers={"X-Ingate-Key": os.environ["INGATE_KEY"]},
)
try:
raw = client.chat.completions.with_raw_response.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello"}],
)
response = raw.parse()
print(response.choices[0].message.content)
# Access Ingate response headers
request_id = raw.headers.get("X-Ingate-Request-Id")
print(f"Request ID: {request_id}")
except openai.RateLimitError:
print("Rate limit exceeded, check your Ingate plan limits")
except openai.AuthenticationError:
print("Invalid Ingate API key")
except openai.APIConnectionError:
print("Cannot reach Ingate, check your network")
except openai.APIStatusError as e:
print(f"API error {e.status_code}: {e.message}")Response correlation
X-Ingate-Request-Id header to correlate responses with logs in your Ingate dashboard. Include it in your error reports for fast debugging.