{"_id":"@ctogg/pi-cost-counter","_rev":"2-7adfd131da46ca1d2c3541575602164c","name":"@ctogg/pi-cost-counter","dist-tags":{"latest":"0.1.1"},"versions":{"0.1.0":{"name":"@ctogg/pi-cost-counter","version":"0.1.0","keywords":["pi","pi-package","pi-extension","cost-tracking","llm-cost","token-usage"],"author":"","license":"MIT","_id":"@ctogg/pi-cost-counter@0.1.0","maintainers":[{"name":"ctogg","email":"christoffer@cto.gg"}],"homepage":"https://github.com/cristeahub/pi-cost-counter#readme","bugs":{"url":"https://github.com/cristeahub/pi-cost-counter/issues"},"pi":{"extensions":["./index.ts"]},"dist":{"shasum":"69e6190a1f5383ce60716059136754f2ff1e2555","tarball":"https://registry.npmjs.org/@ctogg/pi-cost-counter/-/pi-cost-counter-0.1.0.tgz","fileCount":4,"integrity":"sha512-rgmFHnqWSyyFLQBIcbfxgCkd2UqNOovniKjSVqZrm9Jw6gzQ0R+qgdPifXMHGkQ9AFkBhRIaqbY+lrrISFo9IA==","signatures":[{"sig":"MEUCIFy49+hmS+mVY53HYuLsHVqQ43qOHZu22sogtMMZiZQrAiEAyMXeffs9oe5C6F+ePCr5CcfKMwx7m2Sys/jlayecnXs=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":17133},"type":"module","gitHead":"beee7346d4c4ba22cb0fbc69736554b4e3b9b277","scripts":{"build":"echo 'nothing to build'","check":"echo 'nothing to check'","clean":"echo 'nothing to clean'"},"_npmUser":{"name":"ctogg","email":"christoffer@cto.gg"},"repository":{"url":"git+https://github.com/cristeahub/pi-cost-counter.git","type":"git"},"_npmVersion":"10.9.3","description":"Pi extension that tracks LLM API costs across all sessions with daily JSONL logging and a /cost command","directories":{},"_nodeVersion":"22.19.0","_hasShrinkwrap":false,"peerDependencies":{"@mariozechner/pi-coding-agent":"*"},"_npmOperationalInternal":{"tmp":"tmp/pi-cost-counter_0.1.0_1776455820052_0.31476250214142176","host":"s3://npm-registry-packages-npm-production"}},"0.1.1":{"name":"@ctogg/pi-cost-counter","version":"0.1.1","description":"Pi extension that tracks LLM API costs across all sessions with daily JSONL logging and a /cost command","type":"module","keywords":["pi","pi-package","pi-extension","cost-tracking","llm-cost","token-usage"],"license":"MIT","author":"","repository":{"type":"git","url":"git+https://github.com/cristeahub/pi-cost-counter.git"},"homepage":"https://github.com/cristeahub/pi-cost-counter#readme","bugs":{"url":"https://github.com/cristeahub/pi-cost-counter/issues"},"pi":{"extensions":["./index.ts"]},"scripts":{"clean":"echo 'nothing to clean'","build":"echo 'nothing to build'","check":"echo 'nothing to check'"},"peerDependencies":{"@earendil-works/pi-coding-agent":"*"},"_id":"@ctogg/pi-cost-counter@0.1.1","gitHead":"0b71e691f08229c6923079cd8fd289c636306604","_nodeVersion":"22.19.0","_npmVersion":"10.9.3","dist":{"integrity":"sha512-cCuBhu/a0YAPjSn7LTsV/cQ8UTks3afdafBTjCLhdeWaQEGnz2DXBJrGkCExMy6AriUCEvNvCTb47xOzXugJow==","shasum":"7d334e01825e745cc4b06fc43a546230fe9f653e","tarball":"https://registry.npmjs.org/@ctogg/pi-cost-counter/-/pi-cost-counter-0.1.1.tgz","fileCount":4,"unpackedSize":17163,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCICmEzO/WHdBCgR2RkSnhv835yj81NUJFc14fIaKNibc4AiEAsZWAtmDeEY9HF3FN4yCAbxE/v5hlHry0Qd1dwwwQpBc="}]},"_npmUser":{"name":"ctogg","email":"christoffer@cto.gg"},"directories":{},"maintainers":[{"name":"ctogg","email":"christoffer@cto.gg"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/pi-cost-counter_0.1.1_1778231838972_0.06132921134403713"},"_hasShrinkwrap":false}},"time":{"created":"2026-04-17T19:56:59.874Z","modified":"2026-05-08T09:17:19.418Z","0.1.0":"2026-04-17T19:57:00.178Z","0.1.1":"2026-05-08T09:17:19.116Z"},"bugs":{"url":"https://github.com/cristeahub/pi-cost-counter/issues"},"license":"MIT","homepage":"https://github.com/cristeahub/pi-cost-counter#readme","keywords":["pi","pi-package","pi-extension","cost-tracking","llm-cost","token-usage"],"repository":{"type":"git","url":"git+https://github.com/cristeahub/pi-cost-counter.git"},"description":"Pi extension that tracks LLM API costs across all sessions with daily JSONL logging and a /cost command","maintainers":[{"name":"ctogg","email":"christoffer@cto.gg"}],"readme":"# pi Cost Tracker\n\nA [pi](https://github.com/earendil-works/pi-coding-agent) extension that tracks LLM API costs across all sessions. Every API call is logged to a local append-only ledger so you always know what you're spending.\n\n## Features\n\n- **Automatic tracking** — every LLM call is recorded with token counts, cost breakdown, provider, and model\n- **Multi-client safe** — uses append-only JSONL files, safe for concurrent writes from multiple pi instances\n- **`/cost` command** — view today's cost or a multi-day range with breakdowns by model and provider\n- **Zero configuration** — install and go, no API keys or setup required\n\n## Installation\n\n### From npm (recommended)\n\n```bash\npi install npm:@ctogg/pi-cost-counter\n```\n\n### From git\n\n```bash\npi install git:github.com/cristeahub/pi-cost-counter\n```\n\n### Local\n\n```bash\npi install /path/to/pi-cost-counter\n```\n\n### Quick test (temporary, current session only)\n\n```bash\npi -e npm:@ctogg/pi-cost-counter\npi -e /path/to/pi-cost-counter\n```\n\nAfter installation, restart pi or run `/reload` to activate.\n\n## Usage\n\n### `/cost` Command\n\nView cost summaries directly in pi:\n\n```\n/cost            Today's total + model breakdown\n/cost 7d         Last 7 days\n/cost 30d        Last 30 days\n/cost 365d       Last year\n```\n\nExample output:\n\n```\nCost for last 7 days (2026-04-11 → 2026-04-17)\n\n  Total: $4.82  156.3K tokens · 87 calls\n\n  Daily breakdown\n  ────────────────────────────────────────────────────\n  2026-04-11  $0.45\n  2026-04-12  $1.23\n  2026-04-13  —\n  2026-04-14  $0.67\n  2026-04-15  $0.89\n  2026-04-16  $0.34\n  2026-04-17  $1.24\n\n  By model\n  ────────────────────────────────────────────────────\n  anthropic/claude-sonnet-4-5                    $3.41  120.5K tok · 62 calls\n  anthropic/claude-haiku-3-5                     $0.89   28.1K tok · 18 calls\n  google/gemini-2.5-pro                          $0.52    7.7K tok · 7 calls\n\n```\n\n## Data Storage\n\nCost records are stored as JSONL (JSON Lines) files, organized by date:\n\n```\n~/.pi/cost-tracker/\n  2026/\n    04/\n      17.jsonl\n      18.jsonl\n    05/\n      01.jsonl\n```\n\n### Record Format\n\nEach line in a day file is a single JSON object representing one LLM API call:\n\n```json\n{\n  \"ts\": 1713369600000,\n  \"provider\": \"anthropic\",\n  \"model\": \"claude-sonnet-4-5\",\n  \"tokens\": {\n    \"input\": 1200,\n    \"output\": 340,\n    \"cacheRead\": 800,\n    \"cacheWrite\": 0\n  },\n  \"cost\": {\n    \"input\": 0.0036,\n    \"output\": 0.0051,\n    \"cacheRead\": 0.0008,\n    \"cacheWrite\": 0,\n    \"total\": 0.0095\n  }\n}\n```\n\n| Field               | Description                                         |\n| ------------------- | --------------------------------------------------- |\n| `ts`                | Unix timestamp in milliseconds                      |\n| `provider`          | API provider (e.g. `anthropic`, `google`, `openai`) |\n| `model`             | Model identifier (e.g. `claude-sonnet-4-5`)         |\n| `tokens.input`      | Input/prompt tokens                                 |\n| `tokens.output`     | Output/completion tokens                            |\n| `tokens.cacheRead`  | Tokens served from prompt cache                     |\n| `tokens.cacheWrite` | Tokens written to prompt cache                      |\n| `cost.input`        | Cost for input tokens (USD)                         |\n| `cost.output`       | Cost for output tokens (USD)                        |\n| `cost.cacheRead`    | Cost for cache-read tokens (USD)                    |\n| `cost.cacheWrite`   | Cost for cache-write tokens (USD)                   |\n| `cost.total`        | Total cost for this call (USD)                      |\n\n## Querying Data Outside pi\n\nSince the data is plain JSONL, you can query it with standard tools:\n\n```bash\n# Today's total cost\ncat ~/.pi/cost-tracker/2026/04/17.jsonl | jq -s 'map(.cost.total) | add'\n\n# Top models this month\ncat ~/.pi/cost-tracker/2026/04/*.jsonl | jq -s 'group_by(.model) | map({model: .[0].model, total: map(.cost.total) | add}) | sort_by(-.total)'\n\n# Total spend for April\ncat ~/.pi/cost-tracker/2026/04/*.jsonl | jq -s 'map(.cost.total) | add'\n\n# Number of API calls per day this month\nfor f in ~/.pi/cost-tracker/2026/04/*.jsonl; do\n  echo \"$(basename $f .jsonl): $(wc -l < $f) calls\"\ndone\n\n# Export to CSV\ncat ~/.pi/cost-tracker/2026/04/*.jsonl | jq -r '[.ts, .provider, .model, .cost.total] | @csv'\n```\n\n## How It Works\n\nThe extension hooks into pi's event system:\n\n1. **`message_end`** — fires after every message (user, assistant, tool result). The handler filters for assistant messages, extracts the `usage` object that pi attaches to every LLM response, and appends a JSONL record to the appropriate day file.\n\n2. **`/cost` command** — reads JSONL files for the requested date range, aggregates by day and by model, and displays a formatted summary.\n\n## Limitations\n\n- **Cost accuracy depends on pi's cost calculation** — the extension records whatever pi reports in the `usage.cost` fields. If a model's pricing is not configured in pi, costs may show as zero even though tokens were consumed.\n- **Local timezone** — day boundaries are based on your system's local time.\n- **No real-time sync** — the `/cost` command reads files at invocation time. It does not live-update while other pi clients are running. Run it again to see the latest.\n\n## License\n\nMIT\n","readmeFilename":"README.md"}