Skip to content

0012 Langfuse trace opened best-effort in generate()

Status

Accepted

Context

AnswerService::captureIds() correlates a turn with its ai_log entry and Langfuse trace, but nothing in the stack ever opened a trace automatically. LangFuseClient::getCurrentTrace() only reads a state key that createTrace() writes, and before this fix ai_answers never called createTrace() anywhere. trace_id was always null, and feedback had no trace to score against (issue 3610879).

Decision

AnswerService::generate() calls $this->langfuseTraceManager->ensureTrace() before running the agent, tagged with the agent id, conversation id, and turn number, so a trace exists by the time captureIds() runs afterward. The call is wrapped in try/catch: failure (for example, Langfuse being unreachable) is logged at info level and swallowed rather than failing the answer, consistent with every other Langfuse touchpoint in this class.

Consequences

  • This only fixes ai_answers's own trace-opening. The unrelated uncaught exception in langfuse_ai_agents_logging's LangFuseToolSpanSubscriber (see docs/getting-started.md) is a separate risk in a different module and is not addressed by this decision.
  • Opening the trace was necessary but not sufficient to fix feedback scoring: FeedbackLogger::scoreTrace() still cannot score it until LangFuseClient::createScore() exists upstream (see known gap).
  • If generation moves from AnswerService::generate() to AiAgentEntityWrapper as part of the agents/tools migration, the trace-open call must move with it.