Title: Allow reading AI sub-node execution data (e.g. token usage) from main-flow nodes
n8n version: 2.33.4 (self-hosted)
Is your feature request related to a problem? Please describe.
AI Agent workflows (@n8n/n8n-nodes-langchain.agent) can’t report their own token usage/cost anywhere in the main workflow flow. The Chat Model sub-node (e.g. lmChatOpenAi) records a tokenUsageEstimate: { promptTokens, completionTokens, totalTokens } object on each of its runs — and an agent using tools typically triggers multiple runs of that sub-node per execution (one per ReAct iteration). This data is real and present in the execution — it’s visible in the Executions UI/API — but it is not reachable from any node in the main data flow.
Referencing the sub-node from a downstream Code/Set node, e.g.:
$('OpenAI Chat Model').all()
throws:
ExpressionError: No data found from `main` input
This happens because Chat Model / Tool / Memory / Output Parser sub-nodes connect to the Agent via ai_languageModel / ai_tool / ai_memory / ai_outputParser connections, not main, and $() node lookups appear to be restricted to nodes reachable through main connections. This applies regardless of accessor (.all(), .first(), .item, legacy $node[...]) — the restriction is structural, not syntactic.
Generic use case: any workflow that wants to report, log, alert on, or persist LLM token usage/cost as part of its own execution — e.g. appending “tokens used” to a Slack/email notification, writing usage to a database/data table for cost tracking, or triggering an alert when a single execution exceeds a token budget. Right now none of this is possible without an external, out-of-band lookup.
Describe the solution you’d like
Any of the following would solve this (roughly in order of preference):
- Aggregate token usage on the Agent node’s own main output. Have
@n8n/n8n-nodes-langchain.agentsumtokenUsageEstimateacross all of its language-model sub-node calls for the execution and include it in its own output JSON alongsideoutput, e.g.{ output: "...", tokenUsage: { promptTokens, completionTokens, totalTokens } }. This requires no new expression syntax and is discoverable simply by inspecting the Agent’s output. - A dedicated expression helper, e.g.
$('AI Agent').tokenUsage()or$agent('AI Agent').tokenUsage, that returns the aggregated usage for a named Agent node in the current execution. - Relax the
main-connection restriction for read-only access to sub-node run data — i.e. let$('SubNodeName').all()succeed for nodes connected viaai_languageModel/ai_tool/ai_memory/ai_outputParser, returning all runs (not just a “current” one, since these sub-nodes have no single “current” item to pair against). - Execution-level aggregate, e.g.
$execution.tokenUsage.totalTokens, that rolls up all AI-related sub-node token usage for the whole run, accessible from any node downstream.
Describe alternatives you’ve considered
The only current workaround is to have a node in the main flow call n8n’s own REST API (GET /executions/{id}?includeData=true, using {{$execution.id}}) to fetch the same run’s data mid-execution, then parse runData['<Chat Model Node>'] for tokenUsageEstimate client-side. This requires:
- Creating and attaching an n8n API key credential just for this purpose
- Enabling
saveExecutionProgresson the workflow so per-node data is persisted before the run finishes - A fragile, self-referential HTTP call that most workflow builders wouldn’t think to reach for
This is far too much ceremony for what should be a basic reporting need, and it doesn’t work at all in environments where API keys or saveExecutionProgress aren’t available/enabled.
Additional context
Happy to share the workflow / a minimal repro if useful — a Manual Trigger → AI Agent (with any tool that forces >1 ReAct iteration) → Code node referencing the Chat Model node name reliably reproduces the ExpressionError: No data found from 'main' input error.