← ~/pillars / cost-engineering / cost-per-merged-pr

Cost per merged PR

The metric agent rollouts should steer by: an honest definition, the session-to-PR join problem, the Analytics API and telemetry that feed it, and a weekly report you can automate today.

updated 2026-07-11 ~5 min read pattern 06/07 Cost Engineering

on this page

Context. Everything earlier in this pillar measures spend; this page divides it by something that matters. Cost per token is accounting, cost per completed task is engineering. And cost per merged pull request is the version of it your rollout can steer by, because a merged PR is the unit of shipped work your organisation already counts. The FinOps Foundation’s AI framing calls this cost per unit of work (FinOps for AI); the field advocates it widely and publishes it almost never, because the join is awkward. Awkward, not impossible.

Forces. No API anywhere joins an agent session to the PR it produced: spend is known per user per day, outcomes per repository per merge. The numerator tempts you to exclude failures, which is exactly backwards. The denominator invites gaming the moment it becomes a target: PRs shrink, counts rise, the metric “improves”. And the obvious per-person version is a management hazard: the point is pricing the workflow, not ranking the developers.

Resolution. Define it bluntly as all agent spend in the window, divided by PRs merged in the window, compute it weekly from data you already have, classify agent-assisted PRs with a commit-trailer convention, and steer by the trend. The workflow artifact automates the whole loop.

Define it before you compute it

Three decisions make or break comparability:

  • The numerator is everything. All agent spend in the window (abandoned sessions, exploration, the PR that never merged), exactly as our retired cost-per-completed-task index amortised failed trials over completed ones. A cheap-per-attempt workflow that produces unmergeable PRs is expensive; only a failures-included numerator can say so.
  • The denominator is merged, not created. Created is activity; merged is accepted work. The gap between the two is itself a diagnostic (a widening gap means review is rejecting agent output; see the governance playbook).
  • The window is a week. Daily is noise; monthly hides regressions behind averages. Weekly matches review cadence and the reporting grain of the data sources below.

Then report it as a trend. The absolute number is shaped by your PR culture, your stack, and your seniority mix; its movement week on week is shaped by your workflow changes, which is the thing you control.

The data sources

The Claude Code Analytics API is the purpose-built numerator: daily, per-user records at /v1/organizations/usage_report/claude_code, each carrying num_sessions, lines added and removed, commits_by_claude_code, pull_requests_by_claude_code, and a per-model breakdown with estimated_cost.amount, in cents USD (Analytics API). It needs an Admin API key (organisations only; individual accounts cannot call it), data lands within about an hour, and it is free to call. Three honest limits: costs are estimates; pull_requests_by_claude_code counts PRs created through Claude Code, not merged; and it covers Claude API usage only: Bedrock, Vertex, and Foundry deployments are invisible to it, so those fleets build the numerator from the OTel metrics or CUR data instead.

The merge side is your forge: merged PRs per repo per window is one search query. The two sides share no key, which is the actual problem.

The join problem, pragmatically

Four levels, in ascending effort; pick the cheapest that answers your question:

  1. The org ratio. Total spend over total merged PRs, no join at all. Crude and immediately useful: it is the number the CFO is implicitly asking for, and the trend is real even though the attribution is fuzzy.
  2. The created-PR proxy. pull_requests_by_claude_code over merged PRs approximates what share of throughput is agent-originated, from the API alone. Watch it diverge from level 3 to see how much agent work merges under human-created PRs.
  3. The commit-trailer convention. Agent-produced commits carry a trailer (Co-Authored-By, or a session link like the Claude-Session: trailer this site’s repository stamps on every agent commit); a weekly job greps merged PRs’ commits for it and classifies each PR as agent-assisted or not. Now the denominator splits, and you can price agent-assisted throughput separately. This is the level the artifact implements: one convention, one grep, no new infrastructure.
  4. The full session join. Events carry prompt.id and session.id (monitoring usage); a hook can stamp the session ID into a branch name or commit trailer at commit time, making cost-per-this-PR computable from the events store. Real, and rarely worth it: it prices individual PRs when what you steer by is the weekly workflow trend. Build it when someone genuinely needs to know what PR #4127 cost, not before.

The worked example: this site

Both halves ran here. The controlled half was the cost-per-completed-task index (retired July 2026): same metric, laboratory conditions, which priced the same 100% outcome at $0.14 (Sonnet 4.6) versus $0.23 (Sonnet 5) per task before the harness was retired. The in-the-wild half is the trailer convention: every agent commit in this repository carries a session-link trailer, so classifying any merged PR as agent-assisted is a grep. What this site cannot run is the Analytics API numerator (it is an individual-account operation, and the API is organisations-only), so our numerator comes from telemetry instead, which is precisely the substitution the artifact documents for Bedrock fleets. The pattern survives its own edge cases; that is rather the point.

What to do with the number

  • Steer workflow changes. New review gate, model switch, hook rollout: the weekly figure before and after is your evidence. This is the release-delta logic of the evals guidance applied to your own workflow.
  • Sanity-check against the fleet baseline. Anthropic’s published averages (~$13 per developer per active day) put a floor under plausibility; if your cost per merged PR implies developers spend far more, find the missing denominator first.
  • Split, don’t rank. Agent-assisted versus not, team versus team as workflow comparisons, never individual leaderboards. The per-user fields exist for adoption analysis; a developer who spends more per PR on the hardest codebase is not your problem case.
  • Expect the denominator to move. Agents change PR size as well as count. Pair the ratio with lines-per-PR from the same API response, so a “worsening” cost per PR that is actually bigger PRs reads correctly.

// sources