> ## Documentation Index
> Fetch the complete documentation index at: https://docs.egisai.co/llms.txt
> Use this file to discover all available pages before exploring further.

# SDK with self-hosted models

> Govern calls to your own self-hosted or open-source models from Python — in-process, through the Gateway, or automatically when your code calls a registered custom host directly.

The [SDK](https://docs.egisai.co/quickstart.md) governs self-hosted models the same way it governs
OpenAI or Anthropic: one `egisai.init()` call, no per-call wiring. There
are three ways your self-hosted traffic gets covered, depending on how
your code reaches the model.

## 1. Point an OpenAI-compatible client at your server

If you run a model behind an OpenAI-compatible server (vLLM, Ollama,
TGI, SGLang, …), just point the `openai` client's `base_url` at it.
`egisai.init()` patches the client, so every call is governed in-process
before it leaves your process — regardless of the base URL:

```python theme={null}
import egisai
from openai import OpenAI

egisai.init(api_key="egis_live_…", app="my-service")

client = OpenAI(base_url="http://localhost:8000/v1", api_key="not-needed")
client.chat.completions.create(
    model="llama-3-70b",
    messages=[{"role": "user", "content": "Draft a reply to this email…"}],
    stream=True,
)
```

Policies, PII masking, and the audit trail all apply. Streaming and tool
calling are unchanged — the SDK governs the request before the stream
starts and the response as it completes.

## 2. Route through the Gateway to a registered custom model

If you've registered the model as a [custom provider](https://docs.egisai.co/integrations/custom-models.md),
address it as `custom-<slug>/<model>` and let the Gateway handle the
provider key (from the encrypted vault) and the transport — including
reaching a **private** model through the [Egis Connector](https://docs.egisai.co/integrations/egis-connector.md):

```python theme={null}
import egisai

client = egisai.Client(api_key="egis_live_…")
client.chat.completions.create(
    model="custom-verda/llama-3-70b",
    messages=[{"role": "user", "content": "…"}],
)
```

Existing code that already uses the OpenAI client can route through the
Gateway without touching call sites:

```python theme={null}
egisai.init(api_key="egis_live_…", gateway=True)
```

See [`init()` → `gateway`](https://docs.egisai.co/api-reference/init#param-gateway).

> [!TIP]
>
> Use option 1 when the model is reachable from your own process and you
> want sub-millisecond in-process governance; use option 2 when you want
> the key in the vault, Smart Routing, or a private model reached through
> the connector.

## 3. Raw HTTP calls to a registered custom host — covered automatically

Code that calls a self-hosted model over plain `httpx` or `requests`
(no OpenAI client) is still seen. When you register a custom model, its
host is published to the SDK, and a raw call to that host on an
inference-shaped path is attributed and audited automatically:

```python theme={null}
import egisai, httpx

egisai.init(api_key="egis_live_…")

# A direct call to your registered custom host — audited, no wrapper.
httpx.post(
    "https://models.internal.acme.com/v1/chat/completions",
    json={"model": "llama-3-70b", "messages": [...]},
)
```

This path is **audit-only and fail-open**: it records the call so your
self-hosted traffic shows up alongside everything else, and it never
breaks the request if the SDK can't reach the control plane. Turn on
**Govern this host** when registering the model to also cover direct
browser and network traffic to it through the
[egress node](https://docs.egisai.co/coverage/egress-node.md) and
[browser extension](https://docs.egisai.co/coverage/browser-extension.md).

## What's next

> [card] [Custom models](https://docs.egisai.co/integrations/custom-models.md)
>
> Register a self-hosted model on the Gateway.

> [card] [Egis Connector](https://docs.egisai.co/integrations/egis-connector.md)
>
> Reach a model with no inbound access.

> [card] [HTTP clients](https://docs.egisai.co/integrations/http-clients.md)
>
> How the SDK covers raw httpx / requests calls.

> [card] [Self-hosting](https://docs.egisai.co/operations/self-hosting.md)
>
> Run the whole control plane in your network.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.