How she-llac/claude-counter works
Claude Counter is a lightweight browser extension/userscript that augments the claude.ai interface with local-only usage and token telemetry. Its architecture is centered on observing Claude’s own page traffic and DOM, then rendering a small overlay UI inside the conversation page.
1. Top-level runtime boundary
- The project runs in two browser contexts:
- an extension content-script context (src/content/*)
- an injected page context (src/injected/bridge.js)
- This split matters because the injected script can hook page-level behavior and network events more directly, while the content script handles extension-side state and UI insertion.
2. Page instrumentation layer
- The injected bridge is the lowest-level subsystem.
- Its job is to intercept Claude’s responses and observe live streaming data so the extension can read:
- conversation-related payloads
- usage-limit updates
- token-relevant data from the page’s API traffic
- This is the main data acquisition boundary between claude.ai and the extension.
3. Bridge and message passing
- src/content/bridge-client.js likely acts as the communication endpoint on the extension side.
- The bridge pattern isolates page-world access from extension logic, typically via browser messaging or DOM events.
- Architecture-wise, this is the handoff point where raw page events become normalized internal state.
4. Conversation token counting subsystem
- src/content/tokens.js is responsible for approximate token counting.
- The README states it uses a vendored o200k_base tokenizer, aligned with Claude-style token estimation.
- This subsystem converts conversation content into a token estimate and feeds the token bar / context-limit indicator.
5. Usage and quota subsystem
- Claude Counter reads usage data from two sources:
- Claude’s /usage endpoint
- live SSE message_limit updates
- The SSE stream provides more precise utilization fractions than the rounded percentages on the native usage page.
- This subsystem tracks session and weekly quotas and their reset timers.
6. UI rendering layer
- src/content/ui.js is the presentation layer for the overlay.
- It renders:
- token count
- cache timer
- session and weekly usage bars
- associated countdowns
- The UI is injected into the existing claude.ai page rather than replacing the page shell.
7. DOM observation and lifecycle
- src/content/main.js is the orchestrator.
- It watches for navigation and DOM changes so the overlay can be inserted or refreshed as the user moves between conversations.
- This is important because claude.ai is a dynamic app; the extension cannot assume a full page reload for every route change.
8. Styling and visual integration
- src/styles.css contains the extension’s visual styling.
- The design goal is a compact in-page augmentation, not a separate panel or dashboard.
- The visuals are likely tuned to blend with claude.ai while still making usage bars and timers noticeable.
9. Vendor and asset boundary
- src/vendor contains third-party code, most notably the tokenizer dependency.
- icons/ holds browser extension assets used by the packaging layer.
- THIRD_PARTY_NOTICES.md and the MIT license indicate distribution includes embedded external code with attribution.
10. Packaging targets
- manifest.json defines the browser-extension variant.
- userscript/claude-counter.user.js provides a single-file userscript distribution for environments that don’t use extension packaging.
- The underlying architecture is the same, but the delivery mechanism changes: extension manifest versus standalone userscript.
11. Privacy and trust boundary
- The project is explicitly local-only: no external servers, no analytics, no tracking.
- The only network dependency is claude.ai itself.
- It reads the lastActiveOrg cookie to query the usage endpoint, which is a narrow, purpose-specific access boundary.
12. End-to-end data flow
- User interacts with claude.ai.
- Claude page emits API responses / SSE usage events.
- Injected bridge captures those page events.
- Content-side logic normalizes them, counts tokens, and computes quota state.
- UI layer renders the current token/usage status back into the page.
- DOM observers keep the overlay synchronized as the conversation view changes.
Overall, the repository is a small but layered browser-extension system: page instrumentation at the edge, a content-script controller in the middle, and a lightweight injected UI on top. The main architectural boundaries are between page context and extension context, and between raw Claude data and the localized presentation state.
Main entry points: main.js Main · manifest.json Manifest · README.md README · ui.js Overlay UI
Components 14
Content script
Extension-side orchestration, state, and UI insertion.
- Main · Watches navigation and coordinates overlay lifecycle.
src/content/main.js - Bridge client · Receives and normalizes events from the page bridge.
src/content/bridge-client.js - Token count · Approximates conversation token usage from content.
src/content/tokens.js - Overlay UI · Renders token and quota status inside the chat page.
src/content/ui.js - Constants · Shared content-side configuration and limits.
src/content/constants.js
Injected bridge
Page-context instrumentation and event capture.
- Page bridge · Hooks page-level traffic and streams Claude response data.
src/injected/bridge.js
Packaging
Extension and userscript delivery targets.
- Manifest · Browser extension entry and permissions definition.
manifest.json - Userscript · Single-file distribution for userscript environments.
userscript/claude-counter.user.js
Assets and vendor
Styling, icons, and embedded third-party code.
- Styles · Visual styling for the compact overlay.
src/styles.css - Tokenizer vendor · Embedded tokenizer and related vendored dependencies.
src/vendor - Icons · Packaged browser extension icon assets.
icons - Notices · Third-party attribution and licensing information.
THIRD_PARTY_NOTICES.md - License · Project license terms.
LICENSE - README · Usage, architecture, and distribution overview.
README.md
Connections 15
- Manifest → Main: loads · Extension manifest activates the content script.
- Userscript → Main: boots · Userscript distribution starts the same runtime path.
- Main → Bridge client: connects · Main controller subscribes to bridge events.
- Bridge client → Page bridge: relays · Content side forwards requests and receives page events.
- Page bridge → Overlay UI: streams data · Captured page telemetry feeds the overlay state.
- Main → Overlay UI: mounts · Main inserts and refreshes the page overlay.
- Main → Token count: uses · Orchestrator invokes token estimation for the current conversation.
- Main → Constants: reads · Shared thresholds and labels are consumed by the content runtime.
- Token count → Tokenizer vendor: depends on · Token estimation relies on vendored tokenizer code.
- Overlay UI → Styles: styled by · Overlay presentation is defined by the stylesheet.
- Manifest → Icons: packages · Extension packaging includes icon assets.
- README → Manifest: documents · Project documentation explains extension packaging.
- README → Userscript: documents · Project documentation explains the userscript build.
- Notices → Tokenizer vendor: attribution · Third-party code is accompanied by notices.
- License → README: governs · Repository distribution is framed by the published license.