Integrating Langfuse with Spring AI
This guide shows how to integrate Langfuse with Spring AI using OpenTelemetry.
Spring AI: Spring-based framework for AI development with built-in OTel tracing for AI calls.
Langfuse: Open source LLM engineering platform for observability, evals and prompt management.
Please raise an Issue on GitHub if you face any issues with this integration.
Step 1: Enable OpenTelemetry in Spring AI
Add OpenTelemetry and Spring Observability Dependencies (Maven):
Make sure your project includes Spring Boot Actuator and the Micrometer tracing libraries for OpenTelemetry. Spring Boot Actuator is required to enable Micrometer’s observation and tracing features.
You’ll also need the Micrometer -> OpenTelemetry bridge and an OTLP exporter. For Maven, add the following to your pom.xml
(Gradle users can include equivalent coordinates in Gradle):
<!-- Spring Boot Actuator for observability support -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Micrometer Observation -> OpenTelemetry bridge -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-otel</artifactId>
</dependency>
<!-- OpenTelemetry OTLP exporter for traces -->
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-otlp</artifactId>
</dependency>
Enable Span Export and Configure Spring AI Observations (application.yml
):
With the above dependencies, Spring Boot will auto-configure tracing using OpenTelemetry as long as we provide the proper settings. We need to specify where to send the spans (the OTLP endpoint) and ensure Spring AI is set up to include the desired data in those spans. Create or update your application.yml
(or application.properties
) with the following configurations:
spring:
application:
name: spring-ai-llm-app # Service name for tracing (appears in Langfuse UI as the source service)
ai:
chat:
observations:
include-prompt: true # Include prompt content in tracing (disabled by default for privacy)
include-completion: true # Include completion content in tracing (disabled by default)
management:
tracing:
sampling:
probability: 1.0 # Sample 100% of requests for full tracing (adjust in production as needed)
observations:
annotations:
enabled: true # Enable @Observed (if you use observation annotations in code)
With these configurations and dependencies in place, your Spring Boot application is ready to produce OpenTelemetry traces. Spring AI’s internal calls (e.g. when you invoke a chat model or generate an embedding) will be recorded as spans.
Each span will carry attributes like gen_ai.operation.name
, gen_ai.system
(the provider, e.g. “openai”), model names, token usage, etc., and – since we enabled them – events for the prompt and response content
Step 2: Configure Langfuse
Now that your Spring AI application is emitting OpenTelemetry trace data, the next step is to direct that data to Langfuse.
Langfuse will act as the “backend” for OpenTelemetry in this setup – essentially replacing a typical Jaeger/Zipkin/OTel-Collector with Langfuse’s trace ingestion API.
Langfuse Setup
- Sign up for Langfuse Cloud or self-hosted Langfuse.
- Set the OTLP endpoint (e.g.
https://cloud.langfuse.com/api/public/otel
) and API keys.
Configure these via environment variables:
OTEL_EXPORTER_OTLP_ENDPOINT: set this to the Langfuse OTLP URL (e.g. https://cloud.langfuse.com/api/public/otel).
OTEL_EXPORTER_OTLP_HEADERS: set this to Authorization=Basic <base64 public:secret>.
You can find more on authentication via Basic Auth in the here.
Step 3: Run a Test AI Operation
Start your Spring Boot application. Trigger an AI operation that Spring AI handles – for example, call a service or controller that uses a ChatModel
to generate a completion, or an EmbeddingModel
to generate embeddings.
@Autowired
private ChatService chatService;
@EventListener(ApplicationReadyEvent.class)
public void testAiCall() {
String answer = chatService.chat("Hello, Spring AI!");
System.out.println("AI answered: " + answer);
}
Troubleshooting
No Traces:
- Check endpoint/credentials and confirm AI calls are invoked.
- Don’t mix multiple tracer implementations.
Missing Prompt/Completion:
- Ensure
include-prompt
andinclude-completion
aretrue
. - Sampling probability must be high enough.