AWS Bedrock (tracing)

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 Bedrock invocations, streamed responses, and tool calls, plus separate gateway routing through the OpenAI-compatible Respan endpoint.

Create an account at platform.respan.ai and grab an API key.

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

See Amazon Bedrock gateway setup to route Bedrock model calls through the Respan gateway.

Setup

1

Install packages

$pip install respan-ai respan-instrumentation-aws-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 direct 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 respan_instrumentation_aws_bedrock import AWSBedrockInstrumentor
5
6respan = Respan(instrumentations=[AWSBedrockInstrumentor()])
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"])
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, such as AWSBedrockInstrumentor() or new AWSBedrockInstrumentor().
sdkModuleobject | undefinedundefinedTypeScript only. Optional AWS SDK module instance for runtimes that resolve multiple copies.
customer_identifierstr | NoneNoneDefault customer identifier for all spans.
metadatadict | NoneNoneDefault metadata attached to all spans.
environmentstr | NoneNoneEnvironment tag, such as "production".

Supported calls

SDK callTraced
InvokeModelModel invocation spans
InvokeModelWithResponseStreamStreaming invocation spans after the stream is consumed
ConverseChat spans with messages, output, usage, and tool definitions
ConverseStreamStreaming chat spans after the stream is consumed

Attributes

In Respan()

Set defaults at initialization. These apply to all spans.

1from respan import Respan
2from respan_instrumentation_aws_bedrock import AWSBedrockInstrumentor
3
4respan = Respan(
5 instrumentations=[AWSBedrockInstrumentor()],
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 respan_instrumentation_aws_bedrock import AWSBedrockInstrumentor
4
5respan = Respan(instrumentations=[AWSBedrockInstrumentor()])
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.