> ## Documentation Index
> Fetch the complete documentation index at: https://docs.zespan.com/llms.txt
> Use this file to discover all available pages before exploring further.

# AWS Bedrock

> Trace Amazon Bedrock model invocations in TypeScript and Python with wrapBedrock() or patch_bedrock().

<Note>
  Available for: **Python** and **TypeScript**.
</Note>

Wrap your `BedrockRuntimeClient` with `wrapBedrock()` (TypeScript) to trace all model invocations through the Bedrock Converse API — or patch `boto3` with `patch_bedrock()` (Python).

## Installation

<CodeGroup>
  ```bash TypeScript theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  npm install @zespan/sdk @aws-sdk/client-bedrock-runtime
  ```

  ```bash Python theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  pip install zespan boto3
  ```
</CodeGroup>

## Setup

<CodeGroup>
  ```typescript TypeScript theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  import { BedrockRuntimeClient } from "@aws-sdk/client-bedrock-runtime";
  import { zespan } from "@zespan/sdk";

  zespan.init({ apiKey: process.env.ZESPAN_API_KEY! });

  const bedrock = zespan.wrapBedrock(new BedrockRuntimeClient({ region: "us-east-1" }));
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  import os
  import zespan

  zespan.init(api_key=os.environ["ZESPAN_API_KEY"])
  zespan.patch_bedrock()

  import boto3  # import after patching
  client = boto3.client("bedrock-runtime", region_name="us-east-1")
  ```
</CodeGroup>

In TypeScript, `wrapBedrock()` returns a wrapped client instance. In Python, `patch_bedrock()` patches `botocore.client.ClientCreator.create_client` — every `boto3.client("bedrock-runtime", ...)` instance created afterward has its `converse()` and `converse_stream()` methods traced automatically, with no wrapping step.

## Example

<CodeGroup>
  ```typescript TypeScript theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  const response = await bedrock.converse({
    modelId: "anthropic.claude-sonnet-4-6-v1",
    messages: [{ role: "user", content: [{ text: "Summarize observability in two sentences." }] }],
  });
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  response = client.converse(
      modelId="anthropic.claude-sonnet-4-6-v1",
      messages=[{"role": "user", "content": [{"text": "Summarize observability in two sentences."}]}],
  )
  ```
</CodeGroup>

<Note>
  Both `wrapBedrock()` and `patch_bedrock()` trace the **Converse API** (`converse` / `converse_stream`) only — the older, model-specific `invoke_model` / `InvokeModelCommand` API is not patched. Use `converse()` for traced calls.
</Note>

`converse_stream()` (Python) and streaming Converse calls (TypeScript) are traced end-to-end in both languages, including time-to-first-token.

## What gets captured

| Field          | Details                                                                                                                                  |
| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| Model          | Bedrock model ID (e.g. `anthropic.claude-sonnet-4-6-v1`)                                                                                 |
| Input tokens   | From `usage.inputTokens` in the Converse response                                                                                        |
| Output tokens  | From `usage.outputTokens` in the Converse response                                                                                       |
| Cost           | Calculated from token counts and Bedrock pricing                                                                                         |
| Latency        | Total invocation duration                                                                                                                |
| Server latency | `metrics.latencyMs` from the Converse response, when present. Python only — the TypeScript wrapper does not currently extract this field |
| Finish reason  | Bedrock's `stopReason` (e.g. `end_turn`, `tool_use`, `max_tokens`)                                                                       |
| Tool calls     | Tool name and parsed input from `toolUse` content blocks, both languages                                                                 |

<Note>
  Token extraction requires the model response to include `usage` metadata. All Anthropic and Llama models on Bedrock return this via the Converse API. Check your model's response schema if tokens show as zero.
</Note>
