mirror of
https://github.com/langchain-ai/langserve.git
synced 2026-07-18 10:54:29 -04:00
What is the best way to get token usage via api response? #110
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @teeppp on GitHub (Jan 30, 2024).
I want to get the token usage to display token count in frontend application.
I found it has already included in callback_events when include_callback_event=True.
But, there are too much information.
is there how to exclude unnessesary infomartions from callback_events, or other way?
@eyurtsev commented on GitHub (Feb 1, 2024):
If you're using the remote runnable client with invoke use:
https://python.langchain.com/docs/modules/callbacks/token_counting
If you're doing it with your own custom code, then you'll need to filter until you encounter the appropriate callback event that corresponds to on llm end.
Keep in mind that:
One simple thing you could do:
@teeppp commented on GitHub (Feb 2, 2024):
@eyurtsev
Thank you for your reply. I understood it.
as you point it out, my application is not use the runnable client.
And, the your simple solution to use tokenizer on clientside is not good way for me because we have some intermediate steps depends on RAG. there are some internal prompts only in the backend application. this case, we cannot estimate it on client side accurately.
But, fortunately, my current application not use streaming for now. i will use callback as a short term solution.
As a future request, i hope it to implement to easily to some additional information to api response if possible.
@eyurtsev commented on GitHub (Feb 5, 2024):
One option is to do the calculation on the server side and return it as part of the response:
@teeppp commented on GitHub (Feb 14, 2024):
thanks. it seems the option is better way than using callback infos.
i will try it.