SDK-Python :- Cost calculation for custom model #8659
|
I am using a custom model which return token counts and content. i have defined model defination on langfuse ui with usage details as prompttoken and cadidatetoken as keys but not able to get cost getting 0 cost only. Llm.invoke(query)-> AIMessage(content,tokenDetail) |
Replies: 8 comments 4 replies
|
To get cost calculation working for your custom model in Langfuse, the keys in your usage details ( Make sure:
For example, if your model definition uses generation.update(
usage_details={
"prompttoken": <value>,
"cadidatetoken": <value>
}
)If you want to change to the standard keys ( You can review more details here: Model Usage & Cost Tracking. 📚 Sources: Have another question? Just tag @inkeep. |
|
When using the Langchain default callback handler with Langfuse, cost calculation depends on the model name and usage details being passed correctly from Langchain to Langfuse. If you are using a custom model, ensure the following:
If Langchain is not sending the correct model name or usage detail keys by default, you may need to override or extend the callback handler to ensure they are set as expected(2). You can review more about custom model handling and cost calculation here: Model Usage & Cost Tracking, how to pass custom user managed model name to calculate the cost via langchain. 📚 Sources:
Have another question? Just tag @inkeep. |
|
For cost calculation to work in Langfuse when using a custom model, the usage details must be passed to Langfuse in a way that exactly matches the keys defined in your model definition. When using Langchain with the Langfuse callback handler, you need to ensure:
If usage details are not sent automatically by Langchain, you may need to update the generation with the usage details after you get the LLM response. The correct format is a dictionary where keys match your model definition (e.g., if your model uses "prompttoken" and "cadidatetoken", use those keys): generation.update(
usage_details={
"prompttoken": <value>,
"cadidatetoken": <value>
}
)The model name should be set so it matches your Langfuse model definition, and the usage details must use the same key names as in your model definition. If your LLM response provides token counts under different names, map them to the correct keys before passing them to Langfuse. If you want to pass a custom model name in Langchain, you can do so using the metadata or model attributes when invoking the chain or initializing the handler(2). 📚 Sources:
Have another question? Just tag @inkeep. |
|
@inkeep how does langchain passes the details to langfuse |
|
When using Langchain with Langfuse, the Langfuse callback handler maps Langchain events (chains, LLMs, tools, retrievers, agents) to Langfuse observations. For LLM calls, Langchain passes details such as model name, prompts, responses, and usage (token counts) if available from the LLM provider, into the Langfuse generation observation(1). If you want to pass additional information (such as session_id, user_id, tags, or custom usage details), you can use the metadata parameter in your Langchain chain invocation. Here’s the format used for passing details: response = chain.invoke(
{"topic": "cats"},
config={
"callbacks": [langfuse_handler],
"metadata": {
"langfuse_session_id": "session_123",
"langfuse_user_id": "user_456",
"langfuse_tags": ["production", "humor-bot"],
"custom_field": "additional metadata"
}
}
)The callback handler will automatically extract and forward model name, prompt, response, and available usage details (like token counts) to Langfuse. If your LLM or chain does not provide usage details by default, you may need to update the generation observation with usage details after the call(1). 📚 Sources: Have another question? Just tag @inkeep. |
|
What is the key for usage details to be passed in metadata for cost tracking |
|
There is no documented key for passing usage details via the metadata field for cost tracking in Langfuse. Usage details for cost tracking must be provided using the usage_details parameter (or usage in some SDK versions) when updating or creating a generation observation, not as part of metadata(1)(2)(3). The keys in your usage_details dictionary must match exactly the usage type keys defined in your model definition on Langfuse for cost tracking to work(1). Example usage (Python low-level SDK): generation = trace.generation(
name="llm-generation",
usage_details={
"input": 50,
"output": 49,
"total": 99,
}
)Passing usage details in metadata will not trigger cost tracking. Always use usage_details (or usage) with the correct keys for cost calculation(1)(2)(3). 📚 Sources:
Have another question? Just tag @inkeep. |
|
Hi @kshitijbisen, can you see the usage tokens in Langfuse but the costs are still not calculated? |
@jannikmaierhoefer I wasn’t able to see token usage in Langfuse initially, since I didn’t know how to pass the token usage data to Langfuse via the langchain-langfuse callback handler.
I modified the _generate method of my custom model class and made it return token usage inside the llm_output parameter of the ChatResult object.
After doing this, Langfuse was able to pick up the usage correctly, based on the model definition I had already set up in the Langfuse UI with proper usage name mapping.