Amazon Bedrock

Amazon Bedrock is a fully managed service that offers foundation models from leading AI providers through a single API. Respan gives you full observability over every Bedrock invocation, streamed response, and tool call — and gateway routing through the OpenAI-compatible Respan endpoint.

Create an account at platform.respan.ai and grab an API key. For gateway, also add credits or a provider key.

Run npx @respan/cli setup to set up with your coding agent.

Setup

1

Install packages

$pip install respan-ai opentelemetry-instrumentation-bedrock boto3
2

Set environment variables

$export AWS_ACCESS_KEY_ID="YOUR_AWS_ACCESS_KEY_ID"
$export AWS_SECRET_ACCESS_KEY="YOUR_AWS_SECRET_ACCESS_KEY"
$export AWS_REGION="us-east-1"
$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"

AWS credentials are used for Bedrock requests. RESPAN_API_KEY is used to export traces to Respan.

3

Initialize and run

1import json
2import boto3
3from respan import Respan
4from opentelemetry.instrumentation.bedrock import BedrockInstrumentor
5
6respan = Respan(instrumentations=[BedrockInstrumentor()])
7
8client = boto3.client("bedrock-runtime", region_name="us-east-1")
9
10response = client.invoke_model(
11 modelId="anthropic.claude-3-sonnet-20240229-v1:0",
12 body=json.dumps({
13 "anthropic_version": "bedrock-2023-05-31",
14 "max_tokens": 1024,
15 "messages": [{"role": "user", "content": "Say hello in three languages."}],
16 }),
17 contentType="application/json",
18)
19result = json.loads(response["body"].read())
20print(result["content"][0]["text"])
21respan.flush()
4

View your trace

Open the Traces page to see your auto-instrumented LLM spans.

Configuration

ParameterTypeDefaultDescription
api_keystr | NoneNoneFalls back to RESPAN_API_KEY env var.
base_urlstr | NoneNoneFalls back to RESPAN_BASE_URL env var.
instrumentationslist[]Plugin instrumentations to activate (e.g. BedrockInstrumentor()).
customer_identifierstr | NoneNoneDefault customer identifier for all spans.
metadatadict | NoneNoneDefault metadata attached to all spans.
environmentstr | NoneNoneEnvironment tag (e.g. "production").

Attributes

In Respan()

Set defaults at initialization — these apply to all spans.

1from respan import Respan
2from opentelemetry.instrumentation.bedrock import BedrockInstrumentor
3
4respan = Respan(
5 instrumentations=[BedrockInstrumentor()],
6 customer_identifier="user_123",
7 metadata={"service": "chat-api", "version": "1.0.0"},
8)

With propagate_attributes

Override per-request using a context scope.

1import json
2from respan import Respan, propagate_attributes
3from opentelemetry.instrumentation.bedrock import BedrockInstrumentor
4
5respan = Respan(instrumentations=[BedrockInstrumentor()])
6
7def handle_request(user_id: str, question: str):
8 with propagate_attributes(
9 customer_identifier=user_id,
10 thread_identifier="conv_abc_123",
11 metadata={"plan": "pro"},
12 ):
13 response = client.invoke_model(
14 modelId="anthropic.claude-3-sonnet-20240229-v1:0",
15 body=json.dumps({
16 "anthropic_version": "bedrock-2023-05-31",
17 "max_tokens": 1024,
18 "messages": [{"role": "user", "content": question}],
19 }),
20 contentType="application/json",
21 )
22 result = json.loads(response["body"].read())
23 print(result["content"][0]["text"])
AttributeTypeDescription
customer_identifierstrIdentifies the end user in Respan analytics.
thread_identifierstrGroups related messages into a conversation.
metadatadictCustom key-value pairs. Merged with default metadata.