-
Notifications
You must be signed in to change notification settings - Fork 985
refactor(agents-api): centralize usage tracking and remove deprecated metrics #1513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
abhinav-1305
wants to merge
6
commits into
julep-ai:dev
Choose a base branch
from
abhinav-1305:usage-tracking
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dd913f1
refactor(agents-api): centralize usage tracking and remove deprecated…
abhinav-1305 09359d5
prevent tracking when no output is collected
abhinav-1305 cb5f930
Update src/agents-api/agents_api/common/utils/usage_tracker.py
abhinav-1305 728327b
Update src/agents-api/agents_api/common/utils/usage_tracker.py
abhinav-1305 dd25ae7
remove centralized usage tracking from create_response function
abhinav-1305 8c69a31
Merge branch 'usage-tracking' of https://github.com/abhinav-1305/jule…
abhinav-1305 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/agents-api/agents_api/common/utils/usage_tracker.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| """ | ||
| Centralized usage tracking utilities for LLM API calls. | ||
| Handles both Prometheus metrics and database tracking. | ||
| """ | ||
|
|
||
| from typing import Any | ||
| from uuid import UUID | ||
|
|
||
| from beartype import beartype | ||
| from litellm.utils import ModelResponse, Choices, Message | ||
| from prometheus_client import Counter | ||
|
|
||
| from .usage import track_usage | ||
|
|
||
| # Prometheus metrics | ||
| total_tokens_per_user = Counter( | ||
| "total_tokens_per_user", | ||
| "Total token count per user", | ||
| labelnames=("developer_id",), | ||
| ) | ||
|
|
||
|
|
||
| @beartype | ||
| async def track_completion_usage( | ||
| *, | ||
| developer_id: UUID, | ||
| model: str, | ||
| messages: list[dict], | ||
| response: ModelResponse, | ||
| custom_api_used: bool = False, | ||
| metadata: dict[str, Any] = {}, | ||
| connection_pool: Any = None, | ||
| ) -> None: | ||
| """ | ||
| Tracks usage for completion responses (both streaming and non-streaming). | ||
| Args: | ||
| developer_id: The developer ID for usage tracking | ||
| model: The model name used for the response | ||
| messages: The original messages sent to the model | ||
| response: The model response | ||
| custom_api_used: Whether a custom API key was used | ||
| metadata: Additional metadata for tracking | ||
| connection_pool: Connection pool for testing purposes | ||
| """ | ||
| # Track Prometheus metrics | ||
| if response.usage and response.usage.total_tokens > 0: | ||
| total_tokens_per_user.labels(str(developer_id)).inc( | ||
| amount=response.usage.total_tokens | ||
| ) | ||
|
|
||
| # Track usage in database | ||
| await track_usage( | ||
| developer_id=developer_id, | ||
| model=model, | ||
| messages=messages, | ||
| response=response, | ||
| custom_api_used=custom_api_used, | ||
| metadata=metadata, | ||
| connection_pool=connection_pool, | ||
| ) | ||
|
|
||
|
|
||
| @beartype | ||
| async def track_streaming_usage( | ||
| *, | ||
| developer_id: UUID, | ||
| model: str, | ||
| messages: list[dict], | ||
| usage_data: dict[str, Any] | None, | ||
| collected_output: list[dict], | ||
| response_id: str, | ||
| custom_api_used: bool = False, | ||
| metadata: dict[str, Any] = {}, | ||
abhinav-1305 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| connection_pool: Any = None, | ||
| ) -> None: | ||
| """ | ||
| Tracks usage for streaming responses. | ||
| Args: | ||
| developer_id: The developer ID for usage tracking | ||
| model: The model name used for the response | ||
| messages: The original messages sent to the model | ||
| usage_data: Usage data from the streaming response | ||
| collected_output: The complete collected output from streaming | ||
| response_id: The response ID | ||
| custom_api_used: Whether a custom API key was used | ||
| metadata: Additional metadata for tracking | ||
| connection_pool: Connection pool for testing purposes | ||
| """ | ||
| # Track Prometheus metrics if usage data is available | ||
| if usage_data and usage_data.get("total_tokens", 0) > 0: | ||
| total_tokens_per_user.labels(str(developer_id)).inc( | ||
| amount=usage_data.get("total_tokens", 0) | ||
| ) | ||
|
|
||
| # Track usage in database | ||
| await track_usage( | ||
| developer_id=developer_id, | ||
| model=model, | ||
| messages=messages, | ||
| response=ModelResponse( | ||
| id=response_id, | ||
| choices=[ | ||
| Choices( | ||
| message=Message( | ||
| content=choice.get("content", ""), | ||
| tool_calls=choice.get("tool_calls"), | ||
| ), | ||
| ) | ||
| for choice in collected_output | ||
| ], | ||
| usage=usage_data, | ||
| ), | ||
| custom_api_used=custom_api_used, | ||
| metadata=metadata, | ||
| connection_pool=connection_pool, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.