← Back to Pull requests

fix(aws): keep SageMaker billed usage from the response body #827

Open
unknown opened this pull request with 424 lines diff across 5 files
View on GitHub
unknown unknown commented on Nov 13, 2026 16:59 UTC
Author

SageMaker chat and embed responses report billed usage as -1 and drop search_units.

map_response_from_bedrock replaced the response meta with token counts taken only from the X-Amzn-Bedrock-Input-Token-Count and X-Amzn-Bedrock-Output-Token-Count headers, and defaulted each missing header to -1. SageMaker does not send those headers. The replacement object also has no search_units, images, or api_version, so a Bedrock response that does send the headers still lost every billed unit the headers do not carry.

The hook now keeps meta from the response body. When a Bedrock token-count header is present, that count replaces only the matching input or output token field. Other billed units stay as the model reported them. A response with neither body usage nor those headers is left unchanged, instead of being given a synthetic usage of -1.

src/cohere/aws_client.py is listed in .fernignore, so this stays in the hand-maintained AWS client across SDK regenerations.

Testing

tests/test_aws_client_unit.py (TestAwsResponseMetaUsage) builds an httpx response and runs the response hook. It covers a SageMaker chat body whose billed units include search_units, a SageMaker embed body that bills images, a Bedrock response whose headers override token counts while search_units remains, and a chat body with no meta and no Bedrock headers. Before the change those cases come back as -1 and without search_units or images. After the change the four tests pass. The rest of tests/test_aws_client_unit.py passes as well.

Behaviour change: a SageMaker chat response with no body meta used to come back with tokens and billed_units at -1; now meta is absent, so callers reading response.meta.billed_units.input_tokens must guard for meta=None. An empty X-Amzn-Bedrock--Token-Count header now falls back to the body counts instead of raising ValueError.

This change was prepared with the help of an AI coding agent.

<!-- CURSOR_SUMMARY -->
---

[!NOTE]

Medium Risk

Changes how AWS client responses expose billing/usage metadata; SageMaker callers benefit but responses without body meta no longer get synthetic -1 token counts.

> Overview

The Bedrock/SageMaker response hook no longer replaces meta with token counts parsed only from X-Amzn-Bedrock--Token-Count headers (which defaulted missing values to -1). It now starts from the response body’s meta and overlays input/output token fields only when those headers are present and non-empty, so SageMaker billed usage (search_units, images, api_version, etc.) is preserved and Bedrock can still override token counts without dropping other billed units.

> When there is no body meta and no Bedrock token headers, meta is omitted instead of injecting synthetic -1 usage. Empty token-count headers fall back to body counts rather than failing or overwriting with bad values.

> TestAwsResponseMetaUsage in tests/test_aws_client_unit.py exercises SageMaker chat/embed, Bedrock header overlay, missing meta, partial headers, and invalid meta shapes.

> <sup>Reviewed by Cursor Bugbot for commit b0e0ad37ee0e37e2241d67268635cdc5a0349bed. Bugbot is set up for automated code reviews on this repo. Configure here.</sup>

<!-- /CURSOR_SUMMARY -->

Diff: 424 lines (+318 / -106) across 5 files Reviewer: tonydzi Review duration: 24823m
All Vouch review confidence checks passed
Residual risk 0.24 = Change Risk (0.45) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 424 lines diff across 5 files
0.45
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
0.02
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
46%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.362
Shallow Review Depth shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #827"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 46.7% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 45.1% (MEDIUM) · Higher = larger blast radius
45.1%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 46.7% · Lower = rushed or shallow review
46.7%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 24% · Safe to merge
24%