Skip to main content
RubyLLM is a Ruby gem that provides a unified interface for AI providers including OpenAI, Anthropic, Google Gemini, AWS Bedrock, and Mistral. Braintrust traces chat completions, tool executions, and streaming responses across whichever provider you route through.

Setup

Install the Braintrust and RubyLLM gems, then configure your API keys.
1

Install gems

2

Set environment variables

.env

Auto-instrumentation

To trace RubyLLM without modifying how you construct chats, load braintrust/setup before you require ruby_llm. Braintrust intercepts the require and patches RubyLLM::Chat, so every chat, tool, and streaming call emits a span. Loading braintrust/setup also initializes Braintrust for you and logs to the project set by BRAINTRUST_DEFAULT_PROJECT.
In a Rails app, add gem "braintrust", require: "braintrust/setup" to your Gemfile to enable auto-instrumentation without an explicit require line.

Manual instrumentation

To trace RubyLLM calls manually, enable instrumentation yourself with Braintrust.instrument!(:ruby_llm). Once enabled, every chat, tool, and streaming call emits a span. The following app.rb initializes Braintrust, instruments RubyLLM, and makes a request:
To instrument a single chat instead of every RubyLLM chat, pass the instance as a target: Braintrust.instrument!(:ruby_llm, target: chat).

What Braintrust traces

Braintrust captures:
  • Chat completion spans (ruby_llm.chat), with input messages, the model, and provider metadata, plus the assistant response and finish reason.
  • Tool execution spans (ruby_llm.tool.<name>), with the tool name, call ID, arguments, and returned result. Tool schemas passed to the model are captured on the chat span.
  • Token usage metrics (prompt, completion, total, and cached tokens) for chat completions.
  • Streaming responses, aggregated onto the chat span with time to first token.
  • Multimodal message content, including text with image and file attachments.
  • Errors, recorded on the span when a request raises.

Resources