How to separately track token usage for multiple DSPy calls in Langfuse? #10653
-
Describe your questionProblem:I have two sequential DSPy calls in my pipeline (see code below), but currently Langfuse only displays the token usage from the last call. I need to track token consumption for each individual DSPy call separately and also see the total aggregated usage. Expected Behavior:I want to see token usage displayed separately for each DSPy call in the Langfuse tracing UI, similar to the demo screenshot below where both ai.streamText.doStream calls show their individual token consumption (2,988 → 19 and 6,703 → 1,214), and the total is correctly aggregated at the top (9,691 → 1,233). As below: Current Behavior:Only the last DSPy call's token usage is shown: Question:How can I properly instrument my code so that each DSPy call creates a separate span/generation in Langfuse with its own token usage metrics? Additional Context:In my current code, The update_langfuse_usage function calls langfuse.update_current_generation to track token usage. Both During execution, the terminal output correctly prints token usage statistics for both calls, confirming that the usage data is being captured. However, when I check the Langfuse tracing UI, only the token usage from the second (last) call is displayed. The first call's token usage is missing from the trace. My Current Code:Langfuse Cloud or Self-Hosted?Langfuse Cloud If self-hosted, what version are you running?No response SDK and integration versionsNo response Pre-Submission Checklist
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Solution: Create Separate Observations for Each DSPy CallThe issue is that both Recommended Approach: Use
|
Beta Was this translation helpful? Give feedback.


Solution: Create Separate Observations for Each DSPy Call
The issue is that both
my_llm_pipeline()andmy_llm_pipeline222()are updating the same generation (themainfunction decorated with@observe(as_type="generation")). To track token usage separately for each DSPy call, you need to create separate observations (spans or generations) for each call(1).Recommended Approach: Use
@observe()DecoratorWrap each DSPy pipeline function with the
@observe()decorator to create separate spans with their own token usage tracking(1):