> ## 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.

# Azure OpenAI Integration

> Instrument Azure OpenAI API calls in Go using LangWatch to monitor model usage, latency, and AI agent evaluation metrics.

LangWatch provides native instrumentation for **Azure OpenAI** accessed through the official `openai-go/v3` client's `azure` subpackage. The `azureopenai` module is a thin wrapper over the [OpenAI](https://langwatch.ai/docs/integration/go/integrations/open-ai.md) instrumentation: all of OpenAI's chat / responses / embeddings capture applies unchanged, and the provider is recorded as `azure.openai`.

## Installation

```bash theme={null}
go get github.com/langwatch/langwatch/sdks/go
go get github.com/langwatch/langwatch/sdks/go/instrumentation/azureopenai
go get github.com/openai/openai-go/v3
```

## Usage

> [!NOTE]
>
> Set `LANGWATCH_API_KEY`, `AZURE_OPENAI_API_KEY`, and `AZURE_OPENAI_ENDPOINT` environment variables
> before running.

Build the Azure client with the `azure` connection options and wire in `azureopenai.Middleware` via `option.WithMiddleware`:

```go theme={null}
package main

import (
	"context"
	"log"
	"os"

	"github.com/openai/openai-go/v3"
	"github.com/openai/openai-go/v3/azure"
	"github.com/openai/openai-go/v3/option"
	"go.opentelemetry.io/otel"
	sdktrace "go.opentelemetry.io/otel/sdk/trace"

	langwatch "github.com/langwatch/langwatch/sdks/go"
	azureopenai "github.com/langwatch/langwatch/sdks/go/instrumentation/azureopenai"
)

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.

	// Create an instrumented Azure OpenAI client. The provider is recorded as
	// "azure.openai"; by default the middleware captures both input and output.
	const apiVersion = "2024-06-01"
	client := openai.NewClient(
		azure.WithEndpoint(os.Getenv("AZURE_OPENAI_ENDPOINT"), apiVersion),
		azure.WithAPIKey(os.Getenv("AZURE_OPENAI_API_KEY")),
		// Or authenticate with an Azure Identity TokenCredential:
		// azure.WithTokenCredential(tokenCredential),
		option.WithMiddleware(azureopenai.Middleware("my-app")),
	)

	// The Model is your Azure *deployment* name; the azure subpackage rewrites
	// the request path to /openai/deployments/{deployment}/chat/completions.
	response, err := client.Chat.Completions.New(ctx, openai.ChatCompletionNewParams{
		Model: "my-gpt4o-deployment",
		Messages: []openai.ChatCompletionMessageParamUnion{
			openai.SystemMessage("You are a helpful assistant."),
			openai.UserMessage("Hello, Azure OpenAI!"),
		},
	})
	if err != nil {
		log.Fatalf("Chat completion failed: %v", err)
	}

	log.Printf("Response: %s", response.Choices[0].Message.Content)
}
```

Set `AZURE_OPENAI_ENDPOINT` to your Azure OpenAI resource endpoint (e.g., `https://your-resource.openai.azure.com`); the `Model` you pass is your deployment name.

> [!NOTE]
>
> The middleware captures input and output content **by default** (`langwatch.DataCaptureAll`). Pass `azureopenai.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 Azure OpenAI call as an LLM span carrying its input, output and token counts.

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 Azure OpenAI applications


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