# she-llac/claude-counter architecture

> Architecture diagram of the GitHub repository she-llac/claude-counter, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/she-llac/claude-counter
- Repository: https://github.com/she-llac/claude-counter

## Overview

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: [`src/content/main.js`](https://github.com/she-llac/claude-counter/blob/main/src/content/main.js) (Main), [`manifest.json`](https://github.com/she-llac/claude-counter/blob/main/manifest.json) (Manifest), [`README.md`](https://github.com/she-llac/claude-counter/blob/main/README.md) (README), [`src/content/ui.js`](https://github.com/she-llac/claude-counter/blob/main/src/content/ui.js) (Overlay UI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Browser runtime"]
end

subgraph group_content["Content script"]
  node_main["Main<br/>content orchestrator<br/>[main.js]"]
  node_bridge_client["Bridge client<br/>content bridge<br/>[bridge-client.js]"]
  node_tokens["Token count<br/>token estimator<br/>[tokens.js]"]
  node_ui["Overlay UI<br/>in-page renderer<br/>[ui.js]"]
  node_constants["Constants<br/>content config<br/>[constants.js]"]
end

subgraph group_injected["Injected bridge"]
  node_bridge["Page bridge<br/>injected instrumentation<br/>[bridge.js]"]
end

subgraph group_distribution["Packaging"]
  node_manifest["Manifest<br/>extension config<br/>[manifest.json]"]
  node_userscript["Userscript<br/>standalone bundle"]
end

subgraph group_assets["Assets and vendor"]
  node_styles["Styles<br/>presentation css<br/>[styles.css]"]
  node_vendor["Tokenizer vendor<br/>third-party code"]
  node_icons["Icons<br/>extension assets"]
  node_notices["Notices"]
  node_license["License"]
  node_readme["README<br/>project docs<br/>[README.md]"]
end

node_manifest -->|"loads"| node_main
node_userscript -->|"boots"| node_main
node_main -->|"connects"| node_bridge_client
node_bridge_client -->|"relays"| node_bridge
node_bridge -->|"streams data"| node_ui
node_main -->|"mounts"| node_ui
node_main -->|"uses"| node_tokens
node_main -->|"reads"| node_constants
node_tokens -->|"depends on"| node_vendor
node_ui -->|"styled by"| node_styles
node_manifest -->|"packages"| node_icons
node_readme -.->|"documents"| node_manifest
node_readme -.->|"documents"| node_userscript
node_notices -.->|"attribution"| node_vendor
node_license -.->|"governs"| node_readme

click node_manifest "https://github.com/she-llac/claude-counter/blob/main/manifest.json"
click node_userscript "https://github.com/she-llac/claude-counter/blob/main/userscript/claude-counter.user.js"
click node_main "https://github.com/she-llac/claude-counter/blob/main/src/content/main.js"
click node_bridge_client "https://github.com/she-llac/claude-counter/blob/main/src/content/bridge-client.js"
click node_tokens "https://github.com/she-llac/claude-counter/blob/main/src/content/tokens.js"
click node_ui "https://github.com/she-llac/claude-counter/blob/main/src/content/ui.js"
click node_constants "https://github.com/she-llac/claude-counter/blob/main/src/content/constants.js"
click node_bridge "https://github.com/she-llac/claude-counter/blob/main/src/injected/bridge.js"
click node_styles "https://github.com/she-llac/claude-counter/blob/main/src/styles.css"
click node_vendor "https://github.com/she-llac/claude-counter/tree/main/src/vendor"
click node_icons "https://github.com/she-llac/claude-counter/tree/main/icons"
click node_notices "https://github.com/she-llac/claude-counter/blob/main/THIRD_PARTY_NOTICES.md"
click node_license "https://github.com/she-llac/claude-counter/tree/main/LICENSE"
click node_readme "https://github.com/she-llac/claude-counter/blob/main/README.md"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_bridge_client,node_tokens,node_ui,node_constants toneAmber
class node_bridge toneMint
class node_manifest,node_userscript toneRose
class node_styles,node_vendor,node_icons,node_notices,node_license,node_readme toneIndigo
```

## Components

### Content script

Extension-side orchestration, state, and UI insertion.

- [Main](https://github.com/she-llac/claude-counter/blob/main/src/content/main.js): content orchestrator · Watches navigation and coordinates overlay lifecycle. · `src/content/main.js`
- [Bridge client](https://github.com/she-llac/claude-counter/blob/main/src/content/bridge-client.js): content bridge · Receives and normalizes events from the page bridge. · `src/content/bridge-client.js`
- [Token count](https://github.com/she-llac/claude-counter/blob/main/src/content/tokens.js): token estimator · Approximates conversation token usage from content. · `src/content/tokens.js`
- [Overlay UI](https://github.com/she-llac/claude-counter/blob/main/src/content/ui.js): in-page renderer · Renders token and quota status inside the chat page. · `src/content/ui.js`
- [Constants](https://github.com/she-llac/claude-counter/blob/main/src/content/constants.js): content config · Shared content-side configuration and limits. · `src/content/constants.js`

### Injected bridge

Page-context instrumentation and event capture.

- [Page bridge](https://github.com/she-llac/claude-counter/blob/main/src/injected/bridge.js): injected instrumentation · Hooks page-level traffic and streams Claude response data. · `src/injected/bridge.js`

### Packaging

Extension and userscript delivery targets.

- [Manifest](https://github.com/she-llac/claude-counter/blob/main/manifest.json): extension config · Browser extension entry and permissions definition. · `manifest.json`
- [Userscript](https://github.com/she-llac/claude-counter/blob/main/userscript/claude-counter.user.js): standalone bundle · Single-file distribution for userscript environments. · `userscript/claude-counter.user.js`

### Assets and vendor

Styling, icons, and embedded third-party code.

- [Styles](https://github.com/she-llac/claude-counter/blob/main/src/styles.css): presentation css · Visual styling for the compact overlay. · `src/styles.css`
- [Tokenizer vendor](https://github.com/she-llac/claude-counter/tree/main/src/vendor): third-party code · Embedded tokenizer and related vendored dependencies. · `src/vendor`
- [Icons](https://github.com/she-llac/claude-counter/tree/main/icons): extension assets · Packaged browser extension icon assets. · `icons`
- [Notices](https://github.com/she-llac/claude-counter/blob/main/THIRD_PARTY_NOTICES.md): license notices · Third-party attribution and licensing information. · `THIRD_PARTY_NOTICES.md`
- [License](https://github.com/she-llac/claude-counter/tree/main/LICENSE): open-source license · Project license terms. · `LICENSE`
- [README](https://github.com/she-llac/claude-counter/blob/main/README.md): project docs · Usage, architecture, and distribution overview. · `README.md`

## Connections

- 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.
