fix(bedrock): preserve the service total when folding cache input tokens (#12586)

Co-authored-by: Douwe Osinga <douwe.osinga@gmail.com>
This commit is contained in:
Lucien 2026-10-02 14:56:19 +00:00 • committed by GitHub
parent b9db895a77
commit aa294302fe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 14 additions and 9 deletions

View file

@ -45,7 +45,6 @@ jobs:
path: |
target/debug/goose
target/debug/mcp_conformance_driver
retention-days: 1
if-no-files-found: error
conformance:

View file

@ -489,7 +489,8 @@ mcp-conformance version="2025-11-25" suite="all" conformance_version="0.2.0-alph
if [ -n "{{baseline}}" ]; then
baseline_args=(--expected-failures "{{baseline}}")
fi
GOOSE_DISABLE_KEYRING=1 npx -y @modelcontextprotocol/conformance@{{conformance_version}} client --command "target/debug/mcp_conformance_driver" --spec-version "{{version}}" --suite "{{suite}}" ${baseline_args[@]+"${baseline_args[@]}"}
export GOOSE_BIN="$PWD/target/debug/goose"
GOOSE_DISABLE_KEYRING=1 npx -y @modelcontextprotocol/conformance@{{conformance_version}} client --command "$PWD/target/debug/mcp_conformance_driver" --spec-version "{{version}}" --suite "{{suite}}" ${baseline_args[@]+"${baseline_args[@]}"}
build-test-tools:
cargo build -p goose-test

View file

@ -174,7 +174,7 @@ impl Usage {
}
/// For providers whose reported `input_tokens`/`total_tokens` exclude
/// cache tokens (e.g. Anthropic, Bedrock): folds the cache breakdown in.
/// cache tokens (e.g. Anthropic): folds the cache breakdown in.
pub fn from_cache_exclusive_input(
input_tokens: Option<i32>,
output_tokens: Option<i32>,

View file

@ -612,13 +612,18 @@ pub fn from_bedrock_role(role: &bedrock::ConversationRole) -> Result<Role> {
}
pub fn from_bedrock_usage(usage: &bedrock::TokenUsage) -> Usage {
Usage::from_cache_exclusive_input(
Some(usage.input_tokens),
let cache_read = usage.cache_read_input_tokens;
let cache_write = usage.cache_write_input_tokens;
let input_tokens = usage
.input_tokens
.saturating_add(cache_read.unwrap_or(0))
.saturating_add(cache_write.unwrap_or(0));
Usage::new(
Some(input_tokens),
Some(usage.output_tokens),
Some(usage.total_tokens),
usage.cache_read_input_tokens,
usage.cache_write_input_tokens,
)
.with_cache_tokens(cache_read, cache_write)
}
pub fn from_bedrock_json(document: &Document) -> Result<Value> {
@ -1106,11 +1111,11 @@ mod tests {
}
#[test]
fn test_from_bedrock_usage_folds_cache_tokens_into_input() {
fn test_from_bedrock_usage_includes_cache_in_input_and_preserves_total() {
let usage = bedrock::TokenUsage::builder()
.input_tokens(7)
.output_tokens(50)
.total_tokens(57)
.total_tokens(6057)
.cache_read_input_tokens(5000)
.cache_write_input_tokens(1000)
.build()