usage_from_mapping#
Connect a provider and configure retries, credentials, and transport. See the guide.
- zett_agent.providers.base.usage_from_mapping(payload)[source]#
Normalize token usage returned by OpenAI-compatible chat APIs.
This helper is shared by
OpenAIProviderandDeepSeekProvider. Their response fields map as follows:ModelUsage field Accepted API fields ------------------- ------------------------------------------------- input_tokens prompt_tokens, input_tokens output_tokens completion_tokens, output_tokens cache_read_tokens prompt_cache_hit_tokens, cache_read_input_tokens, cached_tokens, prompt_tokens_details.cached_tokens, input_tokens_details.cached_tokens cache_write_tokens cache_write_tokens, cache_creation_input_tokens, cache_creation_tokens, prompt_tokens_details.cache_write_tokens, input_tokens_details.cache_write_tokens reasoning_tokens completion_tokens_details.reasoning_tokens, output_tokens_details.reasoning_tokens
The aliases exist because gateways front one protocol with another and return whichever usage schema their upstream speaks. Reading all of them keeps a relay from silently reporting zero cache reads.
prompt_tokensincludes cached input tokens. For DeepSeek it is the sum ofprompt_cache_hit_tokensandprompt_cache_miss_tokens; the miss count therefore has no separate destination inModelUsage. Likewise,completion_tokensincludes reasoning tokens rather than being added to them. Both providers returntotal_tokens, butModelUsagederives that total from input plus output to preserve one internal invariant.A payload that carries no cache counter at all leaves
cache_reportedFalse, socache_hit_ratereports unknown instead of a false zero. Anthropic reportsinput_tokens,cache_read_input_tokens, andcache_creation_input_tokensseparately. Gemini reportsprompt_token_count,candidates_token_count,cached_content_token_count, andthoughts_token_count. Ollama reportsprompt_eval_countandeval_count. Those provider-specific schemas are normalized in their own adapters rather than by this helper.- Parameters:
payload (Mapping[str, JsonValue])
- Return type: