Interface Backend

  • All Implemented Interfaces:

    
    public interface Backend
    
                        

    An interface that represents adaptations required to support different backend AI services. Different backends may require different credentials, request headers, request authorization, request signatures, response handlers and endpoint URLs. Implementations of this interface may define the information and handling appropriate for each backend service.

    When a backend is no longer needed, such as when the client it is associated with is closed and discarded, close must be called on the backend to ensure any resources it is using are released.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      abstract String baseUrl() Gets the backend service's base URL identifying the network address of the service that will be accessed by this backend.
      HttpRequest prepareRequest(HttpRequest request) Prepares the request for use by the backend service.
      HttpRequest authorizeRequest(HttpRequest request) Authorizes the given request and returns the new authorized request.
      HttpResponse prepareResponse(HttpResponse response) Prepares the response returned by the backend service.
      Boolean supportsMessageThreads() Returns whether the backend service takes the thread field of the Messages API.
      String openTelemetryProviderName() The gen_ai.provider.name attribute of the OpenTelemetry spans of a client that uses this backend: the platform that serves the requests.
      Optional<String> openTelemetryPlatform() The platform of this backend in the instrumentation scope of the OpenTelemetry spans of a client that uses it: bedrock gives the spans the scope com.anthropic.sdk.java.bedrock.
      abstract Unit close() Closes any streams and releases any resources used by this backend.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

    • Method Detail

      • baseUrl

         abstract String baseUrl()

        Gets the backend service's base URL identifying the network address of the service that will be accessed by this backend.

        The client builder adopts this as the client's base URL when the client is built; each request then carries its own base URL, which is what the transport uses.

      • prepareRequest

         HttpRequest prepareRequest(HttpRequest request)

        Prepares the request for use by the backend service. Changes may be made to the request to adapt it for backends other than the default Anthropic backend.

        Changes may be made to the headers, path segments, query parameters, and body content to suit the requirements of the backend where they differ from the default Anthropic backend. All changes that may affect the resolution of the full URL for the request must be made by this method.

        Changes specifically related to request authorization or signing should be performed by the implementation of authorizeRequest.

        If no changes are required, the given request can be returned unchanged. This is the behavior of the default implementation.

      • authorizeRequest

         HttpRequest authorizeRequest(HttpRequest request)

        Authorizes the given request and returns the new authorized request. The URL of the request will be resolved fully before this method is called.

        Changes will typically include the addition of authorization headers or signature headers. Other changes not directly related to authorization should be performed by prepareRequest.

        If the request already carries authorization headers (e.g., added by an interceptor or set at the request level), implementations must return the request unchanged rather than fail, so that interceptors and per-request options can manage credentials themselves.

        Where a backend does not require request authorization or other related changes to the request, the given HttpRequest may be returned unchanged. This is the behavior of the default implementation.

      • prepareResponse

         HttpResponse prepareResponse(HttpResponse response)

        Prepares the response returned by the backend service. This allows changes to be made to the response to adapt it for backend services other than the default Anthropic backend service. The adaptations make the response appear to come from that default Anthropic backend.

        If no changes are required, the given response can be returned unchanged. This is the behavior of the default implementation.

      • supportsMessageThreads

         Boolean supportsMessageThreads()

        Returns whether the backend service takes the thread field of the Messages API.

        The default implementation returns true, so the service answers for itself. Override it to return false for a service that is known to take no message threads.

      • openTelemetryProviderName

         String openTelemetryProviderName()

        The gen_ai.provider.name attribute of the OpenTelemetry spans of a client that uses this backend: the platform that serves the requests. aws.bedrock on Amazon Bedrock, including its Mantle endpoint, gcp.vertex_ai on Vertex AI, and anthropic for every other backend, because Anthropic runs the service each of them calls.

      • openTelemetryPlatform

         Optional<String> openTelemetryPlatform()

        The platform of this backend in the instrumentation scope of the OpenTelemetry spans of a client that uses it: bedrock gives the spans the scope com.anthropic.sdk.java.bedrock. Empty for a backend with no platform of its own, whose spans have the scope com.anthropic.sdk.java.

      • close

         abstract Unit close()

        Closes any streams and releases any resources used by this backend. For example, an implementation might use this to shut down a thread pool that will no longer be needed.