How to properly track external service costs in LangChain tools with LangFuse? #13514
Replies: 1 comment
-
|
Based on the available documentation, I can provide guidance on tracking external service costs in Langfuse, though the sources don't cover the specific LangChain tool integration pattern you're describing. Recommended Approach: Manual Generation with Usage and Cost DetailsFor tracking external service costs, you should create observations of type from langfuse import get_client
langfuse = get_client()
with langfuse.start_as_current_observation(
as_type="generation",
name="openai-style-generation",
model="gpt-4o"
) as generation:
# Simulate LLM call
# response = openai_client.chat.completions.create(...)
generation.update(
usage_details={
# usage (OpenAI-style schema)
"prompt_tokens": 10,
"completion_tokens": 25,
"total_tokens": 35,
"prompt_tokens_details": {
"cached_tokens": 5,
"audio_tokens": 2,
},
"completion_tokens_details": {
"reasoning_tokens": 15,
},
}
)For custom cost details, you can pass them manually: generation.update({
usageDetails: {
input: 10,
output: 5,
cache_read_input_tokens: 2,
some_other_token_count: 10,
total: 17, # optional, it is derived from input + cache_read_input_tokens + output
},
costDetails: {
# If you don't want the costs to be calculated based on model definitions, you can pass the costDetails manually.
input: 1,
output: 1,
cache_read_input_tokens: 0.5,
some_other_token_count: 1,
total: 3.5,
},
})Important Notes
Potential Issue: Context and Parent-Child RelationshipsWhen using the LangChain CallbackHandler alongside manual Langfuse SDK calls, there may be context isolation issues. The documentation notes that filtering or context management can break parent-child relationships in traces. Limitations of Available DocumentationThe sources don't specifically cover:
Check out these potentially useful sources for more context: Would you like me to provide a general answer based on my broader knowledge of how these integrations typically work? 📚 Sources:
Have another question? Just tag @inkeep. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Problem Statement
I'm trying to implement cost tracking for external services (Tavily web search, Pinecone vector DB, OpenAI embeddings, Cohere rerank) in LangFuse traces, but despite successful API calls and "success" logs, the cost data never appears in the LangFuse UI.
Current behavior:
costDetails: {},usage: {input: 0, output: 0}Setup Context
@tooldecorator) → LangGraphToolNode→ LangFuseCallbackHandlerWhat We've Tried
Approach 1: update_current_generation()
Result: Logs show success, but LangFuse API shows
costDetails: {}Approach 2: Manual generation creation
Result: No errors, but cost generation doesn't appear in trace
Approach 3: Context manager pattern
Result: Same issue - costs don't persist
Evidence from LangFuse API
Direct API query of trace shows:
{ "name": "web-search", "type": "TOOL", "cost": null, "costDetails": {}, // ❌ Should contain external costs "usage": {"input": 0, "output": 0, "total": 0} // ❌ Should show usage }Meanwhile, LLM observations work perfectly:
{ "name": "ChatAnthropic", "type": "GENERATION", "costDetails": {"total": 0.031646}, // ✅ Works fine "usage": {"input": 8084, "output": 67} }Questions for the Community
Best practices: What's the recommended approach for tracking external service costs in LangChain tools?
Timing issues: Should cost attribution happen during tool execution, or after completion?
Context management: How do we ensure cost updates target the correct observation when using LangGraph + LangChain tools + async operations?
Known limitations: Is this a known issue with the current LangFuse + LangChain integration?
Alternative approaches: Should we implement cost tracking at the LangGraph level instead of individual tools?
Why This Matters
This affects common LLM application patterns:
Additional Context
Any guidance on proper implementation patterns would be greatly appreciated! Happy to contribute documentation once we find the solution.
Environment:
Beta Was this translation helpful? Give feedback.
All reactions