claude_usage 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rubocop.yml +74 -0
- data/CHANGELOG.md +53 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +212 -0
- data/Rakefile +8 -0
- data/app/controllers/claude_usage/application_controller.rb +11 -0
- data/app/controllers/claude_usage/usage_controller.rb +55 -0
- data/app/helpers/claude_usage/usage_helper.rb +20 -0
- data/app/views/claude_usage/usage/index.html.erb +298 -0
- data/config/routes.rb +6 -0
- data/lib/claude_usage/cost_calculator.rb +57 -0
- data/lib/claude_usage/engine.rb +23 -0
- data/lib/claude_usage/file_reader.rb +112 -0
- data/lib/claude_usage/formatters.rb +32 -0
- data/lib/claude_usage/litellm_pricing_fetcher.rb +148 -0
- data/lib/claude_usage/project_name_resolver.rb +23 -0
- data/lib/claude_usage/usage_aggregator.rb +55 -0
- data/lib/claude_usage/version.rb +5 -0
- data/lib/claude_usage.rb +52 -0
- data/lib/generators/claude_usage/install_generator.rb +30 -0
- data/lib/generators/claude_usage/templates/initializer.rb +34 -0
- data/lib/tasks/claude_usage_tasks.rake +313 -0
- data/sig/claude_usage.rbs +4 -0
- metadata +139 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 8eb8b5aad49b48df6c4b6453edb62b8a150c3e90d435c4676799b52175c0f64f
|
|
4
|
+
data.tar.gz: fd710be1cc9263d734f9bb7873132ce2af550e9fc46cedfd451d02a8f577830f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: c4ed073e2979c9847b30f2c174282bed63e17dea0848837a8fc302463fbabde58626e7306c761d8005e50bef3dbe3875c95f47be9d0d869ade28de94eb5ba6e1
|
|
7
|
+
data.tar.gz: e82b2b96f3a4e0905aea7a7738b5872eebb7ff433abd39142bd2e901a6abf8bf9a5004e83a3a00a8f7046b7c104038eee5d4a4ba2ee0d7f07d65de248cbcee91
|
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
AllCops:
|
|
2
|
+
TargetRubyVersion: 3.2
|
|
3
|
+
NewCops: enable
|
|
4
|
+
SuggestExtensions: false
|
|
5
|
+
Exclude:
|
|
6
|
+
- 'bin/**/*'
|
|
7
|
+
- 'vendor/**/*'
|
|
8
|
+
- 'node_modules/**/*'
|
|
9
|
+
|
|
10
|
+
# Gem-specific configurations
|
|
11
|
+
Gemspec/DevelopmentDependencies:
|
|
12
|
+
Enabled: false
|
|
13
|
+
|
|
14
|
+
# Style preferences
|
|
15
|
+
Style/StringLiterals:
|
|
16
|
+
EnforcedStyle: single_quotes
|
|
17
|
+
|
|
18
|
+
Style/FrozenStringLiteralComment:
|
|
19
|
+
Enabled: true
|
|
20
|
+
|
|
21
|
+
Style/Documentation:
|
|
22
|
+
Enabled: false # We have separate documentation
|
|
23
|
+
|
|
24
|
+
# Metrics - reasonable defaults for gem code
|
|
25
|
+
Metrics/MethodLength:
|
|
26
|
+
Max: 20
|
|
27
|
+
Exclude:
|
|
28
|
+
- 'lib/tasks/**/*'
|
|
29
|
+
- 'lib/claude_usage/litellm_pricing_fetcher.rb'
|
|
30
|
+
- 'lib/claude_usage/cost_calculator.rb'
|
|
31
|
+
- 'lib/claude_usage/file_reader.rb'
|
|
32
|
+
|
|
33
|
+
Metrics/BlockLength:
|
|
34
|
+
Exclude:
|
|
35
|
+
- 'spec/**/*'
|
|
36
|
+
- 'lib/tasks/**/*'
|
|
37
|
+
- 'Rakefile'
|
|
38
|
+
- '*.gemspec'
|
|
39
|
+
|
|
40
|
+
Metrics/AbcSize:
|
|
41
|
+
Max: 20
|
|
42
|
+
Exclude:
|
|
43
|
+
- 'lib/tasks/**/*'
|
|
44
|
+
- 'lib/claude_usage/litellm_pricing_fetcher.rb'
|
|
45
|
+
- 'lib/claude_usage/cost_calculator.rb'
|
|
46
|
+
- 'lib/claude_usage/usage_aggregator.rb'
|
|
47
|
+
|
|
48
|
+
Metrics/CyclomaticComplexity:
|
|
49
|
+
Exclude:
|
|
50
|
+
- 'lib/claude_usage/litellm_pricing_fetcher.rb'
|
|
51
|
+
- 'lib/claude_usage/file_reader.rb'
|
|
52
|
+
- 'lib/claude_usage/usage_aggregator.rb'
|
|
53
|
+
|
|
54
|
+
Metrics/PerceivedComplexity:
|
|
55
|
+
Exclude:
|
|
56
|
+
- 'lib/claude_usage/litellm_pricing_fetcher.rb'
|
|
57
|
+
- 'lib/claude_usage/usage_aggregator.rb'
|
|
58
|
+
|
|
59
|
+
Metrics/ClassLength:
|
|
60
|
+
Exclude:
|
|
61
|
+
- 'lib/claude_usage/litellm_pricing_fetcher.rb'
|
|
62
|
+
|
|
63
|
+
Style/MultilineBlockChain:
|
|
64
|
+
Enabled: false
|
|
65
|
+
|
|
66
|
+
# Layout
|
|
67
|
+
Layout/LineLength:
|
|
68
|
+
Max: 120
|
|
69
|
+
Exclude:
|
|
70
|
+
- 'spec/**/*'
|
|
71
|
+
|
|
72
|
+
# Naming
|
|
73
|
+
Naming/VariableNumber:
|
|
74
|
+
Enabled: false
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.1.2] - 2025-12-23
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- Initial release of claude_usage gem
|
|
14
|
+
- Rails Engine for automatic route mounting
|
|
15
|
+
- Service objects for reading Claude Code JSONL files
|
|
16
|
+
- LiteLLM pricing integration with automatic fetching and caching
|
|
17
|
+
- Cost calculator with tiered pricing support (200k+ tokens)
|
|
18
|
+
- Usage aggregator for daily and total usage statistics
|
|
19
|
+
- Web UI with modern, responsive design
|
|
20
|
+
- JSON API endpoint for programmatic access
|
|
21
|
+
- Rake tasks for CLI usage reports
|
|
22
|
+
- Rails generator for easy installation
|
|
23
|
+
- Configuration system with sensible defaults
|
|
24
|
+
- Support for cache creation and read token tracking
|
|
25
|
+
- Deduplication of usage entries
|
|
26
|
+
- Offline mode for working without internet access
|
|
27
|
+
- Comprehensive documentation and guides
|
|
28
|
+
- ProjectNameResolver module for shared project name detection
|
|
29
|
+
- Formatters module for consistent number and currency formatting
|
|
30
|
+
- UsageHelper for view formatting methods
|
|
31
|
+
- RuboCop enforcement in CI pipeline
|
|
32
|
+
- Rubocop executable in bin directory
|
|
33
|
+
|
|
34
|
+
### Security
|
|
35
|
+
- Path traversal vulnerability protection in FileReader
|
|
36
|
+
- Project name validation to prevent directory traversal attacks
|
|
37
|
+
- Sanitized exception messages in controller to prevent information leakage
|
|
38
|
+
- Input validation for all file operations
|
|
39
|
+
- User-friendly error messages that don't expose internal details
|
|
40
|
+
|
|
41
|
+
### Features
|
|
42
|
+
- Automatic detection of Rails app name for project tracking
|
|
43
|
+
- Multi-path support for Claude Code directories
|
|
44
|
+
- Configurable pricing cache expiry
|
|
45
|
+
- Beautiful HTML reports with gradient styling
|
|
46
|
+
- Centralized number formatting with comma separators
|
|
47
|
+
- Model badge display in reports
|
|
48
|
+
- Robust error handling with detailed server-side logging
|
|
49
|
+
- DRY code with shared utilities for formatting and project detection
|
|
50
|
+
|
|
51
|
+
[Unreleased]: https://github.com/Shoebtamboli/claude_usage/compare/v0.1.2...HEAD
|
|
52
|
+
[0.1.2]: https://github.com/Shoebtamboli/claude_usage/releases/tag/v0.1.2
|
|
53
|
+
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
|
10
|
+
identity and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
|
26
|
+
community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
|
31
|
+
any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
|
35
|
+
without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official email address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
[INSERT CONTACT METHOD].
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
|
86
|
+
actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
|
93
|
+
ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
|
113
|
+
community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.1, available at
|
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by
|
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
123
|
+
|
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
|
127
|
+
|
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Shoeb
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
# ClaudeUsage
|
|
2
|
+
|
|
3
|
+
[](https://github.com/Shoebtamboli/claude_usage/actions/workflows/main.yml)
|
|
4
|
+
[](https://badge.fury.io/rb/claude_usage)
|
|
5
|
+
[](https://codecov.io/gh/Shoebtamboli/claude_usage)
|
|
6
|
+
[](LICENSE.txt)
|
|
7
|
+
|
|
8
|
+
A Rails engine for tracking Claude Code token usage and costs in your Rails applications. Get real-time insights into your Claude Code usage with automatic cost calculation using LiteLLM pricing data.
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
✅ **Automatic Usage Tracking** - Reads Claude Code JSONL logs automatically
|
|
13
|
+
✅ **Auto-Detection** - Automatically detects project name from Rails.root
|
|
14
|
+
✅ **LiteLLM Pricing Integration** - Fetches up-to-date pricing from LiteLLM API
|
|
15
|
+
✅ **Tiered Pricing Support** - Handles 200k+ token tiered pricing
|
|
16
|
+
✅ **Cache Token Tracking** - Tracks cache creation and read tokens
|
|
17
|
+
✅ **Beautiful Web UI** - Modern, responsive interface for viewing usage
|
|
18
|
+
✅ **JSON API** - Programmatic access to usage data
|
|
19
|
+
✅ **Rake Tasks** - CLI commands for quick reports
|
|
20
|
+
✅ **Zero Configuration** - Works out of the box with sensible defaults
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
Add this line to your application's Gemfile in the development group:
|
|
25
|
+
|
|
26
|
+
```ruby
|
|
27
|
+
group :development do
|
|
28
|
+
gem 'claude_usage'
|
|
29
|
+
end
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
And then execute:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
bundle install
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
> **Note:** This gem is designed for development environments to track your Claude Code usage while building your application. It's recommended to keep it in the development group unless you need production usage tracking.
|
|
39
|
+
|
|
40
|
+
Or install it yourself as:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
gem install claude_usage
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Quick Start
|
|
47
|
+
|
|
48
|
+
After installing the gem, run the generator:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
rails generate claude_usage:install
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
**IMPORTANT:** Verify your setup immediately:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
rake claude_usage:show_project
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
This command will:
|
|
61
|
+
- Show which project is being tracked
|
|
62
|
+
- List all available Claude Code projects
|
|
63
|
+
- Indicate if data files exist
|
|
64
|
+
- Provide solutions if no data is found
|
|
65
|
+
|
|
66
|
+
If the project name doesn't match, update `config/initializers/claude_usage.rb`:
|
|
67
|
+
|
|
68
|
+
```ruby
|
|
69
|
+
ClaudeUsage.configure do |config|
|
|
70
|
+
config.project_name = "actual-project-name" # From show_project output
|
|
71
|
+
end
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Start your Rails server and visit:
|
|
75
|
+
|
|
76
|
+
```
|
|
77
|
+
http://localhost:3000/claude-usage
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Usage
|
|
81
|
+
|
|
82
|
+
### Web Interface
|
|
83
|
+
|
|
84
|
+
Visit `http://localhost:3000/claude-usage` to see:
|
|
85
|
+
- Total usage statistics
|
|
86
|
+
- Daily breakdown of token usage
|
|
87
|
+
- Cost calculations
|
|
88
|
+
- Model usage information
|
|
89
|
+
|
|
90
|
+
### JSON API
|
|
91
|
+
|
|
92
|
+
Access usage data programmatically:
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
curl http://localhost:3000/claude-usage/json
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Rake Tasks
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
# Show daily usage report
|
|
102
|
+
rake claude_usage:daily
|
|
103
|
+
|
|
104
|
+
# Show total usage
|
|
105
|
+
rake claude_usage:total
|
|
106
|
+
|
|
107
|
+
# Clear pricing cache
|
|
108
|
+
rake claude_usage:clear_cache
|
|
109
|
+
|
|
110
|
+
# Show which project is being tracked and list all available projects
|
|
111
|
+
rake claude_usage:show_project
|
|
112
|
+
|
|
113
|
+
# Debug pricing issues (helps diagnose $0 cost problems)
|
|
114
|
+
rake claude_usage:debug_pricing
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Programmatic Access
|
|
118
|
+
|
|
119
|
+
```ruby
|
|
120
|
+
# In your Rails app
|
|
121
|
+
aggregator = ClaudeUsage::UsageAggregator.new
|
|
122
|
+
totals = aggregator.total_usage
|
|
123
|
+
daily = aggregator.daily_usage
|
|
124
|
+
|
|
125
|
+
# Access specific data
|
|
126
|
+
puts "Total cost: $#{totals[:total_cost]}"
|
|
127
|
+
puts "Total tokens: #{totals[:total_tokens]}"
|
|
128
|
+
puts "Models used: #{totals[:models_used].join(', ')}"
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Configuration
|
|
132
|
+
|
|
133
|
+
You can customize the behavior in `config/initializers/claude_usage.rb`:
|
|
134
|
+
|
|
135
|
+
```ruby
|
|
136
|
+
ClaudeUsage.configure do |config|
|
|
137
|
+
# Project name - defaults to Rails.root.basename
|
|
138
|
+
config.project_name = "my-rails-app"
|
|
139
|
+
|
|
140
|
+
# Custom Claude paths (optional)
|
|
141
|
+
config.claude_paths = [
|
|
142
|
+
File.expand_path("~/.config/claude/projects"),
|
|
143
|
+
File.expand_path("~/.claude/projects")
|
|
144
|
+
]
|
|
145
|
+
|
|
146
|
+
# Pricing cache expiry (default: 1 hour)
|
|
147
|
+
config.cache_expiry = 1.hour
|
|
148
|
+
|
|
149
|
+
# Offline mode (use cached pricing only)
|
|
150
|
+
config.offline_mode = false
|
|
151
|
+
end
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Configuration Options
|
|
155
|
+
|
|
156
|
+
- **`project_name`** - The Claude Code project name to track (defaults to Rails app name)
|
|
157
|
+
- **`claude_paths`** - Array of paths to search for Claude Code logs
|
|
158
|
+
- **`cache_expiry`** - How long to cache LiteLLM pricing data (default: 1 hour)
|
|
159
|
+
- **`offline_mode`** - Use only cached pricing data without fetching from API
|
|
160
|
+
|
|
161
|
+
## How It Works
|
|
162
|
+
|
|
163
|
+
1. **Reads JSONL Files** - Scans `~/.config/claude/projects/{project_name}/` for usage logs
|
|
164
|
+
2. **Fetches Pricing** - Gets current pricing from [LiteLLM](https://github.com/BerriAI/litellm)
|
|
165
|
+
3. **Calculates Costs** - Applies tiered pricing (200k+ tokens) and cache pricing
|
|
166
|
+
4. **Displays Results** - Shows usage in web UI, JSON API, or CLI
|
|
167
|
+
|
|
168
|
+
## Data Location
|
|
169
|
+
|
|
170
|
+
Claude Code stores usage data at:
|
|
171
|
+
```
|
|
172
|
+
~/.config/claude/projects/{your-project-name}/{sessionId}.jsonl
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
The gem automatically detects your Rails app name and looks for matching Claude Code projects.
|
|
176
|
+
|
|
177
|
+
**Troubleshooting:** If no data is found, see [TROUBLESHOOTING.md](TROUBLESHOOTING.md) for detailed solutions.
|
|
178
|
+
|
|
179
|
+
## Development
|
|
180
|
+
|
|
181
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
182
|
+
|
|
183
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
|
184
|
+
|
|
185
|
+
## Contributing
|
|
186
|
+
|
|
187
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Shoebtamboli/claude_usage. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/Shoebtamboli/claude_usage/blob/main/CODE_OF_CONDUCT.md).
|
|
188
|
+
|
|
189
|
+
## License
|
|
190
|
+
|
|
191
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
192
|
+
|
|
193
|
+
## Code of Conduct
|
|
194
|
+
|
|
195
|
+
Everyone interacting in the ClaudeUsage project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Shoebtamboli/claude_usage/blob/main/CODE_OF_CONDUCT.md).
|
|
196
|
+
|
|
197
|
+
## Acknowledgments
|
|
198
|
+
|
|
199
|
+
- Pricing data from [LiteLLM](https://github.com/BerriAI/litellm)
|
|
200
|
+
- Inspired by the need to track AI development costs
|
|
201
|
+
|
|
202
|
+
## Support
|
|
203
|
+
|
|
204
|
+
If you find this gem useful, please consider:
|
|
205
|
+
- ⭐ Starring the repository
|
|
206
|
+
- 🐛 Reporting bugs
|
|
207
|
+
- 💡 Suggesting new features
|
|
208
|
+
- 🤝 Contributing code
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
Made with ❤️ for Rails developers using Claude Code
|
data/Rakefile
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ClaudeUsage
|
|
4
|
+
class ApplicationController < ActionController::Base
|
|
5
|
+
protect_from_forgery with: :exception
|
|
6
|
+
|
|
7
|
+
# Optional: Add authentication
|
|
8
|
+
# before_action :authenticate_user!
|
|
9
|
+
# before_action :authorize_admin!
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ClaudeUsage
|
|
4
|
+
class UsageController < ApplicationController
|
|
5
|
+
def index
|
|
6
|
+
load_usage_data
|
|
7
|
+
@error = nil
|
|
8
|
+
rescue StandardError => e
|
|
9
|
+
@error = sanitize_error_message(e)
|
|
10
|
+
@daily_usage = []
|
|
11
|
+
@totals = {}
|
|
12
|
+
log_error(e)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def show_json
|
|
16
|
+
load_usage_data
|
|
17
|
+
data = {
|
|
18
|
+
daily: @daily_usage,
|
|
19
|
+
totals: @totals,
|
|
20
|
+
timestamp: Time.current
|
|
21
|
+
}
|
|
22
|
+
render json: data
|
|
23
|
+
rescue StandardError => e
|
|
24
|
+
log_error(e)
|
|
25
|
+
render json: { error: sanitize_error_message(e) }, status: :internal_server_error
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def load_usage_data
|
|
31
|
+
@aggregator = UsageAggregator.new
|
|
32
|
+
@daily_usage = @aggregator.daily_usage
|
|
33
|
+
@totals = @aggregator.total_usage
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def sanitize_error_message(exception)
|
|
37
|
+
# Return user-friendly messages without exposing internal details
|
|
38
|
+
case exception
|
|
39
|
+
when FileReader::InvalidProjectNameError
|
|
40
|
+
exception.message
|
|
41
|
+
when JSON::ParserError
|
|
42
|
+
'Error parsing usage data. Please check your Claude usage files.'
|
|
43
|
+
when Errno::ENOENT, Errno::EACCES
|
|
44
|
+
'Unable to access usage data files. Please check file permissions.'
|
|
45
|
+
else
|
|
46
|
+
'An error occurred while loading usage data. Please try again later.'
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def log_error(exception)
|
|
51
|
+
Rails.logger.error("ClaudeUsage Error: #{exception.class} - #{exception.message}")
|
|
52
|
+
Rails.logger.error(exception.backtrace.join("\n")) if exception.backtrace
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ClaudeUsage
|
|
4
|
+
module UsageHelper
|
|
5
|
+
# Format a number with comma separators
|
|
6
|
+
def format_number(number)
|
|
7
|
+
ClaudeUsage::Formatters.format_number(number)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# Format a dollar amount
|
|
11
|
+
def format_currency(amount)
|
|
12
|
+
ClaudeUsage::Formatters.format_currency(amount)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Format a percentage
|
|
16
|
+
def format_percentage(decimal)
|
|
17
|
+
ClaudeUsage::Formatters.format_percentage(decimal)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|