Repository navigation
Description
首先感谢作者开发的这个工具,他很有意思,我把遇到的问题向您反馈下。
Background
I discovered this ccusage tool on X.com and found it very meaningful for tracking Claude usage. However, I encountered some issues during local testing that prevented the tool from working properly.
Given my limited technical expertise, I sought assistance from AI to help diagnose the problem. The AI provided some analysis and conclusions, but I'm not entirely certain about the accuracy of its findings. I'd appreciate it if you could review the investigation and let me know your thoughts.
Problem
ccusage becomes completely unusable when the LiteLLM API service is down, failing with "fetch failed" error during pricing data retrieval.
Error Details
ERROR Failed to fetch model pricing: fetch failed
Error: Could not fetch model pricing data
at fetchModelPricing (pricing-fetcher.js:33:9)
Root Cause Investigation
Network connectivity test: ✅ Working fine
$ curl -I https://www.google.com # SuccessLiteLLM API status check: ❌ Service unavailable
$ curl -I https://api.litellm.ai/
HTTP/2 503
server: cloudflare
x-render-routing: suspend
$ curl -I https://api.litellm.ai/pricing
HTTP/2 503Conclusion: The issue is caused by LiteLLM API returning HTTP 503 errors, not user configuration problems.
Impact
- ccusage becomes completely unusable during LiteLLM service outages
- Users cannot analyze their Claude usage data at all
- Tool has single point of failure dependency on external service
Proposed Solution: Multi-Source Pricing Fallback
Implement a robust fallback chain to eliminate dependency on single external service:
Fallback Chain Design
1. LiteLLM API (primary)
↓ (on 503/timeout)
2. Anthropic official pricing API
↓ (on failure)
3. Static Claude pricing file (GitHub/CDN)
↓ (on failure)
4. Local cached pricing data
↓ (on failure)
5. Built-in Claude pricing table
Implementation Benefits
- ✅ Eliminates single point of failure
- ✅ Always functional, even when external APIs are down
- ✅ Uses most current pricing when available
- ✅ Graceful degradation with user warnings
Configuration Options
# Manual source selection
ccusage --pricing-source anthropic
ccusage --pricing-source cache
ccusage --pricing-source builtin
# Fallback mode indicators
ccusage daily # Shows: "⚠️ Using cached pricing (LiteLLM API unavailable)"
# Tokens-only mode
ccusage --no-pricing # Skip cost calculation entirelyAdditional Improvements
- Local caching: Store successful API responses with TTL
- Retry logic: Exponential backoff for temporary failures
- Status indicators: Clear warnings when using fallback data
- User overrides: Allow manual pricing configuration
Alternative Data Sources
- Anthropic: Official Claude API pricing endpoints
- Static JSON: Community-maintained Claude pricing on GitHub/jsDelivr
- Vendor pages: Scrape Anthropic's official pricing pages as last resort
Expected User Experience
# Current (broken)
$ ccusage daily
ERROR: Failed to fetch model pricing
# Improved (resilient)
$ ccusage daily
⚠️ LiteLLM API unavailable, using cached pricing data (2 days old)
[Shows usage report successfully]This architecture change would make ccusage much more reliable and user-friendly, ensuring it works even when external dependencies fail.
Activity
evergood2025 commentedon Jun 6, 2025
I see that OpenRouter also provides a public pricing query API endpoint.
API endpoint:
https://openrouter.ai/api/v1/modelsI'm wondering if this could be helpful for this project.
Pricing Data Structure:
Each model returns pricing information including:
evergood2025 commentedon Jun 6, 2025
Finally, by cloning the code locally and having Claude Code analyze it, I discovered the real cause of the problem. It is essentially still a network issue (mainland China network restrictions). Although I configured a proxy, it seems that for some reason the proxy cannot be used. The following is the resolution process organized by AI:
Problem
When running ccusage, encountered network connection error:
ERROR Failed to fetch model pricing: fetch failed
The tool was unable to access the LiteLLM pricing API at:
https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_
context_window.json
Root Cause
environments
with the request
disabled
Solution
Modified the pricing URL to use a GitHub mirror service:
Original URL:
https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json
Mirror URL:
https://ghfast.top/https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json
Code Change:
In src/pricing-fetcher.ts, line 14:
const LITELLM_PRICING_URL =
"https://ghfast.top/https://raw.githubusercontent.com/BerriAI/litellm/
main/model_prices_and_context_window.json";
Result
Alternative Workarounds
For users experiencing similar issues:
ryoppippi commentedon Jun 6, 2025
Hi
I know China has a complicated Internet network issue.
So if you have any solutions to solve this problem, just send a PR.
ryoppippi commentedon Jun 7, 2025
@yonghao2011
My questions are
ryoppippi commentedon Jun 7, 2025
Also openrouter uses different model name.... 🤔
yfzhou0904 commentedon Jun 12, 2025
Looks like the key issue here is issue's author find that they cannot let ccusage use proxy to get past GFW.
Submitted a PR to fetch using proxy if HTTP(S)_PROXY env var is configured.
iamwrm commentedon Jun 13, 2025
#51 this PR adds support for
--fetch local_model_price.jsonThen you can download and prepare the data file using whatever proxy you want
refactor(adapter/copilot): drop unused _event_id param + trim 3 multi…
refactor(adapter/copilot): drop unused _event_id param + trim 3 multi…
refactor(adapter/copilot): drop unused _event_id param + trim 3 multi…
refactor(adapter/copilot): drop unused _event_id param + trim 3 multi…
refactor(adapter/copilot): drop unused _event_id param + trim 3 multi…
refactor(adapter/copilot): drop unused _event_id param + trim 3 multi…