Skip to main content

token_usage

Function token_usage 

Source
pub fn token_usage(response: &ChatResponse) -> TokenUsage
Expand description

Convert a response’s wire usage block into trusty-code’s TokenUsage.

Why: PerfCollector::record_phase accepts &TokenUsage; keeping the conversion in ONE place stops each call site from re-deciding how the two cache-token shapes (flat Anthropic-native fields vs. OpenRouter’s nested prompt_tokens_details) merge. What: delegates the merge to the shared UsageBlock::into_usage — which is the same field-for-field logic the deleted UsageBlock::into_token_usage performed — then copies the five fields across. cost_usd carries the provider’s authoritative, already-cache-discounted price so callers prefer it over a static per-token recompute. Test: token_usage_maps_flat_cache_fields, token_usage_maps_openrouter_details, token_usage_carries_cost.