跳到正文
DEV Community· Span Observer·· 6 小時前AI 評分61

Langfuse + OpenLLMetry:以 OpenTelemetry 實現 LLM 應用可觀測性與追蹤

Langfuse + OpenLLMetry: LLM observability and tracing for your LLM applications with OpenTelemetry

AI 導讀

Langfuse 與 OpenLLMetry 可透過 OpenTelemetry 追蹤 LLM 應用,並由 Collector 將同一批 spans 分送至 Langfuse 和通用可觀測性後端。

正文

Langfuse and OpenLLMetry are two open source projects that answer the same question: what is really happening inside an LLM application? Which prompt was sent, which documents were retrieved, how many tokens were consumed, which step took three seconds? Without the traces a Langfuse/OpenLLMetry setup provides, a RAG application or an agent remains a black box, and every odd answer becomes a blind investigation. This is what LLM observability is about, and Langfuse is one of the most used open source tools for it.

Both projects have a feature that directly interests SRE teams: they speak OpenTelemetry. OpenLLMetry is a set of instrumentations that produce OpenTelemetry spans for calls to LLMs, vector databases and frameworks. Langfuse is an LLM engineering platform (traces, prompt management, evaluations, datasets) that can receive these traces over OTLP.

This article shows how to combine them to trace an LLM application, and how to send the same traces to a general-purpose observability backend. It complements our pillar article on AI observability.

Langfuse + OpenLLMetry: who does what?

OpenLLMetry: the instrumentation

OpenLLMetry, maintained by Traceloop under the Apache 2.0 licence, provides OpenTelemetry instrumentations for many model providers (including Ollama and OpenAI-compatible APIs), vector databases (Chroma, Qdrant, Milvus…) and frameworks (LangChain, LangGraph, LlamaIndex, CrewAI…). The traceloop-sdk SDK enables them all at once; each instrumentation also exists as a separate package if you prefer to configure OpenTelemetry yourself.

The spans carry attributes inspired by the OpenTelemetry semantic conventions for generative AI (gen_ai.*: requested model, input and output tokens, etc.). These conventions are still being stabilised: exact names may vary between versions.

Langfuse: LLM-specific analysis

Langfuse stores traces and presents them from an LLM angle: the call tree, full prompts and responses, estimated costs, user sessions, evaluation scores and user feedback. It can be self-hosted (the documentation describes Docker Compose and Kubernetes deployments) or used as a cloud service. Its OpenTelemetry entry point is /api/public/otel, using OTLP over HTTP (JSON or protobuf); according to the documentation, gRPC is not supported at present.

What about the observability backend?

Langfuse is excellent for the team building the application. The operations team, however, wants to see LLM calls in the context of everything else: calling service, database, infrastructure, deployments. That is the role of a general-purpose OTLP-compatible backend such as Dynatrace. OpenTelemetry lets you feed both from a single instrumentation.

Architecture: one instrumentation, two destinations

  1. The application is instrumented with OpenLLMetry.
  2. Spans are sent over OTLP to an OpenTelemetry Collector.
  3. The Collector sends them both to Langfuse (LLM analysis) and to the observability backend (end-to-end view).

The Collector centralises secrets, lets you filter or mask sensitive attributes (prompts may contain personal data) and avoids coupling the application to a vendor.

In practice: tracing an LLM application with OpenTelemetry

Step 1 — Instrument the application

pip install traceloop-sdk
from traceloop.sdk import Traceloop
from traceloop.sdk.decorators import workflow

Traceloop.init(app_name="assistant-runbooks")

@workflow(name="repondre_question")
def repondre(question: str) -> str:
    # calls to the LLM, the retriever, etc.: instrumented automatically
    ...

The decorators (workflow, task, agent, tool) group the automatic spans under named steps. During development, the disable_batch=True option sends spans immediately.

Step 2 — Point to the Collector

The SDK reads its destination from environment variables. For a local Collector using OTLP/HTTP:

export TRACELOOP_BASE_URL="http://otel-collector:4318"
# do not collect the content of prompts and responses:
export TRACELOOP_TRACE_CONTENT=false

Check the exact behaviour of this variable and of TRACELOOP_HEADERS, as well as the privacy options, in the OpenLLMetry documentation.

Step 3 — Configure the Collector for two destinations

receivers:
  otlp:
    protocols:
      http:
        endpoint: 0.0.0.0:4318

processors:
  batch: {}

exporters:
  otlphttp/langfuse:
    endpoint: "https://langfuse.interne/api/public/otel"
    headers:
      Authorization: "Basic ${env:LANGFUSE_AUTH}"
  otlphttp/dynatrace:
    endpoint: "https://${env:DT_ENV_ID}.live.dynatrace.com/api/v2/otlp"
    headers:
      Authorization: "Api-Token ${env:DT_API_TOKEN}"

service:
  pipelines:
    traces:
      receivers: [otlp]
      processors: [batch]
      exporters: [otlphttp/langfuse, otlphttp/dynatrace]

LANGFUSE_AUTH is the base64 encoding of the Langfuse project’s public_key:secret_key. The Dynatrace token must have the permission to ingest OpenTelemetry traces. Langfuse may require additional headers depending on its version: the Langfuse documentation is the authority.

Step 4 — Check in both tools

In Langfuse, each request appears as a trace with its generations, its estimated cost and the prompt content. In the observability backend, the same spans slot into the distributed trace of the calling service. To link the user click, the service and the LLM call cleanly, see end-to-end OpenTelemetry.

Step 5 — Define the indicators to track

  • latency per step (retrieval, generation, tools) and in total;
  • input and output tokens per request, per model and per feature;
  • error and timeout rate for model calls;
  • quality scores (evaluations, user feedback) tracked in Langfuse.

For agents that call tools via MCP, the same approach applies: see monitoring MCP agents with OpenTelemetry.

LLM observability: what does a well-instrumented trace reveal?

A few diagnoses become possible as soon as traces are in place:

  • The answer is wrong: the trace shows whether the retriever brought back the right documents. If it did, the problem lies in the prompt or the model; if not, in the indexing.
  • Latency has doubled: you see straight away whether it is generation (longer prompt, slower model), an external tool or the vector database.
  • Costs are drifting: the number of tokens per request reveals a growing context, an agent loop or a conversation history that is never truncated.
  • An agent is going round in circles: repeated identical tool calls stand out in the span tree.

Best practices

  • Decide explicitly whether prompt and response content is collected; mask it at the Collector if necessary.
  • Set service.name and the environment to tell production and tests apart.
  • Add a (pseudonymised) session or user ID to group traces.
  • Sample high-volume traces, but keep the errors.
  • Pin instrumentation versions: the gen_ai.* attributes are still evolving.
  • Monitor the Collector itself: if it goes down, you lose visibility in both destinations.

Frequently asked questions

Should you use the Langfuse SDK or OpenLLMetry?

The Langfuse SDK is the most direct route if Langfuse is the only destination. OpenLLMetry, or any standard OpenTelemetry instrumentation, is preferable if the traces must also feed other backends.

Does Langfuse replace an observability tool?

No. Langfuse focuses on the LLM application (prompts, costs, evaluations). Infrastructure, other services and SLOs remain the job of a general-purpose backend.

Can you trace a locally served model?

Yes. OpenLLMetry instruments, among others, the Ollama client and OpenAI-compatible APIs; on the server side, see monitoring Ollama for inference metrics.

Conclusion

Langfuse and OpenLLMetry make a coherent pair: standard instrumentation on one side, specialised analysis on the other, linked by OpenTelemetry. By adding a Collector in the middle, the same trace serves both the team improving the prompts and the team guaranteeing the reliability of the service.

span.observer is an independent community blog, not affiliated with Dynatrace LLC or with the projects mentioned. Trademarks belong to their respective owners. Tools change quickly: check commands and options in their official documentation.


Originally published on span.observer. French version: https://span.observer/fr/langfuse-openllmetry-observabilite-llm/.

來源:DEV Community · dev.to