fix(aws): keep SageMaker billed usage from the response body #827
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
tonydzi
SageMaker chat and embed responses report billed usage as -1 and drop
search_units.map_response_from_bedrockreplaced the responsemetawith token counts taken only from theX-Amzn-Bedrock-Input-Token-CountandX-Amzn-Bedrock-Output-Token-Countheaders, and defaulted each missing header to -1. SageMaker does not send those headers. The replacement object also has nosearch_units,images, orapi_version, so a Bedrock response that does send the headers still lost every billed unit the headers do not carry.The hook now keeps
metafrom 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.pyis 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 includesearch_units, a SageMaker embed body that billsimages, a Bedrock response whose headers override token counts whilesearch_unitsremains, and a chat body with nometaand no Bedrock headers. Before the change those cases come back as -1 and withoutsearch_unitsorimages. After the change the four tests pass. The rest oftests/test_aws_client_unit.pypasses as well.Behaviour change: a SageMaker chat response with no body
metaused to come back withtokensandbilled_unitsat -1; nowmetais absent, so callers readingresponse.meta.billed_units.input_tokensmust guard formeta=None. An emptyX-Amzn-Bedrock--Token-Countheader now falls back to the body counts instead of raisingValueError.This change was prepared with the help of an AI coding agent.
<!-- CURSOR_SUMMARY -->
---
<!-- /CURSOR_SUMMARY -->