Skip to content

Conversation

@sumansaurabh
Copy link

Summary

Adds a new /usage command that allows users to query their current API usage status and remaining quota in real-time.

Changes

  • Implemented /usage command handler
  • Added API integration to fetch usage statistics
  • Created user-friendly display format for quota information
  • Added error handling for API failures

Why

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

Copilot AI review requested due to automatic review settings November 9, 2025 00:21
Copy link
Contributor

Copilot AI left a 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 /usage that 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.

Comment on lines +289 to +298
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)
Copy link

Copilot AI Nov 9, 2025

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.

Suggested change
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))

Copilot uses AI. Check for mistakes.
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)
Copy link

Copilot AI Nov 9, 2025

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.

Suggested change
quota = usage_data.get("total_quota") or usage_data.get("quota", 0)
quota = usage_data.get("total_quota", usage_data.get("quota", 0))

Copilot uses AI. Check for mistakes.

# 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)
Copy link

Copilot AI Nov 9, 2025

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.

Suggested change
usage = usage_data.get("total_usage") or usage_data.get("usage", 0)

Copilot uses AI. Check for mistakes.
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")
Copy link

Copilot AI Nov 9, 2025

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.

Suggested change
reset = usage_data.get("reset_date") or usage_data.get("reset_time")
reset = usage_data.get("reset_date", usage_data.get("reset_time"))

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: add command to query remaining usage count

2 participants