|
| 1 | +""" |
| 2 | +Utilities for tracking token usage and costs for LLM API calls. |
| 3 | +""" |
| 4 | + |
| 5 | +from typing import Any |
| 6 | +from uuid import UUID |
| 7 | + |
| 8 | +from beartype import beartype |
| 9 | +from litellm.utils import ModelResponse, token_counter |
| 10 | + |
| 11 | +from ...queries.usage.create_usage_record import create_usage_record |
| 12 | + |
| 13 | + |
| 14 | +@beartype |
| 15 | +async def track_usage( |
| 16 | + *, |
| 17 | + developer_id: UUID, |
| 18 | + model: str, |
| 19 | + messages: list[dict], |
| 20 | + response: ModelResponse, |
| 21 | + custom_api_used: bool = False, |
| 22 | + metadata: dict[str, Any] = {}, |
| 23 | +) -> None: |
| 24 | + """ |
| 25 | + Tracks token usage and costs for an LLM API call. |
| 26 | +
|
| 27 | + Parameters: |
| 28 | + developer_id (UUID): The unique identifier for the developer. |
| 29 | + model (str): The model used for the API call. |
| 30 | + messages (list[dict]): The messages sent to the model. |
| 31 | + response (ModelResponse): The response from the LLM API call. |
| 32 | + custom_api_used (bool): Whether a custom API key was used. |
| 33 | + metadata (dict): Additional metadata about the usage. |
| 34 | +
|
| 35 | + Returns: |
| 36 | + None |
| 37 | + """ |
| 38 | + |
| 39 | + # Try to get token counts from response.usage |
| 40 | + if response.usage: |
| 41 | + prompt_tokens = response.usage.prompt_tokens |
| 42 | + completion_tokens = response.usage.completion_tokens |
| 43 | + else: |
| 44 | + # Calculate tokens manually if usage is not available |
| 45 | + prompt_tokens = token_counter(model=model, messages=messages) |
| 46 | + |
| 47 | + # Calculate completion tokens from the response |
| 48 | + completion_content = [ |
| 49 | + {"content": choice.message.content} |
| 50 | + for choice in response.choices |
| 51 | + if hasattr(choice, "message") |
| 52 | + and choice.message |
| 53 | + and hasattr(choice.message, "content") |
| 54 | + and choice.message.content |
| 55 | + ] |
| 56 | + |
| 57 | + completion_tokens = ( |
| 58 | + token_counter(model=model, messages=completion_content) if completion_content else 0 |
| 59 | + ) |
| 60 | + |
| 61 | + # Map the model name to the actual model name |
| 62 | + actual_model = model |
| 63 | + |
| 64 | + # Create usage record |
| 65 | + await create_usage_record( |
| 66 | + developer_id=developer_id, |
| 67 | + model=actual_model, |
| 68 | + prompt_tokens=prompt_tokens, |
| 69 | + completion_tokens=completion_tokens, |
| 70 | + custom_api_used=custom_api_used, |
| 71 | + metadata={ |
| 72 | + "request_id": response.id if hasattr(response, "id") else None, |
| 73 | + **metadata, |
| 74 | + }, |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +@beartype |
| 79 | +async def track_embedding_usage( |
| 80 | + *, |
| 81 | + developer_id: UUID, |
| 82 | + model: str, |
| 83 | + inputs: list[str], |
| 84 | + response: Any, |
| 85 | + custom_api_used: bool = False, |
| 86 | + metadata: dict[str, Any] = {}, |
| 87 | +) -> None: |
| 88 | + """ |
| 89 | + Tracks token usage and costs for an embedding API call. |
| 90 | +
|
| 91 | + Parameters: |
| 92 | + developer_id (UUID): The unique identifier for the developer. |
| 93 | + model (str): The model used for the embedding. |
| 94 | + inputs (list[str]): The inputs sent for embedding. |
| 95 | + response (Any): The response from the embedding API call. |
| 96 | + custom_api_used (bool): Whether a custom API key was used. |
| 97 | + metadata (dict): Additional metadata about the usage. |
| 98 | +
|
| 99 | + Returns: |
| 100 | + None |
| 101 | + """ |
| 102 | + |
| 103 | + # Try to get token count from response.usage |
| 104 | + if hasattr(response, "usage") and response.usage: |
| 105 | + prompt_tokens = response.usage.prompt_tokens |
| 106 | + else: |
| 107 | + # Calculate tokens manually if usage is not available |
| 108 | + prompt_tokens = sum( |
| 109 | + token_counter(model=model, text=input_text) for input_text in inputs |
| 110 | + ) |
| 111 | + |
| 112 | + # Map the model name to the actual model name |
| 113 | + actual_model = model |
| 114 | + |
| 115 | + # Create usage record for embeddings (no completion tokens) |
| 116 | + await create_usage_record( |
| 117 | + developer_id=developer_id, |
| 118 | + model=actual_model, |
| 119 | + prompt_tokens=prompt_tokens, |
| 120 | + completion_tokens=0, # Embeddings don't have completion tokens |
| 121 | + custom_api_used=custom_api_used, |
| 122 | + metadata=metadata, |
| 123 | + ) |
0 commit comments