0.01
The project is in a healthy, maintained state
Logs every call your Rails app makes to OpenAI, Anthropic, Gemini, RubyLLM, or an OpenAI-compatible API: tokens, cost, latency, tags. Calls go straight to the provider — no proxy. Includes price sync, budget guardrails, and a mountable dashboard.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 13.0
~> 3.0
~> 1.0
~> 3.0
~> 0.22
~> 1.16
~> 1.6

Runtime

>= 7.1, < 9.0
>= 2.0, < 3.0
~> 3.0
>= 7.1, < 9.0
>= 7.1, < 9.0
 Project Readme

LLM Cost Tracker

Self-hosted LLM cost tracking for Rails.

Gem Version CI codecov

Every call your app makes to OpenAI, Anthropic, Gemini, RubyLLM, or any OpenAI-compatible API gets logged: tokens, cost, latency, tags. Calls go app → provider direct. No proxy.

Not Langfuse, Helicone, or LiteLLM. No prompts, no traces, no replay. Spend attribution only.

Requires Ruby 3.4+, Rails 7.1+, PostgreSQL or MySQL.

LLM Cost Tracker dashboard

Quickstart

# Gemfile
gem "llm_cost_tracker"
gem "openai"
bin/rails llm_cost_tracker:setup

Runs the install generator, drops a price snapshot, migrates the database, and verifies via llm_cost_tracker:doctor. The generated config/initializers/llm_cost_tracker.rb looks like:

LlmCostTracker.configure do |config|
  config.default_tags = -> { { environment: Rails.env } }
  config.instrument :openai
end

Edit it in place to add tags, switch on async ingestion, etc.

Tag your calls to attribute spend:

LlmCostTracker.with_tags(user_id: Current.user&.id, feature: "chat") do
  client = OpenAI::Client.new(api_key: ENV["OPENAI_API_KEY"])
  client.responses.create(model: "gpt-4o", input: "Hello")
end

Mount the dashboard in config/routes.rb, behind your auth:

authenticate :admin do
  mount LlmCostTracker::Engine => "/llm-costs"
end

The engine ships without authentication on purpose.

What lands in the ledger

  • Calls. Provider, model, total tokens, total cost, latency, status.
  • Line items. Per-component breakdown — text/audio/cached tokens, tool charges (web search, code execution, grounding, container sessions).
  • Tags. Whatever attribution you pass — user, feature, tenant, env.
  • Provider IDs. Response, project, API key, workspace — for downstream audits.
  • Pricing snapshot. So historical numbers don't drift when prices change.

Capture surfaces

Surface Path
OpenAI Official SDK or Faraday
Anthropic Official SDK or Faraday
Azure OpenAI Faraday or official SDK (auto-detected on *.openai.azure.com and Foundry *.services.ai.azure.com, both deployments and /openai/v1/...)
Google Gemini Faraday
RubyLLM Provider layer
ruby-openai Faraday
OpenRouter, DeepSeek, Groq, LiteLLM-style gateways OpenAI-compatible Faraday
Anything else LlmCostTracker.track

Streams capture when the provider emits final usage. OpenAI Faraday streams get stream_options: { include_usage: true } auto-injected so the final usage chunk lands in the ledger (opt out via config.auto_enable_stream_usage = false).

What it isn't

  • No proxy. Direct calls only.
  • No prompts. Token counts and metadata only.
  • No traces, evals, or prompt management. Different product, different gem.
  • Not multi-service. Built for a Rails monolith.

Manual tracking

For batch jobs, internal gateways, or anything without an SDK/Faraday hook:

LlmCostTracker.track(
  provider: :anthropic,
  model: "claude-sonnet-4-6",
  tokens: { input_tokens: 1500, output_tokens: 320 },
  tags: { feature: "summarizer", user_id: current_user.id }
)

Docs

Development

bundle install
bin/check

License

MIT — see LICENSE.txt.