Skip to content

fix(analytics): Langfuse cost/token tracking for agentflows + env override#1033

Merged
maxtechera merged 2 commits into
stagingfrom
fix/langfuse-cost-tracking
Mar 19, 2026
Merged

fix(analytics): Langfuse cost/token tracking for agentflows + env override#1033
maxtechera merged 2 commits into
stagingfrom
fix/langfuse-cost-tracking

Conversation

@maxtechera
Copy link
Copy Markdown
Collaborator

Summary

Cherry-picks upstream FlowiseAI/Flowise#5764 with additional fixes for our fork's env-override Langfuse integration.

Problem: All agentflow traces show $0 cost in Langfuse. Token usage, model name, and cost are missing because the langfuseCallbacksActive guard in onLLMStart skipped generation creation for flows with parent traces — which includes all agentflows and sub-flows called via Execute Flow.

Root cause: Known upstream issue (#4779 — 17 thumbsup, #5015, #4311).

Changes

From upstream PR FlowiseAI#5764

  • onLLMEnd now accepts structured output objects (string | object) with usage + model extraction
  • Normalizes token field names across LangChain/OpenAI formats
  • Extracts model name from responseMetadata
  • Adds model and usage (promptTokens/completionTokens/totalTokens) to generation.end()
  • Calls langfuse.flushAsync() to ensure data reaches Langfuse before request completes
  • LLM.ts, Agent.ts, ConditionAgent.ts pass full output object to onLLMEnd
  • Uses chatflow.name as trace name instead of hardcoded Agentflow

Fork-specific fixes (on top of upstream)

  • Removed langfuseCallbacksActive guard from onLLMStart — generations are now always created so onLLMEnd can record cost even when a parent trace exists (env override path)
  • Removed guard from onLLMError — errors are now recorded with level: ERROR on the generation
  • Restored full tag set on both handlerConfig and parentLangfuseTrace.update(): Name:, chatflow_id:, chat_id:, chatmessage_id: (upstream cherry-pick had reverted these to Name: only)

Verification matrix

Path Env override Tags Cost Errors
CHATFLOW standalone yes full set yes yes
CHATFLOW via Execute Flow yes full set yes yes
AGENTFLOW yes full set yes yes

Risk

Low-medium. The generation guard removal means both the manual AnalyticHandler path AND the LangChain CallbackHandler may create generations — potentially causing duplicate generation entries in Langfuse for the same LLM call. This is cosmetic (both will have correct data) and preferable to missing cost data entirely.

Related

@vercel
Copy link
Copy Markdown

vercel Bot commented Mar 18, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
answerai-docs Building Building Preview Mar 18, 2026 5:36pm
the-answerai Building Building Preview Mar 18, 2026 5:36pm

Request Review

…n usage and model name for Langfuse/LangSmith in agentflows

Cherry-picks FlowiseAI/Flowise@11261d20d with conflict resolution to
preserve our fork's custom analytics hooks (callbacks setup, parent trace
propagation).

Key changes:
- onLLMEnd now accepts structured output objects (not just strings)
- Extracts usageMetadata + responseMetadata from AIMessage responses
- Adds model name and usage data to Langfuse generation.end() calls
- Removes langfuseCallbacksActive guard from onLLMStart so generations
  are always created — ensures cost tracking works for agentflows with
  parent traces (env override path)
- Restores full tag set (Name, chatflow_id, chat_id, chatmessage_id)
  on both handlerConfig and parentLangfuseTrace.update()
- Adds flushAsync() to ensure generation updates reach Langfuse
- LLM.ts, Agent.ts, ConditionAgent.ts pass full output to onLLMEnd

Fixes: Langfuse cost/token tracking showing $0 for all agentflow traces.
Upstream: FlowiseAI#5764, fixes FlowiseAI#4779, FlowiseAI#5015, FlowiseAI#4311
@maxtechera maxtechera merged commit 9365283 into staging Mar 19, 2026
7 of 8 checks passed
@maxtechera maxtechera deleted the fix/langfuse-cost-tracking branch March 19, 2026 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant