---
title: "Go"
description: "Integrate Traceloop SDK for Go with Middleware LLM Observability to track AI application traces and ensure efficient execution."
section: "LLM Observability"
breadcrumb: "LLM Observability > Traceloop > Go"
canonical: "https://docs.middleware.io/features/llm/traceloop-go"
---
# Go

This guide demonstrates how to integrate the Traceloop SDK with Middleware for LLM Observability in a Go application and will walk you through the installation, initialization, and then (since Go lacks auto-instrumentation yet) log prompts/completions manually.

## Before you begin

- Credentials: Have your Middleware UID (for the ingest `BaseURL`) and Middleware API key (for the `Authorization` header).
- Manual logging in Go: Traceloop does not auto-instrument Go LLM libraries yet, so you’ll explicitly log prompts and completions (example below).
- Run a real call: After initialising, make at least one LLM request to generate traces you can see in Middleware.

## 1. Install the SDK

Run this in your terminal to add the Traceloop SDK to your Go project:

```bash
go get github.com/traceloop/go-openllmetry/traceloop-sdk
```

## 2. Initialise the SDK

In your LLM service, import the SDK and create the client with your tenant URL and headers:

```js
import sdk "github.com/traceloop/go-openllmetry/traceloop-sdk"

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

  traceloop := sdk.NewClient(config.Config{
    BaseURL: "https://<MW_UID>.middleware.io:443",
    Headers: map[string]string{
            "Authorization": "<MW_API_KEY>",
            "X-Trace-Source": "traceloop",
        },
  })
  defer func() { traceloop.Shutdown(ctx) }()

  traceloop.Initialize(ctx)
}
```

## 3. Log your prompts

Because Go libraries aren’t auto-instrumented yet, log prompts and completions explicitly around your LLM calls (example with OpenAI):

```js
import (
    openai "github.com/sashabaranov/go-openai"
)

func call_llm() {
	// Call OpenAI like you normally would
	resp, err := client.CreateChatCompletion(
		context.Background(),
		openai.ChatCompletionRequest{
			Model: openai.GPT3Dot5Turbo,
			Messages: []openai.ChatCompletionMessage{
				{
					Role:    openai.ChatMessageRoleUser,
					Content: "Tell me a joke about OpenTelemetry!",
				},
			},
		},
	)

	// Log the request and the response
	log := dto.PromptLogAttributes{
		Prompt: dto.Prompt{
			Vendor: "openai",
			Mode:   "chat",
			Model:  request.Model,
		},
		Completion: dto.Completion{
			Model: resp.Model,
		},
		Usage: dto.Usage{
			TotalTokens:      resp.Usage.TotalTokens,
			CompletionTokens: resp.Usage.CompletionTokens,
			PromptTokens:     resp.Usage.PromptTokens,
		},
	}

	for i, message := range request.Messages {
		log.Prompt.Messages = append(log.Prompt.Messages, dto.Message{
			Index:   i,
			Content: message.Content,
			Role:    message.Role,
		})
	}

	for _, choice := range resp.Choices {
		log.Completion.Messages = append(log.Completion.Messages, dto.Message{
			Index:   choice.Index,
			Content: choice.Message.Content,
			Role:    choice.Message.Role,
		})
	}

	traceloop.LogPrompt(ctx, log)
}
```

## Viewing your traces

Once initialised and after at least one LLM request, open the [LLM Observability section in Middleware](https://app.middleware.io/llm/traces?) to see spans/traces for your Go service.

You’ll get visibility into LLM calls and any external services involved. For more in-depth SDK options, refer to the [Traceloop Go documentation](https://www.traceloop.com/docs/openllmetry/getting-started-go).

## Troubleshooting

- No traces appearing: Ensure you executed at least one LLM request after initialisation and that your service can reach `https://<MW_UID>.middleware.io:443`.
- Auth errors: Re-check the `Authorization` header value (Middleware API key) and that the `BaseURL` uses your correct tenant UID.
- Nothing around prompts/completions: Go needs manual logging, so you are required to wrap your LLM calls as shown in the “Log your prompts” example.

> **Note:** Need assistance or want to learn more about using Traceloop with Middleware? Get in touch with us via our [Contact Us](https://middleware.io/contact-us/).
