Skip to content

Langfuse Observability

Langfuse is an open-source LLM observability platform. The Nantian AI Gateway can ingest traces, generations, scores, and prompt usage data directly into your Langfuse instance so you can monitor model performance, debug production issues, and track costs from a single dashboard.

When Langfuse is configured on an AIService, the gateway records every AI request as a Langfuse trace. Each call to a model within that trace produces a generation record with token usage, latency, input content, and output content. You can also attach scores to traces for custom evaluation metrics. The gateway sends this data to the Langfuse ingestion API asynchronously.

Data ingestion is fire-and-forget from the gateway’s perspective. The HTTP call to Langfuse happens outside the request path, so it does not add latency to the client’s response. If the ingestion fails, the gateway logs the error but does not retry and does not affect the ongoing request.

When the Langfuse public key is left empty or the client is explicitly disablD (noop mode), all ingest methods become silent no-ops. No HTTP requests are made and no errors are raised.

Langfuse requires three configuration values:

FieldDescription
hostThe base URL of your Langfuse instance (e.g. https://langfuse.example.com).
publicKeyYour Langfuse project public key.
secretKeyYour Langfuse project secret key.

Authentication uses HTTP Basic Auth with the public key as the username and the secret key as the password, base64-encoded.

The HTTP client used for Langfuse communication has two timeout values:

TimeoutValueDescription
Request timeout10 secondsMaximum time for a full HTTP request-response cycle.
Connect timeout5 secondsMaximum time to establish a TCP connection to the Langfuse host.

These values are hardcoded and not configurable. Since ingestion is non-blocking, a timeout on an ingestion call only means that particular trace or generation is lost, not that the client request fails.

A trace represents the full lifecycle of a single AI request through the gateway. Each trace carries:

FieldDescription
traceIdUnique identifier for this trace.
userIdOptional user identifier scoping the trace to a specific end user.
sessionIdOptional session identifier for grouping multiple user interactions.
metadataArbitrary key-value pairs attached to the trace.
timestampISO 8601 timestamp of when the trace was created.

The trace is created when the request begins processing. All subsequent generations and scores reference the same trace ID, making it easy to reconstruct the full request chain in Langfuse.

A generation represents a single call to an AI model within a trace. Each generation records:

FieldDescription
traceIdThe parent trace this generation belongs to.
modelThe model name (e.g. gpt-4o, claude-3-sonnet).
usage.inputNumber of input (prompt) tokens consumed.
usage.outputNumber of output (completion) tokens produced.
usage.totalSum of input and output tokens.
latencyRound-trip time from request to response, in seconds.
inputThe full request payload sent to the model, as JSON.
outputThe full response payload from the model, as JSON.
metadataArbitrary key-value metadata attached to this generation.

If a request fans out through a fallback chain, each model attempt produces its own generation record under the same trace. This means you can see which model ultimately succeeded and how many attempts were made.

Scores attach custom numeric metrics to a trace. Use them to track evaluation results, quality signals, or any other quantitative measurement relevant to your AI traffic.

FieldDescription
traceIdThe trace this score applies to.
nameA label for the score (e.g. toxicity, relevance, latency_score).
valueA numeric value (float).
commentOptional free-text note about the score.

Scores are ingested independently of generations and can be added after the trace is complete, making them suitable for offline evaluation workflows that run asynchronously.

The gateway can fetch prompt templates from Langfuse Prompt Management (v2 API). A fetched prompt template includes:

FieldDescription
nameThe name of the prompt template in Langfuse.
promptThe compiled prompt text with variables resolved.
versionThe template version number.
configArbitrary configuration data attached to the template (as JSON).
variablesList of variable names used in the template.

You can request a specific version or let Langfuse return the latest. This integration lets you manage prompts in Langfuse’s UI and have the gateway pull them at runtime without redeploying.

Configure Langfuse on an AIService through the observability block:

apiVersion: gateway.nantian.dev/v1alpha1
kind: AIService
metadata:
name: observed-openai
namespace: nantian-demo
spec:
provider: openai
format: openai
model: gpt-4o
observability:
langfuse:
host: https://langfuse.example.com
publicKey: pk-lf-abc123
secretKey: sk-lf-xyz789

With this configuration, every request routed through the observer-openai service automatically ingests traces, generations, and any attached scores into the Langfuse instance at langfuse.example.com.

Ingestion is best-effort. The gateway does not buffer or retry failed ingestion calls. If your Langfuse instance is unreachable, traces and generations are silently dropped. This design keeps the gateway fast and resilient: observability degrades gracefully rather than causing request failures.

Monitor the gateway logs for ingestion errors to detect connectivity issues. If you see persistent failures, check that the Langfuse host is reachable from the data plane pods and that the credentials are still valid.