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

# Ollama

> Trace Ollama-served local LLM calls with OpenInference and send spans to Arize AX for LLM observability.

[Ollama](https://ollama.com/) runs open-source models locally and exposes an [OpenAI-compatible Chat Completions API](https://github.com/ollama/ollama/blob/main/docs/api/openai-compatibility.mdx) at `http://localhost:11434/v1`. Because the client side speaks the OpenAI protocol, Arize AX captures every Ollama call via the [`openinference-instrumentation-openai`](https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-openai) package — the same instrumentor that covers OpenAI's hosted API.

<CardGroup>
  <Card horizontal icon="https://storage.googleapis.com/arize-phoenix-assets/assets/images/phoenix-docs-images/gc.ico" href="https://colab.research.google.com/github/Arize-ai/tutorials_python/blob/main/Arize_Tutorials/Tracing/Arize_Tutorial_Llama32_Instrumentation.ipynb" title="Llama 3.2 + Ollama Tracing Tutorial (Google Colab)" />
</CardGroup>

## Prerequisites

* Python 3.9+
* An Arize AX account ([sign up](https://arize.com/sign-up/))
* [Ollama](https://ollama.com/download) installed and running locally (`ollama serve`)
* A small instruction-tuned model pulled (this guide uses `llama3.2:1b`):

  ```bash theme={null}
  ollama pull llama3.2:1b
  ```

## Launch Arize AX

1. Sign in to your [Arize AX account](https://app.arize.com/).
2. From **Space Settings**, copy your **Space ID** and **API Key**. You will set them as `ARIZE_SPACE_ID` and `ARIZE_API_KEY` below.

## Install

```bash theme={null}
pip install arize-otel openinference-instrumentation-openai openai
```

## Configure credentials

```bash theme={null}
export ARIZE_SPACE_ID="<your-space-id>"
export ARIZE_API_KEY="<your-api-key>"
export ARIZE_PROJECT_NAME="ollama-tracing-example"
```

Ollama does not require an API key — the OpenAI client passes a literal placeholder. No external provider key is needed.

## Setup tracing

```python theme={null}
# instrumentation.py
import os

from arize.otel import register
from openinference.instrumentation.openai import OpenAIInstrumentor

tracer_provider = register(
    space_id=os.environ["ARIZE_SPACE_ID"],
    api_key=os.environ["ARIZE_API_KEY"],
    project_name=os.environ["ARIZE_PROJECT_NAME"],
)

OpenAIInstrumentor().instrument(tracer_provider=tracer_provider)
print("Arize AX tracing initialized for Ollama.")
```

## Run Ollama

```python theme={null}
# example.py

# Importing instrumentation first ensures tracing is set up
# before `openai` is imported.
from instrumentation import tracer_provider

from openai import OpenAI

# Point the OpenAI client at the local Ollama server. The api_key is required
# by the client but is not validated by Ollama.
client = OpenAI(
    base_url="http://localhost:11434/v1",
    api_key="ollama",
)

response = client.chat.completions.create(
    model="llama3.2:1b",
    messages=[
        {
            "role": "user",
            "content": "Why is the ocean salty? Answer in two sentences.",
        },
    ],
)

print(response.choices[0].message.content)
```

### Expected output

```text wrap theme={null}
Arize AX tracing initialized for Ollama.
The ocean is salty because rivers continuously dissolve mineral salts from rocks and soil and carry them to the sea, where they accumulate over millions of years. Water leaves the ocean through evaporation but the salts remain, steadily concentrating until reaching today's roughly 3.5% salinity.
```

## Verify in Arize AX

1. Open your Arize AX space and select project **`ollama-tracing-example`**.
2. You should see a new trace within \~30 seconds containing a `ChatCompletion` LLM span with the prompt, response, and token usage attached. The model name on the span will be the Ollama model you ran (e.g. `llama3.2:1b`).
3. If no traces appear, see [Troubleshooting](#troubleshooting).

## Troubleshooting

* **No traces in Arize AX.** Confirm `ARIZE_SPACE_ID` and `ARIZE_API_KEY` are set in the same shell that runs `example.py`. Enable OpenTelemetry debug logs with `export OTEL_LOG_LEVEL=debug` and re-run.
* **`Connection refused` or `ConnectError` to `localhost:11434`.** The Ollama daemon is not running. Start it with `ollama serve` (in another terminal, or as a background service).
* **`model "llama3.2:1b" not found, try pulling it first`.** Pull the model: `ollama pull llama3.2:1b`. Run `ollama list` to see what's pulled locally.
* **Different model.** Swap `llama3.2:1b` for any model in the [Ollama library](https://ollama.com/library) you've pulled — `llama3.3`, `mistral`, `qwen2.5`, etc. The `OpenAIInstrumentor` doesn't care which model serves the response.
* **Spans show but with the wrong model name.** Ollama reports the model alias you passed to the API; if you renamed the model locally (`ollama cp`), use that alias.

## Resources

<CardGroup>
  <Card icon="book-open" href="https://github.com/ollama/ollama/blob/main/docs/api/openai-compatibility.mdx" title="Ollama OpenAI Compatibility Documentation" horizontal />

  <Card icon="terminal" href="https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-openai" title="OpenInference OpenAI Instrumentor (used for Ollama)" horizontal />

  <Card icon="github" href="https://github.com/ollama/ollama" title="Ollama GitHub" horizontal />
</CardGroup>
