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:
  • With RubyLLM 2.x, turn spans (ruby_llm.complete), one for each call such as chat.ask, with the input messages and the final response. The turn’s chat completion and tool execution spans nest under it, so a tool-calling turn stays in one trace even when your code doesn’t wrap the call in its own span. Token usage is recorded on the chat completion spans, not the turn span. Requires Braintrust Ruby SDK v0.6.1 or later.
  • Chat completion spans (ruby_llm.chat), one for each request to the model, 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, cached, and cache creation tokens) for chat completions. Prompt tokens include cached and cache creation tokens.
  • 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