Skip to main content
OpenInference provides instrumentation for Spring AI applications using Spring’s Micrometer Observation API. This enables you to trace LLM calls, model parameters, token usage, and more using OpenTelemetry.

Installation

Gradle

Add the following to your build.gradle:

Maven

Add the following to your pom.xml:

Requirements

  • Java 17 or higher
  • Spring AI 1.0.0 or higher
  • OpenTelemetry Java 1.49.0 or higher
  • Micrometer Observation 1.15.0 or higher

Quick Start

Basic Setup

OpenTelemetry Setup

Basic Setup with Phoenix

Configuration

Custom Trace Configuration

Control what information is captured in traces:

Chat Options

Configure model parameters:

Spring Boot Integration

Application Configuration

Service Example

Tool Calling (Function Calling)

Spring AI supports function calling with automatic tracing:

Captured Trace Data

The instrumentation automatically captures:
  • LLM Model Information: Model name, provider
  • Input Messages: User prompts, system messages, conversation history
  • Output Messages: Model responses, assistant messages
  • Invocation Parameters: Temperature, max tokens, top_p, etc.
  • Token Usage: Prompt tokens, completion tokens, total tokens
  • Tool Calls: Function names, arguments, and responses
  • Message Roles: System, user, assistant, tool
  • Timing Information: Request latency and duration
  • Error Information: Exceptions and error messages

Multi-turn Conversations

Trace complete conversations with context:

Viewing Traces

Using Phoenix

  1. Start Phoenix locally:
  2. Run your instrumented application
  3. View traces at http://localhost:6006

Using Other Backends

OpenInference instrumentation works with any OpenTelemetry-compatible backend:
  • Jaeger: Change the OTLP endpoint to your Jaeger instance
  • Zipkin: Use the Zipkin exporter
  • Cloud Providers: AWS X-Ray, Google Cloud Trace, Azure Monitor

Best Practices

  1. Singleton Registry: Create a single ObservationRegistry instance and reuse it across your application
  2. Spring Boot Integration: Use Spring’s dependency injection for ObservationRegistry
  3. Set Service Name: Always set a meaningful service.name in your OpenTelemetry resource
  4. Use Batch Processing: Use BatchSpanProcessor for better performance
  5. Handle Secrets: Never log API keys in traces
  6. Graceful Shutdown: Flush spans before application shutdown

Troubleshooting

No traces appearing

  • Verify the ObservationRegistry is properly configured with SpringAIInstrumentor
  • Ensure the registry is passed to your ChatModel via .observationRegistry()
  • Check that your OTLP endpoint is accessible
  • Enable debug logging for Spring AI observations

Missing token counts

Token usage is only available when the model provider returns usage metadata in the response.

Tool calls not traced

Ensure you’re using Spring AI 1.0.0 or higher, which includes observation support for tool calls.

Complete Example

Here’s a complete example with tool calling and conversation context:

Resources