# Initialize and track the Bedrock client
bedrock_client = boto3.client("bedrock-runtime", region_name="us-east-1")
bedrock_client = track_bedrock(bedrock_client)

PROMPT = "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?"

response = bedrock_client.converse(
    modelId="us.meta.llama3-2-3b-instruct-v1:0",
    messages=[{"role": "user", "content": [{"text": PROMPT}]}],
    inferenceConfig={"temperature": 0.5, "maxTokens": 512, "topP": 0.9},
)
print("Response", response["output"]["message"]["content"][0]["text"])
```

<Frame>
  <img src="https://files.buildwithfern.com/https://opik.docs.buildwithfern.com/docs/opik/2026-01-30T17:33:07.444Z/img/tracing/bedrock/bedrock_single_trace.png" />
</Frame>

### Invoke Model API (Model-Specific Formats)

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

<Tabs>
  <Tab value="Anthropic Claude" title="Anthropic Claude">
    ```python
    import json
    import os
    import boto3
    from opik.integrations.bedrock import track_bedrock

    # Set project name via environment variable
    os.environ["OPIK_PROJECT_NAME"] = "bedrock-integration-demo"

    # Initialize and track the Bedrock client
    bedrock_client = boto3.client("bedrock-runtime", region_name="us-east-1")
    bedrock_client = track_bedrock(bedrock_client)

    # Claude models use Anthropic's message format
    request_body = {
        "anthropic_version": "bedrock-2023-05-31",
        "max_tokens": 1000,
        "temperature": 0.7,
        "messages": [
            {
                "role": "user",
                "content": "Explain the benefits of LLM observability"
            }
        ]
    }

    response = bedrock_client.invoke_model(
        modelId="us.anthropic.claude-3-5-sonnet-20241022-v2:0",
        body=json.dumps(request_body),
        contentType="application/json",
        accept="application/json"
    )

    response_body = json.loads(response["body"].read())
    print("Response:", response_body["content"][0]["text"])
    ```
  </Tab>

  <Tab value="Amazon Nova" title="Amazon Nova">
    ```python
    import json
    import os
    import boto3
    from opik.integrations.bedrock import track_bedrock

    # Set project name via environment variable
    os.environ["OPIK_PROJECT_NAME"] = "bedrock-integration-demo"

    # Initialize and track the Bedrock client
    bedrock_client = boto3.client("bedrock-runtime", region_name="us-east-1")
    bedrock_client = track_bedrock(bedrock_client)

    # Nova models use Amazon's nested content format
    request_body = {
        "messages": [
            {
                "role": "user",
                "content": [
                    {
                        "text": "Explain the benefits of LLM observability"
                    }
                ]
            }
        ],
        "inferenceConfig": {
            "max_new_tokens": 1000,
            "temperature": 0.7
        }
    }

    response = bedrock_client.invoke_model(
        modelId="us.amazon.nova-pro-v1:0",
        body=json.dumps(request_body),
        contentType="application/json",
        accept="application/json"
    )

    response_body = json.loads(response["body"].read())
    print("Response:", response_body["output"]["message"]["content"][0]["text"])
    ```
  </Tab>

  <Tab value="Meta Llama" title="Meta Llama">
    ```python
    import json
    import os
    import boto3
    from opik.integrations.bedrock import track_bedrock

    # Set project name via environment variable
    os.environ["OPIK_PROJECT_NAME"] = "bedrock-integration-demo"

    # Initialize and track the Bedrock client
    bedrock_client = boto3.client("bedrock-runtime", region_name="us-east-1")
    bedrock_client = track_bedrock(bedrock_client)

    # Llama models use prompt-based format with special tokens
    request_body = {
        "prompt": "<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\nExplain the benefits of LLM observability<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n",
        "max_gen_len": 1000,
        "temperature": 0.7,
        "top_p": 0.9
    }

    response = bedrock_client.invoke_model(
        modelId="us.meta.llama3-1-8b-instruct-v1:0",
        body=json.dumps(request_body),
        contentType="application/json",
        accept="application/json"
    )

    response_body = json.loads(response["body"].read())
    print("Response:", response_body["generation"])
    ```
  </Tab>

  <Tab value="Mistral AI" title="Mistral AI">
    ```python
    import json
    import os
    import boto3
    from opik.integrations.bedrock import track_bedrock

    # Set project name via environment variable
    os.environ["OPIK_PROJECT_NAME"] = "bedrock-integration-demo"

    # Initialize and track the Bedrock client
    bedrock_client = boto3.client("bedrock-runtime", region_name="us-east-1")
    bedrock_client = track_bedrock(bedrock_client)

    # Mistral models use OpenAI-like message format
    request_body = {
        "messages": [
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": "Explain the benefits of LLM observability"
                    }
                ]
            }
        ],
        "max_tokens": 1000,
        "temperature": 0.7,
        "top_p": 0.9
    }

    response = bedrock_client.invoke_model(
        modelId="us.mistral.pixtral-large-2502-v1:0",
        body=json.dumps(request_body),
        contentType="application/json",
        accept="application/json"
    )

    response_body = json.loads(response["body"].read())
    print("Response:", response_body["choices"][0]["message"]["content"])
    ```
  </Tab>
</Tabs>

## 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:

```python
import os
import boto3
from opik.integrations.bedrock import track_bedrock