Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,11 @@ pub(crate) fn decode_openai_usage(value: Option<&Value>) -> Usage {
.and_then(Value::as_u64);
let cache_creation_input_tokens = value
.get("prompt_tokens_details")
.and_then(|details| details.get("cache_creation_tokens"))
.and_then(|details| {
details
.get("cache_write_tokens")
.or_else(|| details.get("cache_creation_tokens"))
})
.and_then(Value::as_u64);
Usage {
input_tokens: value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,11 @@ fn openai_usage(usage: &Map<String, Value>) -> Usage {
.and_then(Value::as_u64);
let cache_creation_input_tokens = usage
.get("prompt_tokens_details")
.and_then(|details| details.get("cache_creation_tokens"))
.and_then(|details| {
details
.get("cache_write_tokens")
.or_else(|| details.get("cache_creation_tokens"))
})
.and_then(Value::as_u64);
Usage {
input_tokens: usage
Expand Down
64 changes: 34 additions & 30 deletions crates/switchyard-translation/tests/response_translation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,38 +159,42 @@ fn openai_chat_cache_usage_translates_to_responses_usage_details() -> TestResult
Ok(())
}

// Verifies OpenAI cache usage is expressed in Anthropic's standard sibling fields.
// Verifies OpenRouter's cache-write field and the legacy alias normalize identically.
#[test]
fn openai_chat_cache_usage_translates_to_anthropic_usage_fields() -> TestResult {
fn openai_chat_cache_write_aliases_translate_to_anthropic_usage_fields() -> TestResult {
let engine = TranslationEngine::default();
let body = json!({
"id": "chatcmpl-test",
"model": "gpt-cached",
"choices": [{
"index": 0,
"message": {"role": "assistant", "content": "Cached answer"},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 100,
"completion_tokens": 5,
"total_tokens": 105,
"prompt_tokens_details": {"cached_tokens": 80}
}
});

let output = engine
.translate_response(
WireFormat::OpenAiChat,
WireFormat::AnthropicMessages,
&body,
&TranslationPolicy::default(),
)?
.body;

assert_eq!(output["usage"]["input_tokens"], 20);
assert_eq!(output["usage"]["cache_read_input_tokens"], 80);
assert_eq!(output["usage"]["output_tokens"], 5);
for cache_write_field in ["cache_write_tokens", "cache_creation_tokens"] {
let mut body = json!({
"id": "chatcmpl-test",
"model": "gpt-cached",
"choices": [{
"index": 0,
"message": {"role": "assistant", "content": "Cached answer"},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 100,
"completion_tokens": 5,
"total_tokens": 105,
"prompt_tokens_details": {"cached_tokens": 70}
}
});
body["usage"]["prompt_tokens_details"][cache_write_field] = json!(10);

let output = engine
.translate_response(
WireFormat::OpenAiChat,
WireFormat::AnthropicMessages,
&body,
&TranslationPolicy::default(),
)?
.body;

assert_eq!(output["usage"]["input_tokens"], 20);
assert_eq!(output["usage"]["cache_read_input_tokens"], 70);
assert_eq!(output["usage"]["cache_creation_input_tokens"], 10);
assert_eq!(output["usage"]["output_tokens"], 5);
}
Ok(())
}

Expand Down
35 changes: 35 additions & 0 deletions crates/switchyard-translation/tests/stream_translation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,41 @@ fn openai_chat_stream_cache_usage_translates_to_responses_usage_details() -> Tes
Ok(())
}

// Verifies OpenRouter's cache-write field and the legacy alias normalize identically.
#[test]
fn openai_chat_stream_cache_write_usage_is_normalized() -> TestResult {
for cache_write_field in ["cache_write_tokens", "cache_creation_tokens"] {
let mut state = StreamTranslationState::new(WireFormat::OpenAiChat, WireFormat::OpenAiChat);
let mut event = json!({
"id": "chatcmpl-test",
"object": "chat.completion.chunk",
"model": "gpt-cached",
"choices": [],
"usage": {
"prompt_tokens": 100,
"completion_tokens": 5,
"total_tokens": 105,
"prompt_tokens_details": {"cached_tokens": 70}
}
});
event["usage"]["prompt_tokens_details"][cache_write_field] = json!(10);

let chunks = decode_stream_event(&mut state, WireFormat::OpenAiChat, &event);
let Some(usage) = chunks.iter().find_map(|chunk| match chunk {
LlmResponseChunk::Usage(usage) => Some(usage),
_ => None,
}) else {
return Err(format!("expected usage chunk for {cache_write_field}").into());
};

assert_eq!(usage.input_tokens, Some(20));
assert_eq!(usage.cached_input_tokens(), Some(70));
assert_eq!(usage.cache_creation_input_tokens(), Some(10));
assert_eq!(usage.output_tokens, Some(5));
}
Ok(())
}

// Verifies the streamed total-token fallback keeps cached tokens when upstream omits total_tokens.
#[test]
fn responses_stream_usage_without_total_keeps_cached_tokens_in_total() -> TestResult {
Expand Down
Loading