-
Notifications
You must be signed in to change notification settings - Fork 300
feat: add /usage command to query API usage and quota information #201
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
base: main
Are you sure you want to change the base?
feat: add /usage command to query API usage and quota information #201
Conversation
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.
Pull Request Overview
This PR implements a new /usage meta command that allows users to check their API usage and quota information from within the Kimi CLI shell.
- Added a new async meta command
/usagethat queries the API provider's usage endpoint - Implemented comprehensive error handling for various failure scenarios (no config, auth errors, network errors)
- Created a full test suite with 6 test cases covering different scenarios
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/kimi_cli/ui/shell/metacmd.py | Implements the new /usage command with API querying, error handling, and formatted table output |
| tests/test_usage_metacmd.py | Adds comprehensive test coverage for the usage command including edge cases and error scenarios |
| USAGE_COMMAND_IMPLEMENTATION.md | Documents the implementation details, features, and usage examples for the new command |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| usage = usage_data.get("total_usage") or usage_data.get("usage", 0) | ||
| table.add_row("Current Usage", f"{usage:,}") | ||
|
|
||
| if "total_quota" in usage_data or "quota" in usage_data: | ||
| quota = usage_data.get("total_quota") or usage_data.get("quota", 0) | ||
| table.add_row("Total Quota", f"{quota:,}") | ||
|
|
||
| # Calculate remaining and percentage if we have both usage and quota | ||
| if "total_usage" in usage_data or "usage" in usage_data: | ||
| usage = usage_data.get("total_usage") or usage_data.get("usage", 0) |
Copilot
AI
Nov 9, 2025
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 expression usage_data.get('total_usage') or usage_data.get('usage', 0) will incorrectly treat a total_usage value of 0 as falsy and fall back to checking usage. This could display the wrong field when total_usage is 0. Use usage_data.get('total_usage', usage_data.get('usage', 0)) instead to preserve 0 values.
| usage = usage_data.get("total_usage") or usage_data.get("usage", 0) | |
| table.add_row("Current Usage", f"{usage:,}") | |
| if "total_quota" in usage_data or "quota" in usage_data: | |
| quota = usage_data.get("total_quota") or usage_data.get("quota", 0) | |
| table.add_row("Total Quota", f"{quota:,}") | |
| # Calculate remaining and percentage if we have both usage and quota | |
| if "total_usage" in usage_data or "usage" in usage_data: | |
| usage = usage_data.get("total_usage") or usage_data.get("usage", 0) | |
| usage = usage_data.get("total_usage", usage_data.get("usage", 0)) | |
| table.add_row("Current Usage", f"{usage:,}") | |
| if "total_quota" in usage_data or "quota" in usage_data: | |
| quota = usage_data.get("total_quota", usage_data.get("quota", 0)) | |
| table.add_row("Total Quota", f"{quota:,}") | |
| # Calculate remaining and percentage if we have both usage and quota | |
| if "total_usage" in usage_data or "usage" in usage_data: | |
| usage = usage_data.get("total_usage", usage_data.get("usage", 0)) |
| table.add_row("Current Usage", f"{usage:,}") | ||
|
|
||
| if "total_quota" in usage_data or "quota" in usage_data: | ||
| quota = usage_data.get("total_quota") or usage_data.get("quota", 0) |
Copilot
AI
Nov 9, 2025
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.
Same issue as with usage: the expression usage_data.get('total_quota') or usage_data.get('quota', 0) will incorrectly treat a total_quota value of 0 as falsy and fall back to checking quota. Use usage_data.get('total_quota', usage_data.get('quota', 0)) instead.
| quota = usage_data.get("total_quota") or usage_data.get("quota", 0) | |
| quota = usage_data.get("total_quota", usage_data.get("quota", 0)) |
|
|
||
| # Calculate remaining and percentage if we have both usage and quota | ||
| if "total_usage" in usage_data or "usage" in usage_data: | ||
| usage = usage_data.get("total_usage") or usage_data.get("usage", 0) |
Copilot
AI
Nov 9, 2025
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.
This duplicates the logic from lines 288-290. The usage variable should be reused from the earlier calculation instead of recalculating it here, which could lead to inconsistencies if the logic is updated in only one place.
| usage = usage_data.get("total_usage") or usage_data.get("usage", 0) |
| table.add_row("Usage Percentage", f"{percentage:.2f}%") | ||
|
|
||
| if "reset_date" in usage_data or "reset_time" in usage_data: | ||
| reset = usage_data.get("reset_date") or usage_data.get("reset_time") |
Copilot
AI
Nov 9, 2025
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 expression usage_data.get('reset_date') or usage_data.get('reset_time') will incorrectly treat an empty string reset_date value as falsy. If reset_date is an empty string and reset_time has a value, it will incorrectly display reset_time. Consider using usage_data.get('reset_date', usage_data.get('reset_time')) instead.
| reset = usage_data.get("reset_date") or usage_data.get("reset_time") | |
| reset = usage_data.get("reset_date", usage_data.get("reset_time")) |
Summary
Adds a new
/usagecommand that allows users to query their current API usage status and remaining quota in real-time.Changes
/usagecommand handlerWhy
Users previously had no convenient way to check their API usage status and remaining quota, making it difficult to monitor their consumption and plan usage accordingly.
Fixes #186
Agent: blackbox
Model: blackboxai/blackbox-pro