Skip to content

Trace agent runs and LLM calls in Langfuse - #10

Merged
MomoSolaris8 merged 4 commits into
mainfrom
langfuse-tracing
Oct 31, 2026
Merged

MomoSolaris8 merged 4 commits into
mainfrom
langfuse-tracing

Conversation

@MomoSolaris8

@MomoSolaris8 MomoSolaris8 commented Oct 30, 2026 •

Copy link
Copy Markdown
Owner

Until now each request sent one flat Langfuse trace, so we couldn't see which agent node was slow, what prompt the model actually got, or how many tokens each LLM call used. This PR turns every request into a trace tree.

What changed

  • Removed our own integrations/langfuse_client.py and switched to the SDK's shared get_client().
    • Spans are no longer flushed after every request.
    • The client is shut down once, in the app lifespan, after the engine is disposed.
  • answer_with_agent is the root span.
    • It carries user_id, plus session_id set from the thread id, so a conversation is grouped under one session in Langfuse.
    • record_langfuse_trace was renamed update_langfuse_trace and now uses update_current_span.
  • Each of the 8 agent nodes is a child span of the root.
  • complete_chat is recorded as a generation named llm:<task>. It records:
    • the model, input messages, temperature and output;
    • input and output token usage;
    • provider, reasoning tokens and cached input tokens, as metadata.
  • A failed LLM call is marked level=ERROR, with the error message.
  • Every span sets capture_input=False and capture_output=False, so only the fields we pass explicitly are sent.

Testing

  • Tracing is disabled for the test suite (LANGFUSE_TRACING_ENABLED=false in tests/conftest.py).
  • A new langfuse_spans fixture captures spans in memory with InMemorySpanExporter. It turns tracing on only for the tests that ask for it.
  • test_agent_tracing.py checks two things:
    • The root span has no parent and carries the user and session.
    • Every node span is a child of the root.
  • test_llm_tracing.py checks two things:
    • A generation's model, usage and output.
    • A failing call produces an ERROR-level generation.
  • I ran one question locally against Langfuse Cloud. The UI showed the full tree:
    • answer_with_agent, then the node spans, then llm:chat (qwen-plus);
    • the user and the session link.

Deployment

No config change is needed. The Container App already has LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY (both from secrets), LANGFUSE_BASE_URL and LANGFUSE_TRACING_ENVIRONMENT.

After the deploy, ask one question in production and check that a trace shows up in Langfuse under the azure-dev environment.

Follow-ups (not in this PR)

The first real trace surfaced these:

  • An English question was answered in German.
    • detect_response_language counts marker words, and a tie defaults to de. "Does my car insurance cover glass damage?" contains no English marker, so it is detected as de.
    • core/answer.py doesn't tell the model which language to answer in, and the prompt scaffolding is German.
    • The online eval uses the same detector, so its language check is affected too.
  • Retrieval returned irrelevant chunks. For a car insurance question it returned Wohngebäude and Hausrat chunks. The eval work will measure this.
  • Langfuse shows "no cost" for Qwen models. They need custom model prices in Langfuse.
  • Embedding calls in retrieve aren't traced.
  • PII masking. User questions and retrieved documents are sent to Langfuse as they are.

🤖 Generated with Claude Code

@vercel

vercel Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
support-agent-one-coyl Ready Ready Preview Oct 30, 2026 3:29pm UTC

@MomoSolaris8
MomoSolaris8 marked this pull request as ready for review October 31, 2026 21:02
@MomoSolaris8
MomoSolaris8 merged commit 9d54b4f into main Oct 31, 2026
6 checks passed
@MomoSolaris8
MomoSolaris8 deleted the langfuse-tracing branch October 31, 2026 21:02

This branch was successfully deployed

1 active deployment
Preview — 0f8a2ff6 Deployed Oct 30, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant