# Ollama

[Ollama](https://docs.ollama.com/api/openai-compatibility) runs language models locally or in private infrastructure and exposes OpenAI- and Anthropic-compatible APIs. AISIX adds caller keys, stable model aliases, traffic controls, and usage reporting while Ollama continues to serve inference in your environment.

## Prerequisites[​](#prerequisites "Direct link to Prerequisites")

Before starting, prepare the following:

* One AISIX setup:

  <!-- -->

  * For AISIX Enterprise or AISIX Cloud, an environment with an attached gateway and a write-scoped admin token. For AISIX Enterprise, follow the [AISIX Enterprise getting-started guide](https://docs.api7.ai/ai-gateway/getting-started/aisix-cloud-quickstart.md). To request AISIX Cloud access, [contact API7](https://api7.ai/contact).
  * For the open-source AISIX gateway, prepare either a local AISIX installation or the Docker setup from the [AISIX OSS getting-started guide](https://docs.api7.ai/ai-gateway/getting-started/gateway-quickstart.md). Configure the gateway to load a declarative resources file.

* Ollama 0.13.3 or later installed on a host reachable from the AISIX gateway. This version introduced the native Responses endpoint used in this guide.

* `curl` and `jq`.

## Prepare Ollama[​](#prepare-ollama "Direct link to Prepare Ollama")

Pull the model used in this guide:

```
ollama pull gpt-oss:20b
```

Ollama binds to `127.0.0.1:11434` by default. If AISIX runs in another container or host, configure a reachable bind address by following the [Ollama server configuration](https://docs.ollama.com/faq#how-do-i-configure-ollama-server). For example, a foreground server can listen on all interfaces:

```
OLLAMA_HOST="0.0.0.0:11434" ollama serve
```

caution

The local Ollama API does not require authentication. Binding it to `0.0.0.0` makes it reachable from other network peers. Restrict the listening network with firewall, container-network, or Kubernetes policy controls, and do not expose the port directly to the public Internet.

Export an API base reachable **from the AISIX gateway**:

```
# Docker Desktop gateway to Ollama on the host

export OLLAMA_API_BASE="http://host.docker.internal:11434/v1"
```

Choose the address for your topology:

| AISIX gateway and Ollama topology           | Example API base                                       |
| ------------------------------------------- | ------------------------------------------------------ |
| Both processes on one host                  | `http://127.0.0.1:11434/v1`                            |
| AISIX in Docker Desktop, Ollama on the host | `http://host.docker.internal:11434/v1`                 |
| Both containers on one Docker network       | `http://ollama:11434/v1`                               |
| Kubernetes                                  | `http://ollama.<namespace>.svc.cluster.local:11434/v1` |

On Linux, `host.docker.internal` may require an explicit `host-gateway` mapping. A successful request from your laptop does not prove that the AISIX gateway container can reach the same address.

## Configure with AISIX Enterprise or AISIX Cloud[​](#configure-with-aisix-enterprise-or-aisix-cloud "Direct link to Configure with AISIX Enterprise or AISIX Cloud")

Export the control-plane connection details:

```
# AISIX_CP is the Admin API base URL; include /api and omit a trailing slash

# The local AISIX Enterprise quickstart uses http://localhost:8080/api

export AISIX_CP="YOUR_AISIX_CONTROL_PLANE_API_URL"

export AISIX_TOKEN="YOUR_ADMIN_TOKEN"

export ENV_ID="YOUR_ENVIRONMENT_ID"
```

Ollama is a private endpoint rather than an AISIX catalog provider. Configure it with the `byo` provider value and select the `openai` adapter explicitly.

### Create a Provider Key[​](#create-a-provider-key "Direct link to Create a Provider Key")

```
PROVIDER_KEY_ID=$(

  curl -sS -X POST "$AISIX_CP/provider_keys" \

    -H "Authorization: Bearer $AISIX_TOKEN" \

    -H "Content-Type: application/json" \

    -d '{

      "display_name": "ollama-local",

      "provider": "byo",

      "adapter": "openai",

      "api_key": "ollama",

      "api_base": "'"${OLLAMA_API_BASE}"'",

      "apis": {

        "responses": {}

      },

      "allowed_environments": ["'"${ENV_ID}"'"]

    }' | jq -er '.provider_key.id'

)



echo "$PROVIDER_KEY_ID"
```

AISIX requires a non-empty `api_key` in the provider-key schema. Ollama's OpenAI client examples use `ollama` because the client requires a value, but the local Ollama server ignores it. AISIX sends the placeholder as a bearer token; it is not a security control.

A BYO key requires `provider: "byo"`, a non-empty `api_key`, and `api_base`. This guide sets `adapter: "openai"` explicitly; when omitted, AISIX defaults a BYO key to the OpenAI-compatible adapter. The `apis.responses` declaration sends Responses requests to Ollama's native endpoint instead of translating them through Chat Completions.

### Create a Model[​](#create-a-model "Direct link to Create a Model")

Use the exact local Ollama model tag:

```
MODEL_ID=$(

  curl -sS -X POST "$AISIX_CP/environments/$ENV_ID/models" \

    -H "Authorization: Bearer $AISIX_TOKEN" \

    -H "Content-Type: application/json" \

    -d '{

      "display_name": "ollama-gpt-oss-prod",

      "model_name": "gpt-oss:20b",

      "provider_key_id": "'"${PROVIDER_KEY_ID}"'"

    }' | jq -er '.model.id'

)



echo "$MODEL_ID"
```

Run `ollama ls` to see installed model tags. The tag, including a suffix such as `:20b`, is sent upstream unchanged.

### Create a Caller API Key[​](#create-a-caller-api-key "Direct link to Create a Caller API Key")

```
AISIX_API_KEY=$(

  curl -sS -X POST "$AISIX_CP/environments/$ENV_ID/api_keys" \

    -H "Authorization: Bearer $AISIX_TOKEN" \

    -H "Content-Type: application/json" \

    -d '{

      "display_name": "ollama-caller",

      "allowed_models": ["'"${MODEL_ID}"'"]

    }' | jq -er '.plaintext'

)



echo "$AISIX_API_KEY"
```

## Configure with AISIX OSS[​](#configure-with-aisix-oss "Direct link to Configure with AISIX OSS")

Export the upstream credential and choose the caller API key that applications will send to the gateway:

```
export OLLAMA_API_BASE="http://host.docker.internal:11434/v1"

export CALLER_API_KEY="YOUR_CALLER_API_KEY"

export CALLER_API_KEY_SHA256="$(printf '%s' "$CALLER_API_KEY" | sha256sum | cut -d ' ' -f 1)"
```

For a new gateway, use this complete resources file. For an existing gateway, merge the entries into its [current file](https://docs.api7.ai/ai-gateway/reference/resources-file.md#apply-resource-examples-safely), preserving its other resources:

resources.yaml

```
_format_version: "2"



provider_keys:

  - id: 150753ba-dc52-4925-95b9-cc6d4e0c0172

    display_name: "ollama-local"

    provider: "ollama"

    adapter: "openai"

    api_key: "ollama"

    api_base: "${OLLAMA_API_BASE}"

    apis:

      responses: {}



models:

  - id: 5cbea1bd-4dd7-44e6-8ba5-17ae520caa38

    display_name: "ollama-gpt-oss-prod"

    provider: "ollama"

    model_name: "gpt-oss:20b"

    provider_key_id: 150753ba-dc52-4925-95b9-cc6d4e0c0172



api_keys:

  - id: bfcfbfc9-58fe-4d4c-85c8-ed1a3cf8dbb3

    display_name: "ollama-caller"

    key_hash: ${CALLER_API_KEY_SHA256}

    allowed_models:

      - "ollama-gpt-oss-prod"
```

If AISIX is installed locally, validate the file before loading it:

```
aisix validate --resources resources.yaml
```

After validation, start the gateway with the referenced environment variables in its process environment. Reload an existing gateway only if those variables are already available to the process; otherwise, restart it with the updated environment.

If you use Docker, adapt the validation and startup commands in the [AISIX OSS getting-started guide](https://docs.api7.ai/ai-gateway/getting-started/gateway-quickstart.md). Mount this `resources.yaml` file and pass every environment variable it references with `-e` in both commands. After the resources load, prepare the shared verification request below:

```
export AISIX_API_KEY="$CALLER_API_KEY"
```

## Verify the Provider Connection[​](#verify-the-provider-connection "Direct link to Verify the Provider Connection")

Export the AISIX gateway origin:

```
# AISIX_PROXY has no trailing slash or endpoint path

# The local quickstarts use http://127.0.0.1:3000

export AISIX_PROXY="YOUR_AISIX_GATEWAY_URL"
```

Send a Responses request through the AISIX proxy:

```
curl -sS -X POST "$AISIX_PROXY/v1/responses" \

  -H "Authorization: Bearer $AISIX_API_KEY" \

  -H "Content-Type: application/json" \

  -d '{

    "model": "ollama-gpt-oss-prod",

    "input": "Say hello from Ollama."

  }'
```

Ollama should log `POST /v1/responses`. AISIX replaces the alias with the installed Ollama model tag upstream and restores the alias in the response.

Ollama added native `/v1/responses` in version 0.13.3. It supports streaming, function tools, and reasoning summaries, but not state through `previous_response_id` or `conversation`. The declaration in this guide keeps those supported native semantics on the normalized AISIX route while retaining alias rewriting and model access controls.

## Use Native Messages Through Passthrough[​](#use-native-messages-through-passthrough "Direct link to Use Native Messages Through Passthrough")

Ollama also publishes a native Anthropic-shaped Messages route, but not `/v1/messages/count_tokens`. Use a [passthrough route](https://docs.api7.ai/ai-gateway/endpoints/provider-passthrough.md) when the native Messages format matters. Do not declare `apis.messages` or create a separate alias with the `anthropic` adapter for this workflow; both represent the complete Messages surface, including Count Tokens. See [Declare the API Surfaces](https://docs.api7.ai/ai-gateway/models/provider-keys.md#declare-the-api-surfaces).

Passthrough does not rewrite the AISIX alias, so send the installed Ollama model tag. Ollama's other Messages limitations still apply: forced `tool_choice`, prompt caching, citations, and Messages batches are not supported, and extended-thinking budgets are accepted but not enforced.

## Endpoint Coverage[​](#endpoint-coverage "Direct link to Endpoint Coverage")

Ollama documents OpenAI-compatible chat completions, completions, Responses, models, and embeddings routes. It also publishes an [Anthropic-compatible Messages route](https://docs.ollama.com/api/anthropic-compatibility). The provider key in this guide declares native Responses only; other routes continue to follow the `openai` adapter or the route-specific behavior below.

| Route                                                             | Behavior with the Ollama alias in this guide                                                                                                                                                                                                                                                                                         |
| ----------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `/v1/chat/completions`                                            | Supported, including streaming, tools, structured output, vision, and reasoning controls when the installed model supports them.                                                                                                                                                                                                     |
| `/v1/completions`                                                 | Supported through the OpenAI adapter. Ollama accepts only a string in `prompt`.                                                                                                                                                                                                                                                      |
| `/v1/embeddings`                                                  | Supported when the alias names an installed embedding model. Ollama and AISIX accept a string or array of strings.                                                                                                                                                                                                                   |
| `/v1/responses`                                                   | Sent to Ollama's native Responses API because this guide declares `apis.responses`. Without the declaration, AISIX uses the [Responses bridge](https://docs.api7.ai/ai-gateway/endpoints/responses-api.md).                                                                                                                          |
| `/v1/messages`                                                    | Translated through chat because this provider key uses `adapter: openai`. Use passthrough for Ollama's native Messages format, as described above.                                                                                                                                                                                   |
| `/v1/messages/count_tokens`                                       | Rejected for the `openai` adapter. Ollama does not currently implement this endpoint even when a separate Anthropic adapter is used.                                                                                                                                                                                                 |
| `/v1/models`                                                      | Returns caller-accessible AISIX model aliases, not the models installed in Ollama. Use `ollama ls` or a passthrough route to query the Ollama inventory.                                                                                                                                                                             |
| `/v1/images/generations`, `/v1/rerank`                            | Rejected with `400` because the normalized routes do not accept this guide's provider label (`byo` in AISIX Enterprise and AISIX Cloud or `ollama` in the resources file).                                                                                                                                                           |
| `/v1/videos`                                                      | Rejected with `501 not_implemented` because neither provider label is in the video provider allowlist.                                                                                                                                                                                                                               |
| `/v1/audio/*`, `/v1/files`, `/v1/batches`, `/v1/fine_tuning/jobs` | AISIX can forward these OpenAI-shaped routes, but Ollama does not publish the corresponding APIs. The upstream rejects the request.                                                                                                                                                                                                  |
| `/passthrough/byo/*`                                              | Available through a configured [passthrough route](https://docs.api7.ai/ai-gateway/endpoints/provider-passthrough.md) for Ollama-native routes. The conventional prefix is `/passthrough/byo` in AISIX Enterprise and AISIX Cloud and `/passthrough/ollama` with an open-source resources file, matching this page's provider label. |

Ollama's OpenAI-compatible chat route does not currently support `tool_choice`, `logit_bias`, `user`, or `n`. AISIX can forward these fields, but forwarding does not add upstream support. For vision models, send a base64 image in an `image_url` content part; Ollama does not support a remote image URL on this route.

The `/passthrough` paths above assume a passthrough route claiming the chosen prefix with the Ollama root as its `target_url`; grant the route name on the caller key's `allowed_routes`. Passthrough does not rewrite an AISIX alias in the request body, and a route relays to its fixed target with its bound provider key regardless of the requested model. It relays upstream responses incrementally, including SSE. For the request shapes that record token usage, see [Envelope Detection and Usage](https://docs.api7.ai/ai-gateway/endpoints/provider-passthrough.md#envelope-detection-and-token-usage). Prefer the normalized routes when you need alias rewriting, token accounting, or AISIX cost estimates. Because Ollama is a BYO endpoint without catalog pricing, configure [Model Pricing](https://docs.api7.ai/ai-gateway/cloud/model-pricing.md) in AISIX Enterprise and AISIX Cloud or `cost` metadata in an open-source model resource when you need cost estimates.

## Troubleshooting[​](#troubleshooting "Direct link to Troubleshooting")

| Symptom                             | Check                                                                                  |
| ----------------------------------- | -------------------------------------------------------------------------------------- |
| Connection refused or timeout       | Test `OLLAMA_API_BASE` from the AISIX gateway container, not only from the host.       |
| Ollama listens only on `127.0.0.1`  | Set `OLLAMA_HOST` through the supported service configuration and restart Ollama.      |
| Model not found                     | Run `ollama pull gpt-oss:20b` and verify the tag with `ollama ls`.                     |
| Provider key creation returns `400` | Include `provider: "byo"`, `adapter: "openai"`, a non-empty `api_key`, and `api_base`. |

## Next Steps[​](#next-steps "Direct link to Next Steps")

You have now connected AISIX to Ollama and verified the model alias. Continue with these guides:

* [Bring Your Own Endpoint](https://docs.api7.ai/ai-gateway/providers/bring-your-own-endpoint.md): review the reusable setup for private OpenAI-compatible servers.
* [Model Aliases](https://docs.api7.ai/ai-gateway/models/model-aliases.md): configure routing, retry behavior, or cost metadata for the alias.
* [Routing and Failover](https://docs.api7.ai/ai-gateway/routing/routing-and-failover.md): fail over between Ollama and another provider serving the same model.
* [Provider Compatibility](https://docs.api7.ai/ai-gateway/providers/compatibility.md): review supported proxy endpoints and provider-specific boundaries.
