Observability for AWS Bedrock with Opik

AWS Bedrock is a fully managed service that provides access to high-performing foundation models (FMs) from leading AI companies like AI21 Labs, Anthropic, Cohere, Meta, Mistral AI, Stability AI, and Amazon through a single API.

This guide explains how to integrate Opik with the Bedrock Python SDK, supporting both the Converse API and the Invoke Model API. By using the track_bedrock method provided by Opik, you can easily track and evaluate your Bedrock API calls within your Opik projects as Opik will automatically log the input prompt, model used, token usage, and response generated.

Account Setup

Comet provides a hosted version of the Opik platform, simply create an account and grab your API Key.

You can also run the Opik platform locally, see the installation guide for more information.

Getting Started

Installation

To start tracking your Bedrock LLM calls, you’ll need to have both the opik and boto3 packages. You can install them using pip:

$pip install opik boto3

Configuring Opik

Configure the Opik Python SDK for your deployment type. See the Python SDK Configuration guide for detailed instructions on:

  • CLI configuration: opik configure
  • Code configuration: opik.configure()
  • Self-hosted vs Cloud vs Enterprise setup
  • Configuration files and environment variables

Configuring Bedrock

In order to configure Bedrock, you will need to have:

You can request access to models in the AWS Bedrock console.

Once you have these, you can create your boto3 client:

1import boto3
2
3REGION = "us-east-1"
4MODEL_ID = "us.meta.llama3-2-3b-instruct-v1:0"
5
6bedrock_client = boto3.client(
7 service_name="bedrock-runtime",
8 region_name=REGION,
9 # aws_access_key_id=ACCESS_KEY,
10 # aws_secret_access_key=SECRET_KEY,
11 # aws_session_token=SESSION_TOKEN,
12)

Logging LLM calls

Opik supports both AWS Bedrock APIs: the Converse API (unified interface) and the Invoke Model API (model-specific formats). To log LLM calls to Opik, wrap your boto3 client with track_bedrock:

1import os
2from opik.integrations.bedrock import track_bedrock
3
4# Set project name via environment variable
5os.environ["OPIK_PROJECT_NAME"] = "bedrock-integration-demo"
6
7bedrock_client = track_bedrock(bedrock_client)

Despite the Invoke Model API using different input/output formats for each model provider, Opik automatically handles format detection and cost tracking for all supported models, providing unified observability across different model formats.

Converse API (Unified Interface)

The Converse API provides a unified interface across all supported models:

1import os
2import boto3
3from opik.integrations.bedrock import track_bedrock
4
5# Set project name via environment variable
6os.environ["OPIK_PROJECT_NAME"] = "bedrock-integration-demo"
7
8# Initialize and track the Bedrock client
9bedrock_client = boto3.client("bedrock-runtime", region_name="us-east-1")
10bedrock_client = track_bedrock(bedrock_client)
11
12PROMPT = "Why is it important to use a LLM Monitoring like CometML Opik tool that allows you to log traces and spans when working with LLM Models hosted on AWS Bedrock?"
13
14response = bedrock_client.converse(
15 modelId="us.meta.llama3-2-3b-instruct-v1:0",
16 messages=[{"role": "user", "content": [{"text": PROMPT}]}],
17 inferenceConfig={"temperature": 0.5, "maxTokens": 512, "topP": 0.9},
18)
19print("Response", response["output"]["message"]["content"][0]["text"])

Invoke Model API (Model-Specific Formats)

The Invoke Model API uses model-specific request and response formats. Here are examples for different providers:

1import json
2import os
3import boto3
4from opik.integrations.bedrock import track_bedrock
5
6# Set project name via environment variable
7os.environ["OPIK_PROJECT_NAME"] = "bedrock-integration-demo"
8
9# Initialize and track the Bedrock client
10bedrock_client = boto3.client("bedrock-runtime", region_name="us-east-1")
11bedrock_client = track_bedrock(bedrock_client)
12
13# Claude models use Anthropic's message format
14request_body = {
15 "anthropic_version": "bedrock-2023-05-31",
16 "max_tokens": 1000,
17 "temperature": 0.7,
18 "messages": [
19 {
20 "role": "user",
21 "content": "Explain the benefits of LLM observability"
22 }
23 ]
24}
25
26response = bedrock_client.invoke_model(
27 modelId="us.anthropic.claude-3-5-sonnet-20241022-v2:0",
28 body=json.dumps(request_body),
29 contentType="application/json",
30 accept="application/json"
31)
32
33response_body = json.loads(response["body"].read())
34print("Response:", response_body["content"][0]["text"])

Streaming API

Both Bedrock APIs support streaming responses, which is useful for real-time applications. Opik automatically tracks streaming calls for both APIs.

Converse Stream API

The converse_stream method provides streaming with the unified interface:

1import os
2import boto3
3from opik.integrations.bedrock import track_bedrock
4
5# Set project name via environment variable
6os.environ["OPIK_PROJECT_NAME"] = "bedrock-integration-demo"
7
8# Initialize and track the Bedrock client
9bedrock_client = boto3.client("bedrock-runtime", region_name="us-east-1")
10bedrock_client = track_bedrock(bedrock_client)
11
12def stream_conversation(
13 bedrock_client,
14 model_id,
15 messages,
16 system_prompts,
17 inference_config,
18):
19 """
20 Sends messages to a model and streams the response using Converse API.
21 Args:
22 bedrock_client: The Boto3 Bedrock runtime client.
23 model_id (str): The model ID to use.
24 messages (JSON) : The messages to send.
25 system_prompts (JSON) : The system prompts to send.
26 inference_config (JSON) : The inference configuration to use.
27
28 Returns:
29 Nothing.
30 """
31 response = bedrock_client.converse_stream(
32 modelId=model_id,
33 messages=messages,
34 system=system_prompts,
35 inferenceConfig=inference_config,
36 )
37
38 stream = response.get("stream")
39 if stream:
40 for event in stream:
41 if "messageStart" in event:
42 print(f"\nRole: {event['messageStart']['role']}")
43
44 if "contentBlockDelta" in event:
45 print(event["contentBlockDelta"]["delta"]["text"], end="")
46
47 if "messageStop" in event:
48 print(f"\nStop reason: {event['messageStop']['stopReason']}")
49
50 if "metadata" in event:
51 metadata = event["metadata"]
52 if "usage" in metadata:
53 print("\nToken usage")
54 print(f"Input tokens: {metadata['usage']['inputTokens']}")
55 print(f"Output tokens: {metadata['usage']['outputTokens']}")
56 print(f"Total tokens: {metadata['usage']['totalTokens']}")
57
58# Example usage
59system_prompt = """You are an app that creates playlists for a radio station
60 that plays rock and pop music. Only return song names and the artist."""
61
62input_text = "Create a list of 3 pop songs."
63messages = [{"role": "user", "content": [{"text": input_text}]}]
64system_prompts = [{"text": system_prompt}]
65inference_config = {"temperature": 0.5, "topP": 0.9}
66
67stream_conversation(
68 bedrock_client,
69 "us.meta.llama3-2-3b-instruct-v1:0",
70 messages,
71 system_prompts,
72 inference_config,
73)

Invoke Model Stream API

The invoke_model_with_response_stream method supports streaming with model-specific formats:

1import json
2import os
3import boto3
4from opik.integrations.bedrock import track_bedrock
5
6# Set project name via environment variable
7os.environ["OPIK_PROJECT_NAME"] = "bedrock-integration-demo"
8
9# Initialize and track the Bedrock client
10bedrock_client = boto3.client("bedrock-runtime", region_name="us-east-1")
11bedrock_client = track_bedrock(bedrock_client)
12
13# Claude streaming with Anthropic message format
14request_body = {
15 "anthropic_version": "bedrock-2023-05-31",
16 "max_tokens": 1000,
17 "temperature": 0.7,
18 "messages": [
19 {
20 "role": "user",
21 "content": "Tell me about the benefits of LLM observability"
22 }
23 ]
24}
25
26response = bedrock_client.invoke_model_with_response_stream(
27 modelId="us.anthropic.claude-3-5-sonnet-20241022-v2:0",
28 body=json.dumps(request_body),
29 contentType="application/json",
30 accept="application/json"
31)
32
33# Simple streaming - just print the events
34for event in response["body"]:
35 chunk = json.loads(event["chunk"]["bytes"])
36 print(chunk)

Advanced Usage

Using with the @track decorator

If you have multiple steps in your LLM pipeline, you can use the @track decorator to log the traces for each step. If Bedrock is called within one of these steps, the LLM call will be associated with that corresponding step:

1import boto3
2from opik import track
3from opik.integrations.bedrock import track_bedrock
4
5# Initialize and track the Bedrock client
6bedrock_client = boto3.client("bedrock-runtime", region_name="us-east-1")
7bedrock_client = track_bedrock(bedrock_client, project_name="bedrock-integration-demo")
8
9MODEL_ID = "us.anthropic.claude-3-5-sonnet-20241022-v2:0"
10
11@track
12def generate_story(prompt):
13 res = bedrock_client.converse(
14 modelId=MODEL_ID,
15 messages=[{"role": "user", "content": [{"text": prompt}]}],
16 inferenceConfig={"temperature": 0.7, "maxTokens": 1000}
17 )
18 return res["output"]["message"]["content"][0]["text"]
19
20@track
21def generate_topic():
22 prompt = "Generate a topic for a story about Opik."
23 res = bedrock_client.converse(
24 modelId=MODEL_ID,
25 messages=[{"role": "user", "content": [{"text": prompt}]}],
26 inferenceConfig={"temperature": 0.7, "maxTokens": 500}
27 )
28 return res["output"]["message"]["content"][0]["text"]
29
30@track
31def generate_opik_story():
32 topic = generate_topic()
33 story = generate_story(topic)
34 return story
35
36# Execute the multi-step pipeline
37generate_opik_story()

The trace can now be viewed in the UI with hierarchical spans showing the relationship between different steps:

Cost Tracking

The track_bedrock wrapper automatically tracks token usage and cost for all supported AWS Bedrock models, regardless of whether you use the Converse API or the Invoke Model API.

Despite the different input/output formats between the models accessed via the InvokeModel API (Anthropic, Amazon, Meta, Mistral), Opik automatically detects the response format and extracts unified cost and usage information for all models. So even if you can’t use the unified Converse API, you can still have the main tracing benefits by using our integration.

Cost information is automatically captured and displayed in the Opik UI, including:

  • Token usage details
  • Cost per request based on Bedrock pricing
  • Total trace cost

View the complete list of supported models and providers on the Supported Models page.