How to update generation cost and token usage? #4774
-
Hi, I was reading the documentation and I stumbled upon this doc which states that usage and cost of a generation can be update using My use case is to update the cost of a generation which belongs to a dataset: I am using an external LLM-as-a-judger with a custom cost function and I want to update the cost of a generation with the cost of the evaluation. I attempted to update the cost with the following code, but 'Usage' is immutable.
I am self-hosting Langfuse, version 2.93.6. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
To update the cost of a generation in Langfuse, you should use the # Fetch the generation you want to update
generation = self.langfuse.fetch_observations(trace_id=handler.trace.id, type='GENERATION')[0]
# Update the generation's cost
generation.update(usage={
"total_cost": cost
}) This approach allows you to update the cost without directly modifying the immutable To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other |
Beta Was this translation helpful? Give feedback.
This is partially correct.
You'd need to use the following in order to update an existing generation:
If possible, I'd try to add the cost information at run-time of the dataset run in order to prevent this update.