Integrations / AWS Bedrock

Trace AWS Bedrock model calls

To trace AWS Bedrock with Tracify, record each Converse call as a step and copy the token counts from the response. Bedrock has no automatic wrapper in the SDK, so this is a few lines you write once.

Your code

AWS Bedrock

Tracify

One run.

  • 01Model ID for each Bedrock call
  • 02Input and output tokens from the Converse response
  • 03Latency and throttling errors
  • 04Calls grouped into the agent run

Setup

6 steps.

  1. 01

    Install

    Nothing extra beyond boto3 and the Tracify SDK.

    bash
    pip install tracify-sdk boto3
  2. 02

    Set your project key

    Create a project in Tracify, copy its API key and put it in the environment where your code runs. The SDK reads it from there, so nothing is hard-coded.

    bash
    export TRACIFY_API_KEY="tracify_sk_live_..."
  3. 03

    Record the Converse call as a step

    Open a step around the call, then copy the model and the token counts from the response. If Bedrock throws an error such as throttling, the step records it and the error still reaches your code.

    python
    import boto3
    import tracify
    
    bedrock = boto3.client("bedrock-runtime")
    
    def converse(model_id: str, prompt: str) -> str:
        with tracify.span("bedrock-converse", type="llm", input=prompt) as step:
            response = bedrock.converse(
                modelId=model_id,
                messages=[{"role": "user", "content": [{"text": prompt}]}],
            )
            usage = response["usage"]
            text = response["output"]["message"]["content"][0]["text"]
            step.set(model=model_id, input_tokens=usage["inputTokens"], output_tokens=usage["outputTokens"])
            step.set_output(text)
            return text
  4. 04

    Or in TypeScript

    The same pattern with the AWS SDK for JavaScript.

    typescript
    import { BedrockRuntimeClient, ConverseCommand } from "@aws-sdk/client-bedrock-runtime";
    import { span } from "tracify-sdk";
    
    const bedrock = new BedrockRuntimeClient({});
    
    export function converse(modelId: string, prompt: string) {
      return span(
        "bedrock-converse",
        async (step) => {
          const response = await bedrock.send(
            new ConverseCommand({
              modelId,
              messages: [{ role: "user", content: [{ text: prompt }] }],
            }),
          );
          step.set({
            model: modelId,
            inputTokens: response.usage?.inputTokens,
            outputTokens: response.usage?.outputTokens,
          });
          return response.output?.message?.content?.[0]?.text ?? "";
        },
        { type: "llm", input: prompt },
      );
    }
  5. 05

    Group calls into one run

    Wrap your agent's entry point with trace_agent (Python) or traceAgent (TypeScript). Every Bedrock step made inside it joins the same trace, in order.

    python
    @tracify.trace_agent(name="bedrock-agent")
    def run(question: str):
        plan = converse("anthropic.claude-3-5-sonnet-20240620-v1:0", question)
        return converse("anthropic.claude-3-5-sonnet-20240620-v1:0", plan)
  6. 06

    Run it once and look

    Run your code one time, then open the Tracing page of your project. Your Bedrock call shows up as a run with its steps in order, plus the tokens, time and any error for each call.

Good to know

Manual, on purpose

The SDK wraps the OpenAI and Anthropic client shapes. Bedrock uses a different API, so there is no one-line wrapper. The step above gives you the same fields with a few lines of your own.

Good to know

Anthropic models on Bedrock

If you call Claude through the Anthropic library's Bedrock client, you can wrap that client with wrap_anthropic instead and skip the manual step.

Good to know

About cost

Tracify records the tokens the provider returns. A dollar cost is shown when you give the wrapper your own rates with calculate_cost (Python) or calculateCost (TypeScript), so the number always matches your real price plan.

Full details in the documentation. All integrations are listed on the integrations page.

Can I trace Bedrock streaming calls?
Yes, with the same step. Read the usage block that arrives in the final event of the stream and set the tokens when the stream ends.
Will I see throttling errors?
Yes. An error raised inside the step is recorded on it, with its type and message, and then raised again to your code.
Do I need to change my IAM permissions?
No. Tracify never talks to AWS. It only receives what your code sends it.

Send your first trace.

Free to start.