Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Fix: send input and output token usage to Langfuse (#13294)

Br1and46bbd30f7f6
diff --git a/api/db/services/dialog_service.py b/api/db/services/dialog_service.py
index 6f981efb5e..07dcd14b5e 100644
--- a/api/db/services/dialog_service.py
+++ b/api/db/services/dialog_service.py
@@ -809,7 +809,14 @@ async def async_chat(dialog, messages, stream=True, **kwargs):
if langfuse_tracer and "langfuse_generation" in locals():
langfuse_output = "\n" + re.sub(r"^.*?(### Query:.*)", r"\1", prompt, flags=re.DOTALL)
langfuse_output = {"time_elapsed:": re.sub(r"\n", " \n", langfuse_output), "created_at": time.time()}
- langfuse_generation.update(output=langfuse_output)
+ langfuse_generation.update(
+ output=langfuse_output,
+ usage_details={
+ "input": used_token_count,
+ "output": tk_num,
+ "total": used_token_count + tk_num,
+ },
+ )
langfuse_generation.end()
return {"answer": think + answer, "reference": refs, "prompt": re.sub(r"\n", " \n", prompt), "created_at": time.time()}