patterns-ai-core / langchainrb

Build LLM-powered applications in Ruby
https://rubydoc.info/gems/langchainrb
MIT License
1.42k stars 195 forks source link

Track tokens count at the Assistant level #693

Closed dferrazm closed 4 months ago

dferrazm commented 4 months ago

Add state to the Langchain::LLM::Assistant class in order to keep track of the used tokens counts throughout the whole chat interaction with the assistant instance. The following information can now be accessed at the assitant level:

assistant.total_prompt_tokens # sum of all prompted tokens
assistant.total_completion_tokens # sum of all completion tokens
assistant.total_tokens # sum of the two above

The information is basically extracted from the Langchain::LLM::BaseResponse object.