Agent API doesn't return token usage information #5900
Replies: 2 comments 1 reply
-
Hey there, @erik-balfe! I'm here to help you with your coding adventures. Let's squash those bugs and unravel the mysteries of software together! 🤖 To retrieve token usage data from LangChain's agent functionality, you can use the import { ChatGroq } from "npm:@langchain/groq@0.0.12";
import { ChatPromptTemplate } from "npm:@langchain/core@0.2.8/prompts";
import { HumanMessage, SystemMessage } from "npm:@langchain/core@0.2.8/messages";
import { WikipediaQueryRun } from "npm:@langchain/community@0.2.12/tools/wikipedia_query_run";
import { AgentExecutor, createToolCallingAgent } from "npm:langchain@0.2.5/agents";
const GROQ_API_KEY = 'exampleKey';
async function exampleAgentRequest() {
const llmClient = new ChatGroq({ apiKey: GROQ_API_KEY });
const chatMessages = [
new SystemMessage({ content: "You are a helpful assistant." }),
new HumanMessage({ content: "Who is the current president of France?" }),
];
const wikipediaTool = new WikipediaQueryRun({
topKResults: 3,
maxDocContentLength: 20000,
});
const tools = [wikipediaTool];
const promptTemplate = ChatPromptTemplate.fromMessages(chatMessages);
const agent = createToolCallingAgent({ llm: llmClient, tools, prompt: promptTemplate });
const agentExecutor = new AgentExecutor({ agent, tools, returnIntermediateSteps: true });
const result = await agentExecutor.invoke({
template: promptTemplate,
});
// Assuming the response includes usage metadata
console.log(result.usage_metadata);
return result;
}
await exampleAgentRequest(); This code will log the token usage data, including |
Beta Was this translation helpful? Give feedback.
-
I've discovered that this issue is specific to the Groq provider. When using other providers, such as Anthropic, the usage data is indeed available. For example, with Anthropic, the usage information can be found in: intermediateSteps[0].action.messageLog.kwargs.additional_kwargs.usage This suggests that the problem likely lies with either the Groq library or with Groq's backend, which may not return usage data specifically for tool calls. To clarify for other users and developers:
Given this new information, it might be worth investigating:
This update narrows down the scope of the original issue and provides a potential workaround for users who have the flexibility to switch to a different provider. However, for those specifically using or requiring Groq, the problem of accessing usage data in agent calls remains unresolved. Suggestion to LangChain library developers: |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Provided code returns {input, output} object with text values.
But the important part from API is lost - I can not get usage data.
Beta Was this translation helpful? Give feedback.
All reactions