How to get AI token usage information from AI Agent?

How can I get this information? It’s shown in the chat model, but I can’t access this data in my workflow.

I’ve seen workarounds here in the community, but maybe now there’s an official way?

Information on your n8n setup

  • n8n version: 1.84.3
  • Database (default: SQLite): default
  • n8n EXECUTIONS_PROCESS setting (default: own, main): main
  • Running n8n via (Docker, npm, n8n cloud, desktop app): docker
  • Operating system: Ubuntu 24.04

EDIT: Solution Found :white_check_mark:

You can retrieve the execution data as a workaround and get the token usage information directly from the LLM model node.

3 Likes

I copied this workflow as-is into the same version (1.84.3) of n8n (community) and my output doesn’t have that part (tokenUsage) at all. Is there something you have enabled in your settings for your actual OpenAI account to make that appear in the response?

The information is in the OpenAI Chat Model node, not on the AI Agent node.
Did you open that one?

And that is the problem. The information is not on the output of the AI Agent, and I don’t know how to retrieve it from the chat model node :confused:

Ah, ok. Thanks for clarifying. It is kinda twice as annoying when you know something is there, but you can’t reach it.

1 Like

In case you want to cross this off your list of things to try, or build on the idea and see if you can get it to work, this looked promising (a document got inserted), but the actual info wasn’t stored.

2 Likes

So the agent has access to the subnode token usage data? If you ask it, it will answer?

IDK. I just thought it would be worth a try. I would assume if anything in the workflow has access to the tokenUsage info from the model, it would be the agent.

However, it would be a weird interaction. What I tried would essentially be telling the AI to return tool-usage instructions to the agent (somehow) with enough understanding of n8n internals, and the MongoDB client tool, and itself, to create a document, populate its data, and insert it. So, it “knew” some of the MongoDB part, but I was just speculating on whether it would “know” enough to tell the agent how to grab the tokenUsage stats. It was probably futile.

Also, adding that to the System Message ends up making another “tool usage” call to the AI, using up more tokens, so observing it changes it.

1 Like

Alguem ja tentou fazer a consulta por api direto no site da openai.com?

Hi @solomon

Is this what you’re looking for?

Unfortunately no, @mohamed3nan

What I’m trying to get is the information from the model node:

If I open this node I can see the token usage data:

But if I try to get that data on a subsequent node, this is all I see:

1 Like

Hi

I’m sure you can get it from the underlying tables:

Here’s a workflow which was posted recently which is surely based on that:

Reg,
J.

1 Like

Unfortunately that’s an estimator, not real data extraction from the workflow.
@jcuypers

From what I can see it gets the text, the AI model and sends to an external service to estimate the cost. Very smart, but still cumbersome.

Curious, what’s the use case for that? Is it about managing costs for outliers?

Hi,

Potential use cases for such a solution:

  • Cost management (internal/as well client facing)
  • Security and abuse.
  • Alerting: or the same as above
  • Performance / debugging / monitoring: Response time per workflow Vs tokens used
  • Unified solution: I was looking into the open AI stats to add to a grafana dashboard, but this could be across model providers

I’m sure the could be more

1 Like

I run n8n as the backend of a SaaS project. Whenever the users intereact with our agents, I want to save the token usage, so I can deduct from their token balance.

Right now the temporary solution is to use the OpenAI nodes, instead of using the AI Agent node.

But very soon we’ll need the AI Agent node too.

1 Like

Posting the solution here so other people can find it:

Retrieving the execution data allows to get the token usage by the model.

This was posted by @Antony_Eardrop on this topic here

Since the solution provided by @jcuypers contains this information (although it’s a paid one) I’ll mark his reply as the solution and append my reply on my question at the top.

This way both useful informations are easy to find by future people having the same issue.

5 Likes

Is it possible to run this n8n node in the same workflow?
Maybe at the end of the nodes.

You should put it in a subworkflow.
It can be contained in the same workflow file, but I wouldn’t use it in the same execution. Otherwise you’ll end up processing a lot more data.

1 Like

thanks for the response. I just noticed that with the data.
Is there a way to somehow subscribe to a workflow and get results?
One more thing is what if i add a custom node at the end of the workflow?

Can you please ‘upvote’ this feature