Skip to main content
GitHub

Amazon Bedrock

Auto-instrument Amazon Bedrock for multi-model inference.

Risicare automatically instruments Amazon Bedrock via the boto3 SDK.

Bedrock Runtime Only

Only bedrock-runtime operations are traced. Other AWS services (S3, DynamoDB, etc.) are not affected.

Installation

pip install risicare boto3

Auto-Instrumentation

import risicare
import boto3
import json
 
risicare.init()
 
client = boto3.client("bedrock-runtime", region_name="us-east-1")
 
# Automatically traced
response = client.invoke_model(
    modelId="anthropic.claude-3-sonnet-20240229-v1:0",
    body=json.dumps({
        "anthropic_version": "bedrock-2023-05-31",
        "max_tokens": 1024,
        "messages": [{"role": "user", "content": "Hello!"}]
    })
)

Captured Attributes

AttributeDescription
gen_ai.systembedrock
gen_ai.request.modelFull model ID (shortened)
gen_ai.request.bedrock.operationConverse, ConverseStream, InvokeModel, or InvokeModelWithResponseStream
gen_ai.usage.prompt_tokensInput tokens (from inputTokens)
gen_ai.usage.completion_tokensOutput tokens (from outputTokens)
gen_ai.usage.total_tokensTotal tokens
gen_ai.response.stop_reasonStop reason
gen_ai.completion.tool_usesNumber of tool use blocks
gen_ai.completion.tool_use.{j}.nameName of each tool use
gen_ai.response.bedrock.latency_msBedrock-specific response latency
gen_ai.latency_msRequest latency in milliseconds
gen_ai.response.streamWhether the response was streamed

Converse API

The newer Converse API is also supported:

response = client.converse(
    modelId="anthropic.claude-3-sonnet-20240229-v1:0",
    messages=[
        {"role": "user", "content": [{"text": "Hello!"}]}
    ]
)

Streaming

response = client.invoke_model_with_response_stream(
    modelId="anthropic.claude-3-sonnet-20240229-v1:0",
    body=json.dumps({
        "anthropic_version": "bedrock-2023-05-31",
        "max_tokens": 1024,
        "messages": [{"role": "user", "content": "Write a story"}]
    })
)
 
for event in response["body"]:
    chunk = json.loads(event["chunk"]["bytes"])
    if chunk["type"] == "content_block_delta":
        print(chunk["delta"]["text"], end="")

Supported Models

ProviderModel ID
Anthropicanthropic.claude-3-5-sonnet-20241022-v2:0
Anthropicanthropic.claude-3-sonnet-20240229-v1:0
Anthropicanthropic.claude-3-haiku-20240307-v1:0
Metameta.llama3-70b-instruct-v1:0
Metameta.llama3-8b-instruct-v1:0
Mistralmistral.mistral-large-2407-v1:0
Amazonamazon.titan-text-express-v1
Coherecohere.command-r-plus-v1:0

Model ID Shortening

Risicare automatically shortens verbose Bedrock model IDs:

anthropic.claude-3-sonnet-20240229-v1:0 → claude-3-sonnet
meta.llama3-70b-instruct-v1:0 → llama3-70b

Next Steps