{"slug": "prtokens-see-how-much-ai-agent-tokens-cost-a-pr", "title": "Prtokens – See how much AI agent tokens cost a PR", "summary": "Prtokens, a new open-source tool, estimates AI agent token costs per pull request by reading local coding-agent transcripts and posting a single cost comment on GitHub. The tool supports Claude Code, Codex, and OpenCode, ensuring aggregate data never leaves the user's machine.", "body_md": "Attribute coding-agent token usage to the GitHub pull request that shipped it.\n\n`prtokens`\n\nreads your local Claude Code, Codex, and OpenCode transcripts, attributes token usage to the commits on your PR branch, and posts a single estimated-cost comment. Only aggregate numbers leave your machine.\n\n- Authenticate the GitHub CLI once:\n`gh auth login`\n\n- From a repository, on a branch with an open PR, run:\n\n```\nnpx prtokens\n```\n\nprtokens finds the open PR, reads your local transcripts, and posts or updates the comment. Run it again anytime to refresh.\n\nA single comment showing total estimated cost, token and session counts, models used, agent costs, and a collapsible per-commit table. The same comment is updated in place on later runs; multiple contributors each get their own labeled section.\n\n`Sessions`\n\ncounts top-level coding-agent sessions. For OpenCode, child/subagent sessions are grouped under their parent session.\n\n*Example (commit table collapsed by default):*\n\n980k in / 42k out · 7 sessions\n\nModels: `claude-sonnet-4-6`\n\n, `gpt-5-codex`\n\nAgents: `claude-code`\n\n~$3.40 · `codex`\n\n~$0.72\n\nCommit breakdown\n\n| Commit | Message | In | Out | Cost | Sessions |\n|---|---|---|---|---|---|\n`a1b2c3d` |\nfeat: add usage readers | 420k | 18k | ~$1.55 | 3 |\n`e4f5a6b` |\ntest: cover attribution | 310k | 12k | ~$1.10 | 2 |\n`c7d8e9f` |\nfix: dedupe sessions | 250k | 12k | ~$1.47 | 2 |\n\n*Generated by prtokens*\n\n| Command | What it does |\n|---|---|\n`prtokens` |\nResolve the current branch's open PR, read local usage, and post or update the comment. |\n`prtokens --pr <n>` |\nTarget PR number `<n>` instead of auto-detecting from the branch. |\n`prtokens --dry-run` |\nPrint the rendered comment to stdout; post nothing. |\n`prtokens --json` |\nPrint a JSON payload (rendered markdown, attribution, pricing, per-agent totals, and per-source diagnostics); post nothing. |\n`prtokens --verbose` |\nAlso print per-source reader diagnostics to stderr. |\n`prtokens init` |\nInstall or update the optional global pre-push hook (see below). |\n`prtokens init --dry-run` |\nPreview the hook changes without writing files. |\n`prtokens status` |\nShow pending, blocked, failed, and recently completed automatic PR comment jobs. |\n`prtokens pr create -- <gh args>` |\nRun `gh pr create <gh args>` and attempt to post the prtokens comment after successful PR creation. |\n\n- Node.js 22.13+\n- GitHub CLI authenticated with\n`gh auth login`\n\n- Transcripts from at least one supported agent:\n- Claude Code —\n`~/.claude/projects`\n\n- Codex —\n`~/.codex/sessions`\n\nor`~/.codex/archived_sessions`\n\n- OpenCode — SQLite databases under\n`~/.local/share/opencode`\n\n- Claude Code —\n\nInstall globally and run the setup command to post comments automatically on every `git push`\n\n:\n\n```\nnpm i -g prtokens\nprtokens init\n```\n\nPreview changes first with `prtokens init --dry-run`\n\n.\n\n`prtokens init`\n\nwrites a managed block into your global `pre-push`\n\nhook. If an absolute global `core.hooksPath`\n\nis already set, prtokens writes the hook there; otherwise it creates `~/.config/git/hooks/pre-push`\n\nand sets `core.hooksPath`\n\nglobally. A relative `core.hooksPath`\n\nis rejected.\n\n**Caveats:**\n\n`prtokens`\n\nmust be on`PATH`\n\nfor hooks, or the absolute path baked in by`prtokens init`\n\nmust stay valid.- A repository with a local\n`core.hooksPath`\n\nbypasses the global hook. `gh pr create`\n\non an already-pushed branch won't trigger the hook.\n\nWhen a push has an open PR, the comment is posted immediately in the background. When pushed before the PR exists, prtokens records a pending job and retries within ~30 minutes.\n\nFor the most reliable workflow, create PRs through:\n\n```\nprtokens pr create -- --title \"My PR\" --body \"Description\"\n```\n\nEverything after `--`\n\nis passed to `gh pr create`\n\n. If PR creation succeeds, prtokens posts the comment before exiting.\n\nFor manual setup, run `prtokens init --dry-run`\n\nto see the exact managed block and its target path, then place it yourself.\n\nTranscripts never leave your machine. The PR comment contains only aggregate numbers — token counts, dollar estimates, session counts, model names, and commit metadata already visible in the PR. The automatic queue stores only repository path, remote/branch names, head SHA, timestamps, and job status — never transcripts, prompts, or rendered comment text.\n\n| Situation | Outcome |\n|---|---|\n| No open PR | Prints a hint; exits 0 |\n| No transcripts | Prints a hint; exits 0 |\nMissing or unauthenticated `gh` |\nPrints setup instructions; exits 1 |\n| Comment post failure | Prints rendered markdown for manual paste; exits 0 |\n| Push with no PR yet (hook) | Records pending job; exits 0 |\n`prtokens pr create` — `gh` fails |\nReturns `gh` exit code; does not post |\n`prtokens pr create` — comment fails |\nPrints error; exits 0 |\n\nCosts use agent-reported values when available. Otherwise, prtokens estimates at API rates using a bundled LiteLLM pricing snapshot covering Claude models (first-party, Bedrock, and Vertex) and OpenAI GPT-5 coding-agent aliases. Subscription users may have zero marginal cost — treat the number as a cost-awareness estimate. Refresh the snapshot with `npm run update-pricing`\n\n.", "url": "https://wpnews.pro/news/prtokens-see-how-much-ai-agent-tokens-cost-a-pr", "canonical_source": "https://github.com/SamuelZ12/prtokens/", "published_at": "2026-06-15 19:24:15+00:00", "updated_at": "2026-06-15 19:35:02.990866+00:00", "lang": "en", "topics": ["developer-tools", "ai-tools", "ai-agents", "large-language-models"], "entities": ["Prtokens", "Claude Code", "Codex", "OpenCode", "GitHub"], "alternates": {"html": "https://wpnews.pro/news/prtokens-see-how-much-ai-agent-tokens-cost-a-pr", "markdown": "https://wpnews.pro/news/prtokens-see-how-much-ai-agent-tokens-cost-a-pr.md", "text": "https://wpnews.pro/news/prtokens-see-how-much-ai-agent-tokens-cost-a-pr.txt", "jsonld": "https://wpnews.pro/news/prtokens-see-how-much-ai-agent-tokens-cost-a-pr.jsonld"}}