Skip to content

fix(bedrock): log token metrics and summarize InvokeModel embeddings - #1081

Open
alexcastgonz (pcbeingused333) wants to merge 1 commit into
braintrustdata:mainfrom
pcbeingused333:fix/bedrock-invoke-model-embedding-metrics
Open

alexcastgonz (pcbeingused333) wants to merge 1 commit into
braintrustdata:mainfrom
pcbeingused333:fix/bedrock-invoke-model-embedding-metrics

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

Fixes #574

Root cause. _invoke_model_wrapper logs the parsed InvokeModel response body as span output and only reads token usage from a nested usage object (Converse shape) or Anthropic's usage. Amazon Titan Embeddings and Cohere Embed responses have neither, so their spans had no prompt_tokens/tokens, and the output contained the full embedding vectors. Titan v2 also repeats its vector under embeddingsByType.float.

Fix. For invoke_model the wrapper now also (1) reads X-Amzn-Bedrock-Input-Token-Count / X-Amzn-Bedrock-Output-Token-Count from the response headers, and (2) logs embedding / embeddings as embedding_length (+ embedding_count for batches), including Cohere typed embeddings dictionaries and Titan binary-only embeddingsByType responses, dropping the raw vector containers, matching what the other provider integrations do. Existing Converse/Anthropic metrics still take precedence. Streaming is unchanged.

Test. Two new tests (Titan Embeddings v2, Cohere Embed v3) use cassettes recorded against Bedrock in us-east-1, under cassettes/latest and cassettes/1.34.116 (the second with boto3/botocore 1.34.116). They fail on main (KeyError on the token metrics) and pass now. Six parameterized unit cases cover typed Cohere embeddings, binary-only Titan embeddings, the list shapes and unchanged non-vector payloads. With CI=1 and no credentials, test_bedrock_runtime.py gives 18 passed on latest, and 17 passed, 1 skipped on 1.34.116. ruff check and ruff format --check are clean.

This change was prepared with the help of an AI coding assistant (Claude Code and Codex), and the author is responsible for it.

Read token counts from Bedrock response headers when body usage is absent.
Summarize Titan and Cohere embedding vectors as length/count, including
Cohere typed dictionaries and Titan binary-only embeddingsByType payloads.
Keep existing Converse and Anthropic usage metrics unchanged.

Fixes braintrustdata#574
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-08T22:30:13.039060Z 6c52422 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bot] AWS Bedrock Runtime: embedding models via invoke_model (Titan/Cohere Embed) get no embedding-specific instrumentation

1 participant