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. 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 = 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

In order to log the LLM calls to Opik, you will need to create the wrap the boto3 client with track_bedrock. When making calls with that wrapped client, all calls will be logged to Opik:

1from opik.integrations.bedrock import track_bedrock
2
3bedrock_client = track_bedrock(bedrock, project_name="bedrock-integration-demo")
4
5PROMPT = "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?"
6
7response = bedrock_client.converse(
8 modelId=MODEL_ID,
9 messages=[{"role": "user", "content": [{"text": PROMPT}]}],
10 inferenceConfig={"temperature": 0.5, "maxTokens": 512, "topP": 0.9},
11)
12print("Response", response["output"]["message"]["content"][0]["text"])

Streaming API

Bedrock supports streaming responses, which is useful for real-time applications. Here’s how to use streaming with Opik:

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

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:

1from opik import track
2from opik.integrations.bedrock import track_bedrock
3
4os.environ["OPIK_PROJECT_NAME"] = "bedrock-integration-demo"
5bedrock_client = track_bedrock(bedrock)
6
7@track
8def generate_story(prompt):
9 res = bedrock_client.converse(
10 modelId=MODEL_ID, messages=[{"role": "user", "content": [{"text": prompt}]}]
11 )
12 return res["output"]["message"]["content"][0]["text"]
13
14@track
15def generate_topic():
16 prompt = "Generate a topic for a story about Opik."
17 res = bedrock_client.converse(
18 modelId=MODEL_ID, messages=[{"role": "user", "content": [{"text": prompt}]}]
19 )
20 return res["output"]["message"]["content"][0]["text"]
21
22@track
23def generate_opik_story():
24 topic = generate_topic()
25 story = generate_story(topic)
26 return story
27
28# Execute the multi-step pipeline
29generate_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.

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.