Python Integration

Integrate Ingate with Python applications using the OpenAI or Anthropic SDK. One line change. Ingate auto-detects your provider.

Cloud-only

Ingate is a fully managed cloud gateway. No self-hosting, no infrastructure to manage. Point your SDK at https://api.ingateai.com and you're live.

OpenAI SDK

Installation

bash
pip install openai

Quickstart: Auto-Detected Provider

The simplest integration: change base_url and set your Ingate key. Ingate auto-detects the provider from the request path (/v1/chat/completions → OpenAI), so no extra headers are needed.

pythonapp.py
import openai

client = openai.OpenAI(
    base_url="https://api.ingateai.com",
    api_key="sk-ingate-your-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

Ingate recognises well-known paths automatically:
  • /v1/chat/completions → OpenAI
  • /v1/messages → Anthropic
  • /v1/embeddings → OpenAI
  • /api/generate → Ollama
You only need 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:

pythonexplicit_provider.py
client = openai.OpenAI(
    base_url="https://api.ingateai.com",
    api_key="sk-ingate-your-key",
    default_headers={"X-Ingate-Provider": "openai"},
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content)

Streaming

pythonstreaming.py
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

bash
pip install anthropic

Quickstart

Use the official Anthropic SDK exactly as normal. Change base_url and pass your Ingate key. The provider is auto-detected from /v1/messages.

pythonanthropic_sdk.py
import anthropic

client = anthropic.Anthropic(
    base_url="https://api.ingateai.com",
    api_key="sk-ingate-your-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)

pythonanthropic_streaming.py
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.

pythontranslation.py
# Use the OpenAI SDK to talk to Claude
client = openai.OpenAI(
    base_url="https://api.ingateai.com",
    api_key="sk-ingate-your-key",
    default_headers={
        "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

Translation is useful when you want a single SDK interface across providers. If you're already using the native Anthropic SDK, you don't need translation. just point base_url at Ingate.

User & Session Tracking

Attach user and session identifiers to requests for per-user analytics, rate limiting, and audit trails in your Ingate dashboard.

pythontracking.py
client = openai.OpenAI(
    base_url="https://api.ingateai.com",
    api_key="sk-ingate-your-key",
    default_headers={
        "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:

pythonper_request_tracking.py
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

Requests tagged with 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.

pythonmulti_provider.py
import openai

INGATE_URL = "https://api.ingateai.com"
INGATE_KEY = "sk-ingate-your-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] = {}
    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=INGATE_KEY,
        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.

pythonasync_example.py
import asyncio
import openai

client = openai.AsyncOpenAI(
    base_url="https://api.ingateai.com",
    api_key="sk-ingate-your-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

pythonerror_handling.py
import openai

client = openai.OpenAI(
    base_url="https://api.ingateai.com",
    api_key="sk-ingate-your-key",
)

try:
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": "Hello"}],
    )
    print(response.choices[0].message.content)

    # Access Ingate response headers
    request_id = response.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

Use the X-Ingate-Request-Id header to correlate responses with logs in your Ingate dashboard. Include it in your error reports for fast debugging.