token_hawk 0.1.0
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/CHANGELOG.md +51 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +201 -0
- data/Rakefile +12 -0
- data/app/assets/stylesheets/token_hawk/application.css +106 -0
- data/app/controllers/token_hawk/application_controller.rb +6 -0
- data/app/controllers/token_hawk/dashboard_controller.rb +12 -0
- data/app/controllers/token_hawk/domains_controller.rb +15 -0
- data/app/controllers/token_hawk/pricing_controller.rb +9 -0
- data/app/controllers/token_hawk/recent_controller.rb +12 -0
- data/app/views/layouts/token_hawk/application.html.erb +251 -0
- data/app/views/token_hawk/dashboard/index.html.erb +53 -0
- data/app/views/token_hawk/domains/show.html.erb +48 -0
- data/app/views/token_hawk/pricing/index.html.erb +27 -0
- data/app/views/token_hawk/recent/index.html.erb +36 -0
- data/config/routes.rb +8 -0
- data/db/migrate/01_create_token_hawk_calls.rb +22 -0
- data/db/migrate/02_create_token_hawk_llm_rates.rb +16 -0
- data/docs/cli.md +98 -0
- data/docs/configuration.md +90 -0
- data/docs/dashboard.md +73 -0
- data/docs/subscribe_hooks.md +81 -0
- data/exe/token_hawk +15 -0
- data/lib/generators/token_hawk/install/install_generator.rb +42 -0
- data/lib/generators/token_hawk/install/templates/create_token_hawk_calls.rb +18 -0
- data/lib/generators/token_hawk/install/templates/token_hawk.rb +12 -0
- data/lib/token_hawk/adapters/base.rb +23 -0
- data/lib/token_hawk/call.rb +15 -0
- data/lib/token_hawk/call_record.rb +18 -0
- data/lib/token_hawk/cli/commands/costs.rb +92 -0
- data/lib/token_hawk/cli/commands/efficiency.rb +54 -0
- data/lib/token_hawk/cli/commands/recent.rb +52 -0
- data/lib/token_hawk/cli/formatter.rb +13 -0
- data/lib/token_hawk/cli.rb +34 -0
- data/lib/token_hawk/configuration.rb +19 -0
- data/lib/token_hawk/cost.rb +18 -0
- data/lib/token_hawk/dashboard_query.rb +32 -0
- data/lib/token_hawk/domain_query.rb +35 -0
- data/lib/token_hawk/engine.rb +7 -0
- data/lib/token_hawk/pricing.rb +41 -0
- data/lib/token_hawk/recent_query.rb +16 -0
- data/lib/token_hawk/response_adapter.rb +23 -0
- data/lib/token_hawk/storage/active_record.rb +54 -0
- data/lib/token_hawk/storage/memory.rb +33 -0
- data/lib/token_hawk/version.rb +5 -0
- data/lib/token_hawk.rb +122 -0
- data/sig/token_hawk.rbs +4 -0
- metadata +172 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 96402d5c72f5941aa1c3bf248810ec506bf29ea46c4d22a489c98b71eb71a1dd
|
|
4
|
+
data.tar.gz: 629d35e1dd8aa2c7f70c16beedfedd4ceead838f05394d33a693dfb8927b24fd
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 1ef9d64e6fb41d9fe67687bde532e6afb58ed73a649be7ad7cc75f1e8b3102612325d75aafbf1e2480cd9bba31a7ae23b0899ba20334ee743ba2ac394b8ec522
|
|
7
|
+
data.tar.gz: 8c44feb4d45db32c900d48690e03be4040783de5e5326135dded41ae5d677e00c708c79e8737d80264abede9fd10d01908c63cf03bf75aecfbd1331c16b5f586
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
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.0] - 2026-09-01
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
**Core attribution**
|
|
15
|
+
- `TokenHawk.track(domain:, tags: {})` — wraps any LLM API call and captures tokens, cost, and latency automatically
|
|
16
|
+
- `TokenHawk.record(domain:, model:, input_tokens:, output_tokens:, latency_ms:, tags: {})` — manual attribution for apps using abstraction layers (PromptCanary, LangChain, etc.)
|
|
17
|
+
- `TokenHawk.subscribe(:call_recorded)` — hook into every recorded call for metrics, alerting, or custom logging
|
|
18
|
+
- Automatic adapter detection for Anthropic and OpenAI SDK responses
|
|
19
|
+
- Telemetry failures are logged and swallowed — never raised out of the block
|
|
20
|
+
|
|
21
|
+
**Storage**
|
|
22
|
+
- ActiveRecord storage backend with a single `token_hawk_calls` table
|
|
23
|
+
- `tags` column stored as serialized JSON — works with any ActiveRecord-compatible database
|
|
24
|
+
- Scopes: `for_domain`, `since`, `with_tag`
|
|
25
|
+
- In-process memory storage for use in tests
|
|
26
|
+
|
|
27
|
+
**Pricing**
|
|
28
|
+
- Hardcoded pricing table for: `claude-opus-4-7/4-8`, `claude-sonnet-4-6`, `claude-haiku-4-5`, `gpt-4o`, `gpt-4o-mini`, `o1`, `o1-mini`
|
|
29
|
+
- `config.pricing` override for unlisted or custom models
|
|
30
|
+
- Friendly warning when an unknown model is encountered
|
|
31
|
+
|
|
32
|
+
**CLI** (`token_hawk` command)
|
|
33
|
+
- `costs` — monthly spend grouped by domain, day, or tag; supports `--domain`, `--since`, `--tag`, `--format json`
|
|
34
|
+
- `efficiency` — cost per call by domain, sorted descending; surfaces expensive-per-call domains
|
|
35
|
+
- `recent` — reverse-chronological call log with `--limit` and `--format json`
|
|
36
|
+
|
|
37
|
+
**Rails engine dashboard** (mount at `/token_hawk`)
|
|
38
|
+
- Overview — monthly total, top 3 domains by cost, daily spend trend
|
|
39
|
+
- Domain detail — total cost, call count, avg cost per call, top tags, recent calls
|
|
40
|
+
- Recent calls — full reverse-chronological call log with domain filtering
|
|
41
|
+
- Pricing reference — all supported models and their input/output rates
|
|
42
|
+
|
|
43
|
+
**Generator**
|
|
44
|
+
- `rails generate token_hawk:install` — creates the initializer and migration in one step
|
|
45
|
+
|
|
46
|
+
### Dependencies
|
|
47
|
+
- Ruby >= 3.2.0
|
|
48
|
+
- ActiveRecord >= 7.0, < 9
|
|
49
|
+
- ActiveSupport >= 7.0, < 9
|
|
50
|
+
- Railties >= 7.0, < 9
|
|
51
|
+
- Thor >= 1.0
|
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) 2026 Rockwell Windsor Rice
|
|
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,201 @@
|
|
|
1
|
+
# TokenHawk
|
|
2
|
+
|
|
3
|
+
**Code-native LLM cost attribution for Rails apps.**
|
|
4
|
+
|
|
5
|
+
Know exactly which feature, domain, or customer is driving your AI bill — without a SaaS dashboard, without changing how you call the API.
|
|
6
|
+
|
|
7
|
+
[](https://badge.fury.io/rb/token_hawk)
|
|
8
|
+
[](https://github.com/rockwellwindsor/token_hawk/actions/workflows/main.yml)
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## 30-second demo
|
|
13
|
+
|
|
14
|
+
```ruby
|
|
15
|
+
# Wrap any LLM call with a domain label
|
|
16
|
+
TokenHawk.track(domain: :invoice_extraction) do
|
|
17
|
+
client.messages.create(model: "claude-sonnet-4-6", max_tokens: 1024, messages: [...])
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# That's it. TokenHawk captures tokens, cost, and latency automatically.
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Then from the terminal:
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
$ token_hawk costs --by domain
|
|
27
|
+
|
|
28
|
+
invoice_extraction $4.21 312 calls
|
|
29
|
+
chat_support $1.08 87 calls
|
|
30
|
+
pdf_summarizer 38¢ 14 calls
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
Add to your Gemfile:
|
|
38
|
+
|
|
39
|
+
```ruby
|
|
40
|
+
gem "token_hawk"
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Run the generator:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
bundle exec rails generate token_hawk:install
|
|
47
|
+
bundle exec rails db:migrate
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Mount the dashboard in `config/routes.rb`:
|
|
51
|
+
|
|
52
|
+
```ruby
|
|
53
|
+
mount TokenHawk::Engine, at: "/token_hawk"
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Add attribution to your initializer (`config/initializers/token_hawk.rb`):
|
|
57
|
+
|
|
58
|
+
```ruby
|
|
59
|
+
TokenHawk.configure do |config|
|
|
60
|
+
config.storage = :active_record
|
|
61
|
+
end
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## Tracking calls
|
|
67
|
+
|
|
68
|
+
### With the SDK response (automatic extraction)
|
|
69
|
+
|
|
70
|
+
```ruby
|
|
71
|
+
TokenHawk.track(domain: :invoice_extraction) do
|
|
72
|
+
client.messages.create(...)
|
|
73
|
+
end
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Supports Anthropic and OpenAI SDK responses out of the box.
|
|
77
|
+
|
|
78
|
+
### With tags (multi-tenant attribution)
|
|
79
|
+
|
|
80
|
+
```ruby
|
|
81
|
+
TokenHawk.track(domain: :invoice_extraction, tags: { customer_id: 42 }) do
|
|
82
|
+
client.messages.create(...)
|
|
83
|
+
end
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Manual attribution (abstraction layers, PromptCanary, LangChain, etc.)
|
|
87
|
+
|
|
88
|
+
If your app wraps LLM calls behind a service layer, use `record` to pass token counts directly:
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
TokenHawk.record(
|
|
92
|
+
domain: :invoice_extraction,
|
|
93
|
+
model: "claude-sonnet-4-6",
|
|
94
|
+
input_tokens: 820,
|
|
95
|
+
output_tokens: 214,
|
|
96
|
+
latency_ms: 1340,
|
|
97
|
+
tags: { customer_id: 42 }
|
|
98
|
+
)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
## CLI
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
# Monthly spend by domain (default)
|
|
107
|
+
token_hawk costs
|
|
108
|
+
|
|
109
|
+
# Group by day
|
|
110
|
+
token_hawk costs --by day
|
|
111
|
+
|
|
112
|
+
# Filter to one domain
|
|
113
|
+
token_hawk costs --domain invoice_extraction
|
|
114
|
+
|
|
115
|
+
# Filter to a specific tag value
|
|
116
|
+
token_hawk costs --tag customer_id=42
|
|
117
|
+
|
|
118
|
+
# Group by tag
|
|
119
|
+
token_hawk costs --by tag --tag customer_id
|
|
120
|
+
|
|
121
|
+
# Cost per call by domain (find expensive-per-call domains)
|
|
122
|
+
token_hawk efficiency
|
|
123
|
+
|
|
124
|
+
# Recent calls for debugging
|
|
125
|
+
token_hawk recent --limit 20
|
|
126
|
+
|
|
127
|
+
# JSON output (pipe to jq, etc.)
|
|
128
|
+
token_hawk costs --format json | jq .
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
## Dashboard
|
|
134
|
+
|
|
135
|
+
Mount the engine and visit `/token_hawk` for a browser-based view:
|
|
136
|
+
|
|
137
|
+
- **Overview** — monthly total, top domains by cost, daily spend trend
|
|
138
|
+
- **Domain detail** — per-domain breakdown with top tags and recent calls
|
|
139
|
+
- **Recent calls** — reverse-chronological call log for debugging
|
|
140
|
+
- **Pricing reference** — supported models and their rates
|
|
141
|
+
|
|
142
|
+
The engine has no auth of its own — protect the mount point with your app's existing authentication:
|
|
143
|
+
|
|
144
|
+
```ruby
|
|
145
|
+
# config/routes.rb
|
|
146
|
+
authenticate :user, ->(u) { u.admin? } do
|
|
147
|
+
mount TokenHawk::Engine, at: "/token_hawk"
|
|
148
|
+
end
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
## Configuration
|
|
154
|
+
|
|
155
|
+
```ruby
|
|
156
|
+
TokenHawk.configure do |config|
|
|
157
|
+
# Storage backend: :active_record (default) or :memory (tests only)
|
|
158
|
+
config.storage = :active_record
|
|
159
|
+
|
|
160
|
+
# Log telemetry failures instead of raising
|
|
161
|
+
config.log_failures = true
|
|
162
|
+
config.failure_logger = ->(msg) { Rails.logger.warn(msg) }
|
|
163
|
+
|
|
164
|
+
# Override or add pricing for unlisted models
|
|
165
|
+
config.pricing["my-custom-model"] = { vendor: "anthropic", input: 0.3, output: 1.5 }
|
|
166
|
+
end
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
## Subscribe hooks
|
|
172
|
+
|
|
173
|
+
React to every recorded call in real time:
|
|
174
|
+
|
|
175
|
+
```ruby
|
|
176
|
+
TokenHawk.subscribe(:call_recorded) do |call|
|
|
177
|
+
StatsD.increment("llm.calls", tags: ["domain:#{call.domain}"])
|
|
178
|
+
StatsD.gauge("llm.cost_cents", call.total_cost_cents, tags: ["domain:#{call.domain}"])
|
|
179
|
+
end
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
---
|
|
183
|
+
|
|
184
|
+
## What it doesn't do
|
|
185
|
+
|
|
186
|
+
- **No streaming support** — token counts require a complete response
|
|
187
|
+
- **No real-time dashboard** — the UI reads from the database; refresh manually
|
|
188
|
+
- **No multi-database support** — Postgres and SQLite only in v0.1.0
|
|
189
|
+
- **No alerting** — use subscribe hooks to wire your own
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
## Why it exists
|
|
194
|
+
|
|
195
|
+
Most Rails apps reach for an LLM and start accumulating costs they can't explain. By the time the bill is painful, the usage is spread across dozens of call sites with no attribution. TokenHawk solves this at the source — one wrapper in your code, costs attributed from day one.
|
|
196
|
+
|
|
197
|
+
---
|
|
198
|
+
|
|
199
|
+
## License
|
|
200
|
+
|
|
201
|
+
MIT. See [LICENSE.txt](LICENSE.txt).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/* TokenHawk dashboard — self-contained, no external dependencies */
|
|
2
|
+
|
|
3
|
+
.th-dashboard {
|
|
4
|
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
|
|
5
|
+
max-width: 960px;
|
|
6
|
+
margin: 0 auto;
|
|
7
|
+
padding: 2rem 1.5rem;
|
|
8
|
+
color: #1a1a2e;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
.th-dashboard h1 {
|
|
12
|
+
font-size: 1.75rem;
|
|
13
|
+
font-weight: 700;
|
|
14
|
+
margin-bottom: 1.5rem;
|
|
15
|
+
border-bottom: 2px solid #e2e8f0;
|
|
16
|
+
padding-bottom: 0.75rem;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
.th-dashboard h2 {
|
|
20
|
+
font-size: 1rem;
|
|
21
|
+
font-weight: 600;
|
|
22
|
+
text-transform: uppercase;
|
|
23
|
+
letter-spacing: 0.05em;
|
|
24
|
+
color: #64748b;
|
|
25
|
+
margin-bottom: 0.75rem;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
.th-dashboard section {
|
|
29
|
+
margin-bottom: 2.5rem;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/* Stat blocks */
|
|
33
|
+
.th-stat-row {
|
|
34
|
+
display: flex;
|
|
35
|
+
gap: 1.5rem;
|
|
36
|
+
flex-wrap: wrap;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
.th-stat {
|
|
40
|
+
background: #f8fafc;
|
|
41
|
+
border: 1px solid #e2e8f0;
|
|
42
|
+
border-radius: 8px;
|
|
43
|
+
padding: 1.25rem 1.5rem;
|
|
44
|
+
min-width: 180px;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
.th-total {
|
|
48
|
+
font-size: 2rem;
|
|
49
|
+
font-weight: 700;
|
|
50
|
+
margin: 0;
|
|
51
|
+
color: #0f172a;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/* Tables */
|
|
55
|
+
.th-dashboard table {
|
|
56
|
+
width: 100%;
|
|
57
|
+
border-collapse: collapse;
|
|
58
|
+
font-size: 0.9rem;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
.th-dashboard th {
|
|
62
|
+
text-align: left;
|
|
63
|
+
padding: 0.6rem 0.75rem;
|
|
64
|
+
background: #f1f5f9;
|
|
65
|
+
border-bottom: 2px solid #e2e8f0;
|
|
66
|
+
font-weight: 600;
|
|
67
|
+
color: #475569;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
.th-dashboard td {
|
|
71
|
+
padding: 0.6rem 0.75rem;
|
|
72
|
+
border-bottom: 1px solid #e2e8f0;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
.th-dashboard tr:last-child td {
|
|
76
|
+
border-bottom: none;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.th-dashboard tr:hover td {
|
|
80
|
+
background: #f8fafc;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/* Tags */
|
|
84
|
+
.th-tags ul {
|
|
85
|
+
list-style: none;
|
|
86
|
+
padding: 0;
|
|
87
|
+
margin: 0;
|
|
88
|
+
display: flex;
|
|
89
|
+
flex-wrap: wrap;
|
|
90
|
+
gap: 0.5rem;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
.th-tags li {
|
|
94
|
+
background: #e0f2fe;
|
|
95
|
+
color: #0369a1;
|
|
96
|
+
border-radius: 4px;
|
|
97
|
+
padding: 0.25rem 0.6rem;
|
|
98
|
+
font-size: 0.85rem;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/* Note text */
|
|
102
|
+
.th-note {
|
|
103
|
+
font-size: 0.85rem;
|
|
104
|
+
color: #64748b;
|
|
105
|
+
margin-bottom: 1rem;
|
|
106
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module TokenHawk
|
|
4
|
+
class DashboardController < ApplicationController
|
|
5
|
+
def index
|
|
6
|
+
query = DashboardQuery.new(since: Time.now.beginning_of_month)
|
|
7
|
+
@monthly_total = query.monthly_total
|
|
8
|
+
@top_domains = query.top_domains
|
|
9
|
+
@daily_costs = query.daily_costs
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module TokenHawk
|
|
4
|
+
class DomainsController < ApplicationController
|
|
5
|
+
def show
|
|
6
|
+
query = DomainQuery.new(domain: params[:domain])
|
|
7
|
+
@domain = params[:domain]
|
|
8
|
+
@total_cost = query.total_cost
|
|
9
|
+
@call_count = query.call_count
|
|
10
|
+
@cost_per_call = query.cost_per_call
|
|
11
|
+
@top_tags = query.top_tags
|
|
12
|
+
@recent_calls = query.recent_calls
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|