class RubyLLM::Tokens
A Tokens holds the token counts a provider reported for a single response. Instances are read from Message#tokens and Chunk#tokens. Counts the provider did not report are nil.
response = chat.ask "What is the capital of France?" response.tokens.input # standard input tokens response.tokens.output # billable output tokens response.tokens.cache_read # prompt cache reads response.tokens.cache_write # prompt cache writes
Attributes
The number of tokens served from the provider’s prompt cache, or nil if the provider did not report it.
The number of tokens written to the provider’s prompt cache, or nil if the provider did not report it.
The number of standard (non-cached) input tokens, or nil if the provider did not report it.
The number of billable output tokens, or nil if the provider did not report it. Includes thinking tokens when the provider bills them as output.
The number of thinking (reasoning) tokens, or nil if the provider does not report them.
Public Instance Methods
Source
# File lib/ruby_llm/tokens.rb, line 63 def to_h { input_tokens: input, output_tokens: output, cache_read_tokens: cache_read, cache_write_tokens: cache_write, thinking_tokens: thinking }.compact end
Returns the counts as a hash with keys :input_tokens, :output_tokens, :cache_read_tokens, :cache_write_tokens, and :thinking_tokens, omitting nil counts.
response.tokens.to_h # => {input_tokens: 14, output_tokens: 5}