The project is in a healthy, maintained state
Emits ActiveSupport::Notifications events for chat completions, embeddings, tool calls, and image generation. Works with any LLM provider. Subscribe to events for cost tracking, logging, analytics, or alerting.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 5.25
~> 3.1
~> 13.0

Runtime

 Project Readme

ask-instrumentation

Gem Version CI

LLM observability for the ask-rb ecosystem. Wraps ActiveSupport::Notifications and emits events for chat completions, embeddings, tool calls, and image generation. Works with any LLM provider: subscribe to events for cost tracking, logging, analytics, or alerting.

Installation

gem "ask-instrumentation"

Quick Start

require "ask/instrumentation"

# Subscribe to all ask events
Ask::Instrumentation.subscribe do |event|
  puts "#{event.name}: #{event.duration}ms"
end

# Instrument a block of work
Ask::Instrumentation.instrument("chat.ask", provider: "openai", model: "gpt-4") do
  # your LLM call here
end

# Attach context to every event emitted inside the block
Ask::Instrumentation.with_metadata(user_id: 42, session_id: "abc") do
  Ask::Instrumentation.instrument("chat.ask", provider: "openai", model: "gpt-4") do
    # ...
  end
end

Events

Event Description
chat.ask Chat completion
chat.stream.ask Streaming chat
tool.ask Tool execution
embedding.ask Embedding generation
image.ask Image generation
tool_call.ask LLM requested a tool call
tool_result.ask Tool call result (or error)

Event payloads carry provider, model, and token counts when available, plus any metadata set with with_metadata.

Essential API

Entry point Purpose
Ask::Instrumentation.subscribe(pattern = /\.ask$/, &block) Subscribe to matching events; returns a subscriber for unsubscribe
Ask::Instrumentation.unsubscribe(subscriber_or_pattern) Remove a subscriber
Ask::Instrumentation.instrument(name, payload = {}) { } Emit an event, timing the block and passing through its return value
Ask::Instrumentation.with_metadata(hash) { } Thread-local metadata merged into all events emitted inside the block
Ask::Instrumentation.current_metadata Current thread's metadata hash

Metadata lives in Thread.current, so concurrent threads each carry their own context. Nested with_metadata calls merge, with inner values taking precedence.

Full documentation

The full ask-rb documentation lives at https://ask-rb.github.io/ask-docs. https://ask-rb.github.io/ask-docs/production/observability covers ask-instrumentation in depth. API reference: https://ask-rb.github.io/ask-docs/reference/api.

Development

bundle install bundle exec rake test

License

MIT