(messages: Optional[Iterable[Message]])
| 35 | |
| 36 | |
| 37 | def summarize_usage(messages: Optional[Iterable[Message]]) -> Dict[str, int]: |
| 38 | prompt_tokens = 0 |
| 39 | completion_tokens = 0 |
| 40 | cached_tokens = 0 |
| 41 | cache_creation_input_tokens = 0 |
| 42 | api_calls = 0 |
| 43 | if messages: |
| 44 | for msg in messages: |
| 45 | if getattr(msg, 'role', None) != 'assistant': |
| 46 | continue |
| 47 | prompt_tokens += int(getattr(msg, 'prompt_tokens', 0) or 0) |
| 48 | completion_tokens += int(getattr(msg, 'completion_tokens', 0) or 0) |
| 49 | cached_tokens += int(getattr(msg, 'cached_tokens', 0) or 0) |
| 50 | cache_creation_input_tokens += int( |
| 51 | getattr(msg, 'cache_creation_input_tokens', 0) or 0) |
| 52 | api_calls += int(getattr(msg, 'api_calls', 0) or 0) |
| 53 | return { |
| 54 | 'prompt_tokens': prompt_tokens, |
| 55 | 'completion_tokens': completion_tokens, |
| 56 | 'total_tokens': prompt_tokens + completion_tokens, |
| 57 | 'cached_tokens': cached_tokens, |
| 58 | 'cache_creation_input_tokens': cache_creation_input_tokens, |
| 59 | 'api_calls': api_calls, |
| 60 | } |
| 61 | |
| 62 | |
| 63 | async def append_stats(path: str, record: Dict[str, Any]) -> None: |
no outgoing calls
no test coverage detected