CLAUDE.md - Internal Package
This package contains shared internal utilities for the ccusage monorepo.
Overview
CLAUDE.md - Internal Package
This package contains shared internal utilities for the ccusage monorepo.
Package Overview
Name: @ccusage/internal
Description: Shared internal utilities for ccusage toolchain
Type: Internal library (private package)
Important Notes
CRITICAL: This is an internal package that gets bundled into the final applications. Therefore:
- Always add this package as a
devDependencyin apps that use it, NOT as a regular dependency - Apps in this monorepo (ccusage, mcp, codex) are bundled CLIs, so all their runtime dependencies should be in
devDependencies - The bundler will include the code from this package in the final output
Available Exports
Utilities:
./pricing- LiteLLM pricing fetcher and utilities./pricing-fetch-utils- Pricing fetch helper functions./logger- Logger factory using consola with LOG_LEVEL support./format- Number formatting utilities (formatTokens, formatCurrency)./constants- Shared constants (DEFAULT_LOCALE, MILLION)
Development Commands
pnpm run test- Run testspnpm run lint- Lint codepnpm run format- Format and auto-fix codepnpm typecheck- Type check with TypeScript
Adding New Utilities
When adding new shared utilities:
-
Create the utility file in
src/ -
Add the export to
package.jsonexports field -
Import in consuming apps as
devDependencies:"devDependencies": { "@ccusage/internal": "workspace:*" } -
Use the utility:
Dependencies
This package has minimal runtime dependencies that get bundled:
@praha/byethrow- Functional error handlingconsola- Loggingvalibot- Schema validation
Pricing Implementation Notes
Tiered Pricing Support
LiteLLM supports tiered pricing for large context window models. Not all models use tiered pricing:
Models WITH tiered pricing:
-
Claude/Anthropic models: 200k token threshold
- Fields:
input_cost_per_token_above_200k_tokens,output_cost_per_token_above_200k_tokens - Cache fields:
cache_creation_input_token_cost_above_200k_tokens,cache_read_input_token_cost_above_200k_tokens - ✅ Currently implemented in cost calculation logic
- Fields:
-
Gemini models: 128k token threshold
- Fields:
input_cost_per_token_above_128k_tokens,output_cost_per_token_above_128k_tokens - ⚠️ Schema supports these fields but calculation logic NOT implemented
- Would require different threshold handling if Gemini support is added
- Fields:
Models WITHOUT tiered pricing:
- GPT/OpenAI models: Flat rate pricing (no token-based tiers)
- Note: OpenAI has "tier levels" but these are for API rate limits, not pricing
⚠️ IMPORTANT for Future Development
When adding support for new models:
- Check if the model has tiered pricing in LiteLLM's schema
- Verify the threshold value (200k for Claude, 128k for Gemini, etc.)
- Update calculation logic if threshold differs from currently implemented 200k
- Add comprehensive tests for boundary conditions at the threshold
- Document the pricing structure in relevant CLAUDE.md files
- If cache-specific rates are missing, fall back to the corresponding input rates (base and above-threshold) to avoid under-charging cached tokens
The current implementation in pricing.ts only handles 200k threshold. Adding models with different thresholds would require refactoring the calculateTieredCost helper function.
Code Style
Follow the same conventions as the main ccusage package:
- Use
.tsextensions for local imports - Prefer
@praha/byethrow Resulttype over try-catch - Only export what's actually used by other modules
- Use vitest in-source testing with
if (import.meta.vitest != null)blocks