[PR #13117] pass model and cost so that langfuse can show cost #27846

Closed
opened 2026-02-21 20:42:19 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/13117

State: closed
Merged: Yes


Summary

Dify's integration with Langfuse doesn't show generation cost.

Closes #10684 (and probably more similar issues).

Details

The code that transforms Message into dict to attach as message_data in trace_info object doesn't include total_price and model_id, which are necessary for Langfuse to calculate the cost. The same is true about the code responsible for instantiating a Message object from a dict.

Some things to be aware of:

  • I can pass the model ID alone, and Languse will use this with the rest of the available information to infer the cost, but I thought that it would be better to pass the cost if it is available. Should this be reconsidered?
  • usage_details is deprecated

Screenshots

Before:
image

After:
image

Checklist

Important

Please review the checklist below before submitting your pull request.

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
**Original Pull Request:** https://github.com/langgenius/dify/pull/13117 **State:** closed **Merged:** Yes --- # Summary Dify's integration with Langfuse doesn't show generation cost. Closes #10684 (and probably more similar issues). # Details The code that transforms `Message` into dict to attach as `message_data` in `trace_info` object doesn't include `total_price` and `model_id`, which are necessary for Langfuse to calculate the cost. The same is true about the code responsible for instantiating a `Message` object from a dict. Some things to be aware of: - I can pass the model ID alone, and Languse will use this with the rest of the available information to infer the cost, but I thought that it would be better to pass the cost if it is available. Should this be reconsidered? - [`usage_details` is deprecated](https://python.reference.langfuse.com/langfuse/client#Langfuse.generation) # Screenshots Before: <img width="354" alt="image" src="https://github.com/user-attachments/assets/f4bfdd5a-191e-4052-a8cd-2ce0b23264b8" /> After: <img width="358" alt="image" src="https://github.com/user-attachments/assets/b1bef2d4-e84f-4f56-9be4-895595ed0cf2" /> # Checklist > [!IMPORTANT] > Please review the checklist below before submitting your pull request. - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [ ] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [x] I've updated the documentation accordingly. - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods
yindo added the pull-request label 2026-02-21 20:42:19 -05:00
yindo closed this issue 2026-02-21 20:42:19 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#27846