Returns a dictionary of OpenTelemetry token usage attributes.
(self)
| 67 | return (candidates_tokens or 0) + (thoughts_tokens or 0) |
| 68 | |
| 69 | def to_attributes(self) -> dict[str, AttributeValue]: |
| 70 | """Returns a dictionary of OpenTelemetry token usage attributes.""" |
| 71 | attrs: dict[str, AttributeValue] = {} |
| 72 | if self.input_token_count is not None: |
| 73 | attrs[GEN_AI_USAGE_INPUT_TOKENS] = self.input_token_count |
| 74 | if self.output_token_count is not None: |
| 75 | attrs[GEN_AI_USAGE_OUTPUT_TOKENS] = self.output_token_count |
| 76 | |
| 77 | if self.usage_metadata is not None: |
| 78 | cached_tokens = self.usage_metadata.cached_content_token_count |
| 79 | if cached_tokens is not None: |
| 80 | attrs[GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] = cached_tokens |
| 81 | |
| 82 | thoughts_tokens = self.usage_metadata.thoughts_token_count |
| 83 | if thoughts_tokens is not None: |
| 84 | attrs[GEN_AI_USAGE_REASONING_OUTPUT_TOKENS] = thoughts_tokens |
| 85 | |
| 86 | system_instruction_tokens = getattr( |
| 87 | self.usage_metadata, 'system_instruction_tokens', None |
| 88 | ) |
| 89 | if system_instruction_tokens is not None: |
| 90 | attrs['gen_ai.usage.experimental.system_instruction_tokens'] = ( |
| 91 | system_instruction_tokens |
| 92 | ) |
| 93 | |
| 94 | return attrs |