Skip to main content
LangWatch supports tracing local models served by Ollama through its OpenAI-compatible endpoint.

Installation

go get github.com/langwatch/langwatch/sdk-go github.com/openai/openai-go

Usage

The LangWatch API key is configured by default via the LANGWATCH_API_KEY environment variable.
Configure the OpenAI client to point to your local Ollama endpoint:
package main

import (
	"context"
	"log"
	"os"

	otelopenai "github.com/langwatch/langwatch/sdk-go/instrumentation/openai"
	"github.com/openai/openai-go"
	oaioption "github.com/openai/openai-go/option"
)

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

	client := openai.NewClient(
		oaioption.WithBaseURL(os.Getenv("OLLAMA_BASE_URL")),
		oaioption.WithMiddleware(otelopenai.Middleware("<project_name>",
			otelopenai.WithCaptureInput(),
			otelopenai.WithCaptureOutput(),
		)),
	)

	response, err := client.Chat.Completions.New(ctx, openai.ChatCompletionNewParams{
		Model: "openai/gpt-5",
		Messages: []openai.ChatCompletionMessageParamUnion{
			openai.SystemMessage("You are a helpful assistant."),
			openai.UserMessage("Hello, OpenAI!"),
		},
	})
	if err != nil {
		log.Fatalf("Chat completion failed: %v", err)
	}

	log.Printf("Chat completion: %s", response.Choices[0].Message.Content)
}
Set OLLAMA_BASE_URL to your Ollama endpoint (default: http://localhost:11434/v1). Ollama doesn’t require an API key, but you can set any non-empty string.