Tracking token usage when using RunnableWithMessageHistory and Streaming #5880
Replies: 4 comments 10 replies
-
To track the token usage based on each session when using import { ChatOpenAI } from "@langchain/openai";
import { RunnableWithMessageHistory } from "your-runnable-with-message-history-module"; // Replace with actual import
const llm = new ChatOpenAI({
modelName: "gpt-3.5-turbo-0125",
temperature: 0,
callbacks: [
{
handleLLMEnd(output) {
if (output.llmOutput && output.llmOutput.tokenUsage) {
console.log("Token usage:", output.llmOutput.tokenUsage);
}
},
},
],
});
const prompt = ChatPromptTemplate.fromMessages([
["system", system],
["placeholder", "{chat_history}"],
["human", "{input}"],
]);
const chain = prompt.pipe(llm);
const chatwithMessageHistory = new RunnableWithMessageHistory({
runnable: chain,
getMessageHistory: (_sessionId) =>
new UpstashRedisChatMessageHistory({
sessionId: `${_sessionId}-${ai}-Chat`,
sessionTTL: 300,
config: {
url: process.env.REDIS_URL!,
token: process.env.REDIS_TOKEN!,
},
}),
inputMessagesKey: "input",
historyMessagesKey: "history",
onMessage: (message) => {
if (message.response_metadata && message.response_metadata.tokenUsage) {
console.log("Token usage for session:", message.response_metadata.tokenUsage);
}
},
});
// Example usage
const stream = await chatwithMessageHistory.stream(
{
input: message,
},
{
configurable: {
sessionId: sessId,
},
}
);
const chunks = [];
for await (const chunk of stream) {
console.log(chunk);
chunks.push(chunk);
} In this example, the |
Beta Was this translation helpful? Give feedback.
-
@dosu I get Object literal may only specify known properties, and 'onMessage' does not exist in type 'RunnableWithMessageHistoryInputs<any, AIMessageChunk>'.ts(2353) error |
Beta Was this translation helpful? Give feedback.
-
Try setting the As for processing the usage while streaming, I haven't used callbacks much, but maybe the documentation can help you with those. Alternatively you can try the streamEvents API. You'd be looking for the 'on_llm_end' event here. |
Beta Was this translation helpful? Give feedback.
-
const llm = new ChatOpenAI({
modelName: "gpt-3.5-turbo-0125",
temperature: 0,
streaming: true,
streamUsage: true,
});
const prompt = ChatPromptTemplate.fromMessages([
["system", system],
["placeholder", "{chat_history}"],
["human", "{input}"],
]);
const chain = prompt.pipe(llm);
const chatwithMessageHistory = new RunnableWithMessageHistory({
runnable: chain,
getMessageHistory: (_sessionId) =>
new UpstashRedisChatMessageHistory({
sessionId: "session id goes here",
sessionTTL: 300,
config: {
url: "url goes here",
token: "token goes here",
},
}),
inputMessagesKey: "input",
historyMessagesKey: "history",
});
/*Code above remains the same*/
// Solution for stream Events - useful for extracting extra data from the stream
const streamEvent = chatwithMessageHistory.streamEvents(
{
input: message,
},
{
version: "v1",
configurable: {
sessionId: sessId,
},
}
);
for await (const chunk of streamEvent) {
if (chunk.event == "on_chain_end" && chunk.tags?.length == 0) {
console.log(chunk);
if (chunk.data.output.usage_metadata !== undefined) {
console.log(chunk.data.output.usage_metadata);
totalTokens = chunk.data.output.usage_metadata.total_tokens;
console.log(
`Session: ${chunk.metadata.sessionId}\nTotal tokens: ${totalTokens}`
);
}
}
if (chunk.event == "on_chain_stream") {
console.log(chunk.data.chunk.content);
}
}
// Solution for stream - better for just getting the content and usage
const stream = await chatwithMessageHistory.stream(
{
input: message,
},
{
configurable: {
sessionId: sessId,
},
}
);
for await (const chunk of stream) {
if (chunk.usage_metadata) {
console.log(chunk.usage_metadata.total_tokens);
}
console.log(chunk.content)
} will keep this discussion open for a day or so |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
I am trying to track the token usage based on each session, however the sample code provide on the documentation only works on the bare bones model and setup with the message history or anything. What can I add to track the token usage with the code above. Please note that I am using streaming and NOT invoke.
System Info
Node v18.18.0
"@langchain/anthropic": "^0.1.9",
"@langchain/community": "^0.2.13",
"@langchain/core": "^0.2.9",
"@langchain/openai": "^0.1.3",
"langchain": "^0.2.5",
Beta Was this translation helpful? Give feedback.
All reactions