Class SpringAiAnthropicHttpClient

java.lang.Object
org.springframework.ai.anthropic.http.okhttp.SpringAiAnthropicHttpClient
All Implemented Interfaces:
com.anthropic.core.http.HttpClient, AutoCloseable

public final class SpringAiAnthropicHttpClient extends Object implements com.anthropic.core.http.HttpClient
OkHttp-backed HttpClient for the Anthropic Java SDK, with Micrometer's OkHttpObservationInterceptor attached so each HTTP attempt produces an observation (span + metric + traceparent propagation). When a MeterRegistry is supplied, OkHttpConnectionPoolMetrics is also bound. We own this code because the SDK's stock AnthropicOkHttpClient.Builder doesn't expose an interceptor seam — see the SDK's Custom HTTP client guide for the integration pattern and anthropic-sdk-java #315 for the upstream fix being tracked.

Attribution: the body of this class is a Java port of OkHttpClient from anthropic-java-client-okhttp (Apache License 2.0, authored by Anthropic). Only the Micrometer wiring in SpringAiAnthropicHttpClient.Builder.build() is original to Spring AI.

Since:
2.0.0
Author:
Soby Chacko
  • Method Details

    • builder

      public static SpringAiAnthropicHttpClient.Builder builder()
    • execute

      public com.anthropic.core.http.HttpResponse execute(com.anthropic.core.http.HttpRequest request, com.anthropic.core.RequestOptions requestOptions)
      Specified by:
      execute in interface com.anthropic.core.http.HttpClient
    • executeAsync

      public CompletableFuture<com.anthropic.core.http.HttpResponse> executeAsync(com.anthropic.core.http.HttpRequest request, com.anthropic.core.RequestOptions requestOptions)
      Specified by:
      executeAsync in interface com.anthropic.core.http.HttpClient
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface com.anthropic.core.http.HttpClient