docs/docs/genai/tracing/integrations/listing/ollama.mdx
import TilesGrid from "@site/src/components/TilesGrid"; import TileCard from "@site/src/components/TileCard"; import { Users, BookOpen, Scale } from "lucide-react"; import ImageBox from "@site/src/components/ImageBox"; import StepHeader from "@site/src/components/StepHeader"; import ServerSetup from "@site/src/content/setup_server_slim.mdx"; import Tabs from "@theme/Tabs"; import TabItem from "@theme/TabItem"; import TabsWrapper from "@site/src/components/TabsWrapper";
MLflow Tracing provides automatic tracing capability for Ollama models through the OpenAI SDK integration. Because Ollama exposes an OpenAI-compatible API, you can simply use mlflow.openai.autolog() to trace Ollama calls.
MLflow trace automatically captures the following information about Ollama calls:
temperature, max_tokens, if specified.pip install mlflow openai
npm install @mlflow/openai openai
Ensure your Ollama server is running and the model you want to use is pulled.
ollama run llama3.2:1b
```python
import mlflow
from openai import OpenAI
# Enable auto-tracing for OpenAI (works with Ollama)
mlflow.openai.autolog()
# Optional: Set a tracking URI and an experiment
mlflow.set_tracking_uri("http://localhost:5000")
mlflow.set_experiment("Ollama")
# Initialize the OpenAI client with Ollama API endpoint
client = OpenAI(
base_url="http://localhost:11434/v1",
api_key="dummy",
)
response = client.chat.completions.create(
model="llama3.2:1b",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Why is the sky blue?"},
],
temperature=0.1,
max_tokens=100,
)
```
```typescript
import { OpenAI } from "openai";
import { tracedOpenAI } from "@mlflow/openai";
// Wrap the OpenAI client and point to Ollama endpoint
const client = tracedOpenAI(
new OpenAI({
baseURL: "http://localhost:11434/v1",
apiKey: "dummy",
})
);
const response = await client.chat.completions.create({
model: "llama3.2:1b",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Why is the sky blue?" },
],
temperature: 0.1,
max_tokens: 100,
});
```
Browse to your MLflow UI (for example, http://localhost:5000) and open the Ollama experiment to see traces for the calls above.
→ View <u>Next Steps</u> for learning about more MLflow features like user feedback tracking, prompt management, and evaluation.
MLflow supports automatic tracing for the following Ollama APIs through the OpenAI integration:
| Chat Completion | Function Calling | Streaming | Async |
|---|---|---|---|
| ✅ | ✅ | ✅ (*1) | ✅ (*2) |
(*1) Streaming support requires MLflow 2.15.0 or later. (*2) Async support requires MLflow 2.21.0 or later.
To request support for additional APIs, please open a feature request on GitHub.
MLflow supports tracing for streaming and async Ollama APIs. Visit the OpenAI Tracing documentation for example code snippets for tracing streaming and async calls through OpenAI SDK.
To control the tracing behavior more precisely, MLflow provides Manual Tracing SDK to create spans for your custom code. Manual tracing can be used in conjunction with auto-tracing to create a custom trace while keeping the auto-tracing convenience. For more details, please refer to the Combine with Manual Tracing section in the OpenAI Tracing documentation.
MLflow automatically tracks token usage and cost for Ollama models through the OpenAI SDK integration. The token usage for each LLM call will be logged in each Trace/Span and the aggregated cost and time trend are displayed in the built-in dashboard. See the Token Usage and Cost Tracking documentation for details on accessing this information programmatically.
:::note Cost may not be available for many local Ollama models as pricing data is not available. :::
Auto tracing for Ollama (through OpenAI SDK) can be disabled globally by calling mlflow.openai.autolog(disable=True) or mlflow.autolog(disable=True).