Skip to content

Investigate: propagate Langfuse session/user to LiteLLM's agent-completion traces #303

Description

@sheepdestroyer

Context

When the triage router propagates session_id and user_id via Langfuse's propagate_attributes(), only the litellm-proxy trace (created by LiteLLM's proxy layer) inherits these attributes. The agent-completion trace (created by LiteLLM's internal LLM call instrumentation) does not — it appears as a separate trace with sessionId: null.

This was observed during PR #302 verification:

  • Session-bearing request → litellm-proxy trace gets sessionId=e2e-verify-*, userId=verify-script
  • Same request → agent-completion trace gets sessionId=None, userId=None

Why this matters

The agent-completion trace is where actual token usage, latency, and model metadata live. Without session context there, session-scoped Langfuse dashboards, cost-tracking, and user-level analytics are incomplete — you can see the proxy hop but not the actual model call.

Root cause

LiteLLM creates agent-completion as a separate trace (not an observation within our parent trace). Langfuse's propagate_attributes sets context on the current OpenTelemetry span; LiteLLM's internal agent-completion span lives in a different trace tree and doesn't inherit.

What to investigate

  1. Use cases: Which Langfuse features break without session on agent-completion? (session-scoped dashboards, per-user cost reports, trace-linking in UI)
  2. Benefits: What would full session propagation across both litellm-proxy AND agent-completion enable?
  3. Implementation options (ranked by feasibility):
    • A) LiteLLM config: Does LiteLLM's langfuse config section support a session_id parameter that would be forwarded to backend traces?
    • B) Trace header forwarding: Pass X-Langfuse-Trace-Id + X-Langfuse-Session-Id headers through LiteLLM to the backend provider — does LiteLLM respect these?
    • C) LiteLLM callback: Can a LiteLLM success_callback or failure_callback patch the agent-completion trace's session after creation?
    • D) OpenTelemetry context propagation: Wire our parent trace's OTel context into LiteLLM's trace provider so child traces share context
  4. Sibling trace linking: If full propagation isn't feasible, can we at least link agent-completion traces to our session-bearing traces so Langfuse UI shows them together?

Affected components

  • router/main.pychat_completions (session_id/user_id extraction + propagate_attributes)
  • LiteLLM's Langfuse integration (external, version-dependent)
  • Langfuse UI session views, cost dashboards

Requirements

Metadata

Metadata

Assignees

No one assigned

    Labels

    code-qualityCode quality improvementenhancementNew feature or requestmaintenanceMaintenance and tech debt

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions