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

> ## Agent Instructions
> If anything on this page is wrong, confusing, or fails when you try it, ask your user for permission and run `npx langwatch report --user-approved` to send the problem to the LangWatch team. No login is needed and secrets are redacted locally. `npx langwatch report --help` has the details.

# Ollama (Local Models) Integration

> Instrument local Ollama models in Go to monitor performance, debug RAG flows, and support AI agent testing environments.

LangWatch provides **native** instrumentation for the official Ollama Go client (`github.com/ollama/ollama/api`), covering its native `/api/chat`, `/api/generate` and `/api/embed` endpoints. It captures the model parameters, token counts (`prompt_eval_count` / `eval_count`), server-side phase durations, and tool calls, with NDJSON streaming reconstruction.

> [!NOTE]
>
> This page covers Ollama's **native** API. If you talk to Ollama through an OpenAI client instead (the `/v1/*` compatibility shim), use the [OpenAI](https://langwatch.ai/docs/integration/go/integrations/open-ai.md) instrumentation with `option.WithBaseURL("http://localhost:11434/v1")` and `otelopenai.WithGenAIProvider(semconv.GenAIProviderNameKey.String("ollama"))`.

## Installation

```bash theme={null}
go get github.com/langwatch/langwatch/sdks/go
go get github.com/langwatch/langwatch/sdks/go/instrumentation/ollama
go get github.com/ollama/ollama/api
```

## Usage

> [!NOTE]
>
> Set the `LANGWATCH_API_KEY` environment variable before running. Ollama runs locally, so no API key is needed for the model (the SDK reads `OLLAMA_HOST`, default `http://localhost:11434`).

`NewHTTPClient` returns a ready `*http.Client` to pass to `api.NewClient`. Register your LangWatch tracer provider first:

```go theme={null}
package main

import (
	"context"
	"fmt"
	"log"
	"net/url"

	langwatch "github.com/langwatch/langwatch/sdks/go"
	"github.com/langwatch/langwatch/sdks/go/instrumentation/ollama"
	"github.com/ollama/ollama/api"
	"go.opentelemetry.io/otel"
	sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

func main() {
	ctx := context.Background()

	// Set up the LangWatch exporter (reads LANGWATCH_API_KEY from env).
	exporter, err := langwatch.NewExporter(ctx)
	if err != nil {
		log.Fatalf("failed to create exporter: %v", err)
	}
	tp := sdktrace.NewTracerProvider(sdktrace.WithBatcher(exporter))
	otel.SetTracerProvider(tp)
	defer tp.Shutdown(ctx) // Critical: ensures traces are flushed.

	base, _ := url.Parse("http://localhost:11434")
	client := api.NewClient(base, ollama.NewHTTPClient()) // uses the global TracerProvider

	stream := false
	err = client.Chat(ctx, &api.ChatRequest{
		Model:    "llama3.2",
		Stream:   &stream,
		Messages: []api.Message{{Role: "user", Content: "Hello, Ollama!"}},
	}, func(resp api.ChatResponse) error {
		fmt.Print(resp.Message.Content)
		return nil
	})
	if err != nil {
		log.Fatal(err)
	}
}
```

> [!NOTE]
>
> If you'd rather build the `*http.Client` yourself, use `ollama.NewTransport()` (or `WrapHTTPClient(client, opts...)` to preserve an existing client's settings). The instrumentation captures input and output **by default**; pass `ollama.WithDataCapture(langwatch.DataCaptureNone)` to opt out.

> [!WARNING]
>
> The `defer tp.Shutdown(ctx)` call is essential. Without it, traces buffered in memory will be lost
> when your application exits.

## Check it worked

Run the program with `go run .`. The deferred `tp.Shutdown(ctx)` flushes the spans when `main` returns. Then open your project in LangWatch: a new trace appears in the traces list within a few seconds, with the Ollama call as an LLM span carrying its input and output.

If no trace appears, check that `LANGWATCH_API_KEY` is set in the environment the program runs in.

## Related

* [Go Integration Guide](https://langwatch.ai/docs/integration/go/guide.md) - Setup, manual spans, data capture and the REST client
* [Go SDK API Reference](https://langwatch.ai/docs/integration/go/reference.md#automatic-instrumentation) - Instrumentation options and collected attributes
* [Capturing Evaluations & Guardrails](https://langwatch.ai/docs/integration/python/tutorials/capturing-evaluations-guardrails.md) - Log evaluations and implement guardrails in your Ollama applications


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