Switchyard lets LLM applications route traffic across models and providers while preserving native OpenAI and Anthropic API compatibility - enabling flexible model selection, benchmarking, and cost/performance optimization.
Rust
fix(translation): preserve Anthropic thinking-token usage (#769)
nachiketb-nvidia7508c22f4b37
diff --git a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs b/crates/switchyard-translation/src/codecs/anthropic/buffered.rsindex 9661c7674..82722c75d 100644--- a/crates/switchyard-translation/src/codecs/anthropic/buffered.rs+++ b/crates/switchyard-translation/src/codecs/anthropic/buffered.rs@@ -1195,10 +1195,12 @@ fn decode_anthropic_usage(value: Option<&Value>) -> Usage {+ cache_creation_input_tokens.unwrap_or(0)+ output}),- reasoning_tokens: value- .get("output_tokens_details")- .and_then(|details| details.get("reasoning_tokens"))- .and_then(Value::as_u64),+ reasoning_tokens: value.get("output_tokens_details").and_then(|details| {+ details+ .get("thinking_tokens")+ .and_then(Value::as_u64)+ .or_else(|| details.get("reasoning_tokens").and_then(Value::as_u64))+ }),}}@@ -1214,6 +1216,9 @@ fn encode_anthropic_usage(usage: &Usage) -> Value {if let Some(cache_creation_tokens) = usage.cache_creation_input_tokens() {value["cache_creation_input_tokens"] = json!(cache_creation_tokens);}+ if let Some(thinking_tokens) = usage.reasoning_tokens {+ value["output_tokens_details"] = json!({"thinking_tokens": thinking_tokens});+ }value}diff --git a/crates/switchyard-translation/src/codecs/anthropic/stream.rs b/crates/switchyard-translation/src/codecs/anthropic/stream.rsindex 7b5663ce4..62dc0e368 100644--- a/crates/switchyard-translation/src/codecs/anthropic/stream.rs+++ b/crates/switchyard-translation/src/codecs/anthropic/stream.rs@@ -574,6 +574,14 @@ fn capture_anthropic_usage(state: &mut StreamTranslationState, usage: &Value) {if let Some(value) = usage.get("cache_read_input_tokens").and_then(Value::as_u64) {state.usage.set_cached_input_tokens(value);}+ if let Some(value) = usage.get("output_tokens_details").and_then(|details| {+ details+ .get("thinking_tokens")+ .and_then(Value::as_u64)+ .or_else(|| details.get("reasoning_tokens").and_then(Value::as_u64))+ }) {+ state.usage.reasoning_tokens = Some(value);+ }}// Builds Anthropic usage payloads from normalized and provider-extra state.@@ -596,6 +604,12 @@ fn anthropic_stream_usage(state: &StreamTranslationState) -> Value {if let Some(value) = state.usage.cached_input_tokens() {usage.insert("cache_read_input_tokens".to_string(), json!(value));}+ if let Some(value) = state.usage.reasoning_tokens {+ usage.insert(+ "output_tokens_details".to_string(),+ json!({"thinking_tokens": value}),+ );+ }Value::Object(usage)}diff --git a/crates/switchyard-translation/tests/response_translation.rs b/crates/switchyard-translation/tests/response_translation.rsindex 29e81eabb..c4ad3e83d 100644--- a/crates/switchyard-translation/tests/response_translation.rs+++ b/crates/switchyard-translation/tests/response_translation.rs@@ -371,6 +371,85 @@ fn anthropic_thinking_response_translates_to_openai_reasoning_content() -> TestRlet message = &output["choices"][0]["message"];assert_eq!(message["content"], "Visible answer");assert_eq!(message["reasoning_content"], "private reasoning");+ for (details, thinking_tokens) in [+ (json!({"thinking_tokens": 0}), 0),+ (json!({"thinking_tokens": 5}), 5),+ (json!({"thinking_tokens": null, "reasoning_tokens": 5}), 5),+ ] {+ let mut body = body.clone();+ body["usage"]["output_tokens_details"] = details;+ let decoded = engine+ .decode_response(WireFormat::AnthropicMessages, &body, &normalized_policy())?+ .response;+ assert_eq!(decoded.usage.reasoning_tokens, Some(thinking_tokens));+ assert_eq!(decoded.usage.output_tokens, Some(7));+ assert_eq!(decoded.usage.total_tokens, Some(19));++ for (target, details, field) in [+ (+ WireFormat::OpenAiChat,+ "completion_tokens_details",+ "reasoning_tokens",+ ),+ (+ WireFormat::OpenAiResponses,+ "output_tokens_details",+ "reasoning_tokens",+ ),+ (+ WireFormat::AnthropicMessages,+ "output_tokens_details",+ "thinking_tokens",+ ),+ ] {+ let output = engine+ .translate_response(+ WireFormat::AnthropicMessages,+ target,+ &body,+ &normalized_policy(),+ )?+ .body;+ assert_eq!(output["usage"][details][field], thinking_tokens);+ let mut state = switchyard_translation::StreamTranslationState::new(+ WireFormat::AnthropicMessages,+ target,+ );+ engine.translate_event(+ &mut state,+ WireFormat::AnthropicMessages,+ target,+ &json!({"type": "message_start", "message": {+ "id": "msg_test", "model": "claude-opus", "usage": {"input_tokens": 12}+ }}),+ )?;+ engine.translate_event(+ &mut state,+ WireFormat::AnthropicMessages,+ target,+ &json!({"type": "message_delta", "delta": {}, "usage": body["usage"]}),+ )?;+ let mut events = engine.translate_event(+ &mut state,+ WireFormat::AnthropicMessages,+ target,+ &json!({"type": "message_delta", "delta": {"stop_reason": "end_turn"},+ "usage": {"output_tokens": 7}}),+ )?;+ assert_eq!(state.usage.reasoning_tokens, Some(thinking_tokens));+ assert_eq!(state.usage.output_tokens, Some(7));+ events.extend(engine.finish_stream(&mut state, target)?);+ let usage = events+ .iter()+ .find_map(|event| {+ event+ .get("usage")+ .or_else(|| event.get("response")?.get("usage"))+ })+ .ok_or("missing terminal usage")?;+ assert_eq!(usage[details][field], thinking_tokens);+ }+ }Ok(())}