---
title: "Ruby"
description: "Use Traceloop SDK with Middleware LLM Observability in Ruby to capture AI traces, monitor performance, and detect runtime issues."
section: "LLM Observability"
breadcrumb: "LLM Observability > Traceloop > Ruby"
canonical: "https://docs.middleware.io/features/llm/traceloop-ruby"
---
# Ruby

This guide walks you through wiring the Traceloop SDK to Middleware for LLM Observability in a Ruby app. The core idea: initialise Traceloop in your app, then (because Ruby doesn’t have auto-instrumentation yet) log prompts/completions around your LLM calls.

## Before you begin

- Credentials: Have your Middleware UID (for the ingest `base_url`) and Middleware API key (for the `Authorization` header).
- Rails placement: If you use Rails, put the initialiser in `config/initializers/traceloop.rb`.
- Manual logging: Ruby libraries are not yet auto-instrumented, and you’ll need to log prompts and completions explicitly (see the example below).

## 1. Install the SDK

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

```bash
gem install traceloop-sdk
```

## 2. Initialize the SDK

Add this to your LLM application to set up the Traceloop tracer (fill in your tenant UID and API key):

```js
require "traceloop/sdk"

traceloop = Traceloop::SDK::Traceloop.new(
  base_url: "https://<MW_UID>.middleware.io:443",
  api_key: "<MW_API_KEY>"
)

# Add headers
traceloop.add_headers({
  "Authorization" => "<MW_API_KEY>",
  "X-Trace-Source" => "traceloop"
})
```

> **Warning:** **Note**: If you’re on Rails, place this initialization in `config/initializers/traceloop.rb`.

## 3. Log Your Prompts

Since Ruby doesn’t auto-instrument LLM libraries yet, wrap your calls and log the prompt/completion (example with OpenAI):

```js
require "openai"

client = OpenAI::Client.new

# This tracks the latency of the call and the response
traceloop.llm_call(provider: "openai", model: "gpt-3.5-turbo") do |tracer|
  # Log the prompt
  tracer.log_prompt(user_prompt: "Tell me a joke about OpenTelemetry")

  # Call OpenAI like you normally would
  response = client.chat(
    parameters: {
      model: "gpt-3.5-turbo",
      messages: [{ role: "user", content: "Tell me a joke about OpenTelemetry" }]
    })

  # Pass the response from OpenAI as is to log the completion and token usage
  tracer.log_response(response)
end
```

## Viewing Your Traces

After setup and at least one LLM request, open the [LLM Observability section in Middleware](https://app.middleware.io/llm/traces?) to view spans and traces across your Ruby service and any external calls.

For deeper options, see the [Traceloop Ruby documentation](https://www.traceloop.com/docs/openllmetry/getting-started-ruby).

## Troubleshooting

- No traces appearing: Make sure you executed at least one LLM request after initialisation and that the service can reach `https://<MW_UID>.middleware.io:443`.
- Auth errors: Re-check the `Authorization` header (Middleware API key) and your tenant `base_url`.
- Nothing around prompts/completions: Remember that Ruby requires manual logging, and you need to use the “Log Your Prompts” pattern above.

> **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/).
