class RubyLLM::Tokens

A Tokens holds normalized token counts for a provider attempt, a response, or an aggregate. Read them from Chat#tokens, Message#tokens, Chunk#tokens, and individual operation results such as Embedding. 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

Operations a provider bills in other units count those units here too, so Cost prices them the same way:

RubyLLM.speak("Hello").tokens.characters             # => 5
RubyLLM.transcribe("call.mp3").tokens.audio_seconds  # => 42.3
RubyLLM.rerank(query, documents).tokens.search_units # => 1