Skip to content

Tracking Token Counter in RefineDocumentChain and MapReduceDocument Chain #26575

Closed Answered by dosubot bot
VedantR3907 asked this question in Q&A
Discussion options

You must be logged in to vote

Yes, there are alternative solutions to calculate the token counter without writing a custom script. You can use a language model's built-in token counting method or other available methods in LangChain. Here are some options:

Using a Language Model's Built-in Token Counting Method

You can use a language model's built-in token counting method, such as ChatOpenAI(model="gpt-4o").

Example:

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o")

def token_counter(messages):
    return llm.get_num_tokens(messages)

result = chain.invoke(split_docs)
total_tokens = token_counter(result)
print(f"Total token usage: {total_tokens}")

Using tiktoken for Custom Token Counting

If y…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
5 replies
@VedantR3907
Comment options

@dosubot
Comment options

@VedantR3907
Comment options

@dosubot
Comment options

Answer selected by VedantR3907
@VedantR3907
Comment options

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
1 participant