AWS Bedrock Java Integration with Future AGI Tracing

Trace AWS Bedrock model invocations in Java with TracedBedrockRuntimeClient. Supports both InvokeModel (raw JSON) and Converse (typed API).

📝
TL;DR
  • TracedBedrockRuntimeClient wraps BedrockRuntimeClient from the AWS SDK
  • Two APIs: invokeModel() (raw JSON body) and converse() (typed messages)
  • Provider auto-detected from model ID prefix (anthropic., amazon., meta., etc.)
  • Parses provider-specific JSON formats for Claude, Titan, Llama, and others

Prerequisites

Complete the Java SDK setup first. Configure AWS credentials, a region, and access to the model you plan to invoke.

Installation

These JitPack coordinates use a mutable main-SNAPSHOT. For reproducible deployments, select an immutable wrapper version compatible with your AWS SDK; see the Java SDK installation guidance.

<dependency>
    <groupId>com.github.future-agi.traceAI</groupId>
    <artifactId>traceai-java-bedrock</artifactId>
    <version>main-SNAPSHOT</version>
</dependency>
implementation 'com.github.future-agi.traceAI:traceai-java-bedrock:main-SNAPSHOT'

You also need the AWS Bedrock Runtime SDK. Use 2.25.67 to match the wrapper’s dependency and include the Converse API (AWS versioned API source):

<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>bedrockruntime</artifactId>
    <version>2.25.67</version>
</dependency>
implementation 'software.amazon.awssdk:bedrockruntime:2.25.67'

Wrap the client

import ai.traceai.TraceAI;
import ai.traceai.bedrock.TracedBedrockRuntimeClient;
import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient;

TraceAI.initFromEnvironment();

BedrockRuntimeClient client = BedrockRuntimeClient.create();
TracedBedrockRuntimeClient traced = new TracedBedrockRuntimeClient(client);

InvokeModel (raw JSON)

The invokeModel API takes a raw JSON body. The wrapper parses the JSON to extract inputs and outputs based on the provider format.

import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.services.bedrockruntime.model.*;

// Claude Messages format
String requestBody = """
    {
        "anthropic_version": "bedrock-2023-05-31",
        "messages": [{"role": "user", "content": "What is the capital of France?"}],
        "max_tokens": 1024
    }
    """;

InvokeModelResponse response = traced.invokeModel(InvokeModelRequest.builder()
    .modelId("anthropic.claude-3-haiku-20240307-v1:0")
    .body(SdkBytes.fromUtf8String(requestBody))
    .build());

String responseJson = response.body().asUtf8String();
System.out.println(responseJson);

Span created: “Bedrock Invoke Model” with kind LLM

The wrapper identifies the provider from the model ID prefix and extracts supported fields from the JSON body.


Converse (typed API)

The converse API uses typed request/response objects instead of raw JSON. This is the recommended API for new integrations.

import software.amazon.awssdk.services.bedrockruntime.model.*;
import java.util.List;

ConverseResponse response = traced.converse(ConverseRequest.builder()
    .modelId("anthropic.claude-3-haiku-20240307-v1:0")
    .messages(List.of(
        Message.builder()
            .role(ConversationRole.USER)
            .content(List.of(ContentBlock.fromText("What is the capital of France?")))
            .build()
    ))
    .inferenceConfig(InferenceConfiguration.builder()
        .maxTokens(1024)
        .temperature(0.7f)
        .topP(0.9f)
        .build())
    .build());

String text = response.output().message().content().get(0).text();
System.out.println(text);

Span created: “Bedrock Converse” with kind LLM


Inspect the trace

Open your project in Future AGI to inspect the recorded call. The wrapper records raw request and response data, so consider the sensitivity of the content you send before enabling tracing. For invokeModel, field extraction depends on the provider and JSON format.


Accessing the original client

BedrockRuntimeClient original = traced.unwrap();
Was this page helpful?

Questions & Discussion