Skip to content

fix: filter zero-cost entries in usage list, add --api flag to list - #17

Merged
nazozokc merged 1 commit into
mainfrom
AI-agent
Jun 20, 2026
Merged

fix: filter zero-cost entries in usage list, add --api flag to list#17
nazozokc merged 1 commit into
mainfrom
AI-agent

Conversation

@nazozokc

@nazozokc nazozokc commented Jun 20, 2026

Copy link
Copy Markdown
Owner

Changes

  • usage list: Now filters out zero-cost entries (token-only records). Only entries with actual charges (cost > 0) are displayed. Output format migrated from fixed-width text to cli-table3 for consistency with list command.

  • list --api / list -a: New flag to include current month's LLM API usage summary after the subscription table. Shows per-provider breakdown and total cost.

Files changed

File Change
src/types.ts Added minCost to GetLlmUsageOptions
src/db.ts Added cost >= ? SQL filter for getLlmUsage()
src/index.ts Added --api flag to list command
src/commands.ts handleList now displays API usage when --api set
src/display.ts Added renderUsageTable() and showApiUsage()
src/usage.ts handleUsageList always filters minCost: 0.01, uses cli-table3
src/commands.test.ts Updated test assertions for new output format

Verification

  • pnpm build — ✅
  • pnpm test — ✅ (175 passed)

Summary by CodeRabbit

  • New Features
    • Added --api / -a flag to the list command to display LLM API usage totals for the current month, including per-provider breakdowns.
    • Enhanced usage table rendering with improved styling and better formatting for usage entries.

@coderabbitai

coderabbitai Bot commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds an optional minCost filter to the LLM usage DB query, introduces renderUsageTable and showApiUsage display functions in display.ts, refactors handleUsageList to use renderUsageTable with a minCost: 0.01 threshold, and extends handleList with an --api flag that fetches and renders current-month LLM usage totals.

Changes

LLM Usage Display and list --api Integration

Layer / File(s) Summary
minCost filter type and DB predicate
subtrack/src/types.ts, subtrack/src/db.ts
GetLlmUsageOptions gains minCost?: number, and getLlmUsage conditionally appends cost >= ? to its WHERE clause using that value.
renderUsageTable and showApiUsage rendering functions
subtrack/src/display.ts
Imports LlmUsageEntry; adds renderUsageTable which renders a terminal-width-constrained per-entry table with a bold "Total" footer row (or an info log when empty); adds showApiUsage which renders a per-provider cost summary for a labeled period.
handleUsageList refactor, handleList --api flag, CLI wiring, and tests
subtrack/src/usage.ts, subtrack/src/commands.ts, subtrack/src/index.ts, subtrack/src/commands.test.ts
handleUsageList applies minCost: 0.01 and delegates rendering to renderUsageTable; handleList gains api?: boolean, computes the current month's date range, fetches totals via getLlmUsageTotal/getLlmUsageTotalByProvider, and calls showApiUsage; the list CLI command gains --api/-a; tests update expected strings to "No paid usage entries" and "Total".

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • nazozokc/subtrack#13: Extends the same handleList function signature and list command argument schema, adding sort/desc options in the same pattern used here for the api flag.

Poem

🐇 Hop, hop, the tokens are counted at last,
A table of costs rendered bright and fast,
minCost filters out the tiniest fee,
--api flag shows what the LLMs cost thee,
Bold "Total" at the bottom, neat and true —
This bunny approves of the terminal view! 🌟

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes both main changes: filtering zero-cost entries in the usage list and adding the --api flag to the list command.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch AI-agent

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
subtrack/src/display.ts (1)

344-471: ⚡ Quick win

Add JSDoc to newly exported display APIs.

Lines 344 and 420 introduce public exported functions without JSDoc. Please add concise API docs for parameters and behavior.

📝 Suggested doc shape
+/**
+ * Render paid LLM usage entries as a formatted table with a total footer.
+ */
 export function renderUsageTable(entries: LlmUsageEntry[]): void {
   ...
 }

+/**
+ * Show current-period API usage summary by provider and total.
+ */
 export function showApiUsage(
   total: number,
   byProvider: { provider: string; total: number }[],
   periodLabel: string,
 ): void {
   ...
 }

As per coding guidelines, subtrack/**/*.{js,jsx,ts,tsx}: Document public APIs with JSDoc comments in JavaScript/TypeScript.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@subtrack/src/display.ts` around lines 344 - 471, The exported functions
renderUsageTable and showApiUsage are missing JSDoc documentation. Add JSDoc
comments above each function to document the purpose, parameters, and return
types. For renderUsageTable, document the entries parameter of type
LlmUsageEntry[] and note that it renders to console with no return value. For
showApiUsage, document the total, byProvider, and periodLabel parameters and
their types, along with the void return type and that it displays API usage
summary to console.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@subtrack/src/db.ts`:
- Around line 424-427: The GetLlmUsageOptions type definition (lines 398-404) is
missing the minCost property that is being accessed in the condition check at
line 424. Add the minCost property to the GetLlmUsageOptions interface or type
definition, declaring it as an optional property with an appropriate type
(likely optional number) to match how it is being used in the conditional check
with options?.minCost.

In `@subtrack/src/usage.ts`:
- Line 196: The minCost threshold value of 0.01 is too high and filters out
legitimate paid entries with fractional cents (between 0 and 0.01), not just
zero-cost entries. Change the minCost value to 0 (or use a minimal epsilon
value) to only exclude truly zero-cost entries while preserving all positive
cost entries, regardless of their fractional cent values.

---

Nitpick comments:
In `@subtrack/src/display.ts`:
- Around line 344-471: The exported functions renderUsageTable and showApiUsage
are missing JSDoc documentation. Add JSDoc comments above each function to
document the purpose, parameters, and return types. For renderUsageTable,
document the entries parameter of type LlmUsageEntry[] and note that it renders
to console with no return value. For showApiUsage, document the total,
byProvider, and periodLabel parameters and their types, along with the void
return type and that it displays API usage summary to console.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 82e1ada9-9f5e-4a7f-867c-c59483872646

📥 Commits

Reviewing files that changed from the base of the PR and between 014c518 and b4bb1b4.

📒 Files selected for processing (7)
  • subtrack/src/commands.test.ts
  • subtrack/src/commands.ts
  • subtrack/src/db.ts
  • subtrack/src/display.ts
  • subtrack/src/index.ts
  • subtrack/src/types.ts
  • subtrack/src/usage.ts

Comment thread subtrack/src/db.ts
Comment on lines +424 to +427
if (options?.minCost !== undefined) {
conditions.push("cost >= ?")
params.push(options.minCost)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Verify local type shape and minCost usage in db.ts + shared type in types.ts"
rg -n 'export type GetLlmUsageOptions|minCost' subtrack/src/db.ts subtrack/src/types.ts

Repository: nazozokc/subtrack

Length of output: 419


🏁 Script executed:

sed -n '398,410p' subtrack/src/db.ts

Repository: nazozokc/subtrack

Length of output: 352


minCost is used without being declared on this file's options type.

Line 424 accesses options?.minCost, but the local GetLlmUsageOptions type (lines 398–404) does not declare minCost. This breaks the type contract and fails strict TypeScript checks.

💡 Proposed fix
 export type GetLlmUsageOptions = {
   provider?: string
   from?: string
   to?: string
   limit?: number
   offset?: number
+  minCost?: number
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (options?.minCost !== undefined) {
conditions.push("cost >= ?")
params.push(options.minCost)
}
export type GetLlmUsageOptions = {
provider?: string
from?: string
to?: string
limit?: number
offset?: number
minCost?: number
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@subtrack/src/db.ts` around lines 424 - 427, The GetLlmUsageOptions type
definition (lines 398-404) is missing the minCost property that is being
accessed in the condition check at line 424. Add the minCost property to the
GetLlmUsageOptions interface or type definition, declaring it as an optional
property with an appropriate type (likely optional number) to match how it is
being used in the conditional check with options?.minCost.

Source: Coding guidelines

Comment thread subtrack/src/usage.ts
from: options.from,
to: options.to,
limit: 100,
minCost: 0.01,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

minCost: 0.01 can hide real paid usage entries.

Line 196 filters out any entry below 0.01 cents. Since cost supports fractional cents, positive (paid) entries between 0 and 0.01 are excluded even though they are not zero-cost.

💡 Safer threshold for “exclude only zero-cost” behavior
-    minCost: 0.01,
+    minCost: Number.EPSILON,
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
minCost: 0.01,
minCost: Number.EPSILON,
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@subtrack/src/usage.ts` at line 196, The minCost threshold value of 0.01 is
too high and filters out legitimate paid entries with fractional cents (between
0 and 0.01), not just zero-cost entries. Change the minCost value to 0 (or use a
minimal epsilon value) to only exclude truly zero-cost entries while preserving
all positive cost entries, regardless of their fractional cent values.

@nazozokc
nazozokc merged commit 55b30ca into main Jun 20, 2026
10 checks passed
@nazozokc
nazozokc deleted the AI-agent branch June 20, 2026 04:26
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.

1 participant