-
Notifications
You must be signed in to change notification settings - Fork 22
Add token usage tracker #46
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a77dc13
Add token usage tracker
lingtonglu 0b7586e
fix the integration tests
lingtonglu ce65fbe
undo the key clear when deleting the keys
lingtonglu 7f57747
Merge branch 'main' of https://github.com/lingtonglu/forge into usage…
lingtonglu 3c71bee
fix the unit tests
lingtonglu 904ad6b
Merge branch 'main' of https://github.com/lingtonglu/forge into usage…
lingtonglu 3137d61
delete unncessary test files
lingtonglu 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
44 changes: 44 additions & 0 deletions
44
alembic/versions/4a685a55c5cd_create_usage_tracker_table.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,44 @@ | ||
| """create usage tracker table | ||
|
|
||
| Revision ID: 4a685a55c5cd | ||
| Revises: 9daf34d338f7 | ||
| Create Date: 2025-08-02 12:29:07.955645 | ||
|
|
||
| """ | ||
| from alembic import op | ||
| import sqlalchemy as sa | ||
| from sqlalchemy.dialects.postgresql import UUID | ||
| import uuid | ||
|
|
||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = '4a685a55c5cd' | ||
| down_revision = '9daf34d338f7' | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| op.create_table( | ||
| "usage_tracker", | ||
| sa.Column("id", UUID(as_uuid=True), nullable=False, primary_key=True, default=uuid.uuid4), | ||
| sa.Column("user_id", sa.Integer(), nullable=False), | ||
| sa.Column("provider_key_id", sa.Integer(), nullable=False), | ||
| sa.Column("forge_key_id", sa.Integer(), nullable=False), | ||
| sa.Column("model", sa.String(), nullable=True), | ||
| sa.Column("endpoint", sa.String(), nullable=True), | ||
| sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), | ||
| sa.Column("updated_at", sa.DateTime(timezone=True), nullable=True), | ||
| sa.Column("input_tokens", sa.Integer(), nullable=True), | ||
| sa.Column("output_tokens", sa.Integer(), nullable=True), | ||
| sa.Column("cached_tokens", sa.Integer(), nullable=True), | ||
| sa.Column("reasoning_tokens", sa.Integer(), nullable=True), | ||
|
|
||
| sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"), | ||
| sa.ForeignKeyConstraint(["provider_key_id"], ["provider_keys.id"], ondelete="CASCADE"), | ||
| sa.ForeignKeyConstraint(["forge_key_id"], ["forge_api_keys.id"], ondelete="CASCADE"), | ||
| ) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| op.drop_table("usage_tracker") |
28 changes: 28 additions & 0 deletions
28
alembic/versions/831fc2cf16ee_enable_soft_deletion_for_provider_keys_.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,28 @@ | ||
| """enable soft deletion for provider keys and api keys | ||
|
|
||
| Revision ID: 831fc2cf16ee | ||
| Revises: 4a685a55c5cd | ||
| Create Date: 2025-08-02 17:50:12.224293 | ||
|
|
||
| """ | ||
| from alembic import op | ||
| import sqlalchemy as sa | ||
|
|
||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = '831fc2cf16ee' | ||
| down_revision = '4a685a55c5cd' | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| op.add_column('provider_keys', sa.Column('deleted_at', sa.DateTime(timezone=True), nullable=True)) | ||
| op.add_column('forge_api_keys', sa.Column('deleted_at', sa.DateTime(timezone=True), nullable=True)) | ||
| op.alter_column('forge_api_keys', 'key', nullable=True) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| op.drop_column('provider_keys', 'deleted_at') | ||
| op.drop_column('forge_api_keys', 'deleted_at') | ||
| op.alter_column('forge_api_keys', 'key', nullable=False) | ||
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 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why does "key" can be nullable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the user is trying to deleted this record. I would like to wipe out the key value to be None so there is no security issue. We only keep the id/name and the created/deleted timestamp for reference check.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same goes for provider keys, I would wipe out the
encrypted_api_keyvalue there. Probablybase_urlas well?