ask-tokens
Pure-Ruby token accounting: count real tokens, price them at any rate, declare activity costs, and run a wallet/ledger engine against a pluggable store. Rails-free by design — pair with ask-tokens-rails for ActiveRecord persistence.
Installation
gem "ask-tokens"Usage
require "ask-tokens"
Ask::Tokens.configure do |c|
c.price_per_1m = Money.from_amount(100, "USD")
end
Ask::Tokens.activity(:chat_message) do |params|
Ask::Tokens.count_tokens(params[:input]) +
Ask::Tokens.count_tokens(params[:output])
end
wallet = Ask::Tokens.wallet_for("user:42")
wallet.grant!(1000, reason: :signup)
wallet.spend!(:chat_message, input: "hi", output: "yo") do
LLM.chat(...) # only charged when the block succeeds
end
wallet.balance # => 996Token counting uses tiktoken (model-aware encoding, cl100k_base
fallback); pricing uses the Money gem at a configurable per-million
rate. The in-memory store ships for scripts and tests; bring your own
store (e.g. ActiveRecord via ask-tokens-rails) for production.
Contributing
See CONTRIBUTING.md. Run the suite with
bundle exec rake test.
License
MIT License. See LICENSE.