Skip to content

Conversation

@patriciomacadden
Copy link

@patriciomacadden patriciomacadden commented Dec 4, 2025

What this does

This PR uses the ActiveSupport::Notifications API to instrument RubyLLM. It's useful when you want to track costs or usage, without doing it explicitly in Chat, Image, etc.

Taking chat for example, we could track costs by using the end_message hook:

chat.on_end_message do |message|
  # calculate cost and save
end

but we need to do it manually in all chat instances (or other objects that talk to providers too!)

With ActiveSupport::Notifications we can subscribe to an event like this:

ActiveSupport::Notifications.subscribe("complete_chat.ruby_llm") do |event|
  # use the event to see duration, input/output tokens, etc., calculate cost and save
end

It is optional: the new RubyLLM::Instrumentation#instrument method doesn't emit any events if ActiveSupport::Notifications isn't available.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Performance improvement

Scope check

  • I read the Contributing Guide
  • This aligns with RubyLLM's focus on LLM communication
  • This isn't application-specific logic that belongs in user code
  • This benefits most users, not just my specific use case

Quality check

  • I ran overcommit --install and all hooks pass
  • I tested my changes thoroughly
    • For provider changes: Re-recorded VCR cassettes with bundle exec rake vcr:record[provider_name]
    • All tests pass: bundle exec rspec
  • I updated documentation if needed
  • I didn't modify auto-generated files manually (models.json, aliases.json)

API changes

  • Breaking change
  • New public methods/classes
  • Changed method signatures
  • No API changes

Related issues

No related issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant