Skip to content

What is the best way to get token usage via api response? #497

Answered by eyurtsev
teeppp asked this question in Q&A
Discussion options

You must be logged in to vote

One option is to do the calculation on the server side and return it as part of the response:

from langchain_openai.chat_models import ChatOpenAI
from langchain_core.prompts import PromptTemplate
from langchain_core.runnables import RunnableMap, RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser

prompt = PromptTemplate.from_template("{input}")

def calculate_tokens(text: str) -> int:
    return len(text) # Replace with appropriate tokenization logic

model = ChatOpenAI()

chain = prompt | RunnableMap({
    "output": model,
    "input_tokens": lambda prompt: calculate_tokens(prompt.text)
}) | RunnablePassthrough.assign(output_tokens=lambda x: calculate_tokens(x['…

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by teeppp
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #432 on February 28, 2024 18:41.