:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(engineering-analytics): list agent llm spend per pr for an author (#123368)
Georges-Antoine Assiaaf7c90d0dac
diff --git a/hogli.yaml b/hogli.yamlindex 4b65ce2612fa..474be0401b02 100644--- a/hogli.yaml+++ b/hogli.yaml@@ -1136,6 +1136,9 @@ devex:devex:feedback:click: hogli_commands.feedback:devex_feedbackdescription: Send feedback about the dev experience to the devex team (humans and agents)+ agent:usage:+ click: hogli_commands.agent_usage:cmd_agent_usage+ description: Show agent LLM token spend per pull request, from engineering analyticsutilities:nuke:steps:diff --git a/products/engineering_analytics/backend/facade/api.py b/products/engineering_analytics/backend/facade/api.pyindex 4119149850d8..68c1129e58c1 100644--- a/products/engineering_analytics/backend/facade/api.py+++ b/products/engineering_analytics/backend/facade/api.py@@ -27,6 +27,7 @@ from products.engineering_analytics.backend.facade.contracts import (AttentionPullRequestList,AuthorFrictionDetail,AuthorFrictionList,+ AuthorLLMSpendList,BranchPRMatch,BrokenTestsResult,CICardSummary,@@ -45,6 +46,7 @@ from products.engineering_analytics.backend.facade.contracts import (GitHubSource,GitHubTeamRoster,JobLogInsights,+ LLMSpendCoverage,MasterFailureGroup,MergedPullRequest,PRCostSummary,@@ -290,6 +292,44 @@ def list_author_workflow_costs()+def list_author_llm_spend(+ *,+ team: Team,+ author: str,+ date_from: str | None = None,+ date_to: str | None = None,+ limit: int | None = None,+ source_id: str | None = None,+ repo: str | None = None,+ user_access_control: "UserAccessControl | None" = None,+) -> AuthorLLMSpendList:+ return logic.build_author_llm_spend(+ curated=_authorized_source(team, source_id, user_access_control, repo=repo),+ author=author,+ repo=repo,+ date_from=date_from,+ date_to=date_to,+ limit=limit,+ )+++def get_llm_spend_coverage(+ *,+ team: Team,+ date_from: str | None = None,+ date_to: str | None = None,+ source_id: str | None = None,+ repo: str | None = None,+ user_access_control: "UserAccessControl | None" = None,+) -> LLMSpendCoverage:+ return logic.build_llm_spend_coverage(+ curated=_authorized_source(team, source_id, user_access_control, repo=repo),+ repo=repo,+ date_from=date_from,+ date_to=date_to,+ )++def get_author_friction(*,team: Team,diff --git a/products/engineering_analytics/backend/facade/contracts.py b/products/engineering_analytics/backend/facade/contracts.pyindex df44b22ac835..9176c5a4f89c 100644--- a/products/engineering_analytics/backend/facade/contracts.py+++ b/products/engineering_analytics/backend/facade/contracts.py@@ -677,6 +677,34 @@ class PRLLMSpend:input_tokens: intoutput_tokens: intgenerations: int+ cache_read_input_tokens: int+ cache_creation_input_tokens: int+ peak_context_tokens: int+++@dataclass(frozen=True)+class AuthorPRLLMSpend:+ number: int+ title: str+ state: PRState+ head_branch: str+ created_at: datetime+ llm_spend: PRLLMSpend | None+++@dataclass(frozen=True)+class AuthorLLMSpendList:+ items: list[AuthorPRLLMSpend]+ truncated: bool+ limit: int+++@dataclass(frozen=True)+class LLMSpendCoverage:+ cost_usd: float+ attributed_cost_usd: float+ generations: int+ attributed_generations: int@dataclass(frozen=True)diff --git a/products/engineering_analytics/backend/logic/__init__.py b/products/engineering_analytics/backend/logic/__init__.pyindex 535bbc1786a9..00ba27925050 100644--- a/products/engineering_analytics/backend/logic/__init__.py+++ b/products/engineering_analytics/backend/logic/__init__.py@@ -44,9 +44,11 @@ from products.engineering_analytics.backend.logic.job_log_insights import ()from products.engineering_analytics.backend.logic.pull_requests import (build_attention_pull_requests as build_attention_pull_requests,+ build_author_llm_spend as build_author_llm_spend,build_author_workflow_costs as build_author_workflow_costs,build_ci_cards as build_ci_cards,build_ci_failure_logs as build_ci_failure_logs,+ build_llm_spend_coverage as build_llm_spend_coverage,build_merged_pull_requests as build_merged_pull_requests,build_pr_cost as build_pr_cost,build_pr_lifecycle as build_pr_lifecycle,diff --git a/products/engineering_analytics/backend/logic/pull_requests.py b/products/engineering_analytics/backend/logic/pull_requests.pyindex 7a8c272bab63..d91a884d353b 100644--- a/products/engineering_analytics/backend/logic/pull_requests.py+++ b/products/engineering_analytics/backend/logic/pull_requests.py@@ -3,13 +3,17 @@from dataclasses import replacefrom datetime import datetime+from django.utils import timezone+import structlogfrom products.engineering_analytics.backend.facade.contracts import (AttentionPullRequestList,+ AuthorLLMSpendList,BranchPRMatch,CICardSummary,CIFailureLogs,+ LLMSpendCoverage,MergedPullRequest,PRCostSummary,PRLifecycle,@@ -28,7 +32,11 @@ from products.engineering_analytics.backend.logic._shared import (from products.engineering_analytics.backend.logic.queries._curated import CuratedGitHubSourcefrom products.engineering_analytics.backend.logic.queries.ci_cards import query_ci_cardsfrom products.engineering_analytics.backend.logic.queries.ci_failure_logs import query_ci_failure_logs-from products.engineering_analytics.backend.logic.queries.llm_spend import query_pr_llm_spend+from products.engineering_analytics.backend.logic.queries.llm_spend import (+ query_author_llm_spend,+ query_llm_spend_coverage,+ query_pr_llm_spend,+)from products.engineering_analytics.backend.logic.queries.merged_pull_requests import query_merged_pull_requestsfrom products.engineering_analytics.backend.logic.queries.pr_cost import query_author_workflow_costs, query_pr_costfrom products.engineering_analytics.backend.logic.queries.pr_lifecycle import query_pr_lifecycle@@ -101,6 +109,52 @@ def build_author_workflow_costs(return query_author_workflow_costs(curated=curated, author=author.strip(), date_from=parsed_from, date_to=parsed_to)+_AUTHOR_LLM_SPEND_DEFAULT_LIMIT = 100+_AUTHOR_LLM_SPEND_MAX_LIMIT = 200+++def build_author_llm_spend(+ *,+ curated: CuratedGitHubSource,+ author: str,+ repo: str | None = None,+ date_from: str | None = None,+ date_to: str | None = None,+ limit: int | None = None,+) -> AuthorLLMSpendList:+ if not author.strip():+ raise ValueError("author is required")+ owner, name = _require_repo(repo or curated.repository)+ parsed_from, parsed_to = _parse_window(curated.team, date_from, date_to, default=_DEFAULT_WINDOW)+ return query_author_llm_spend(+ curated=curated,+ author=author.strip(),+ repo_owner=owner,+ repo_name=name,+ date_from=parsed_from,+ date_to=parsed_to or timezone.now(),+ limit=min(limit or _AUTHOR_LLM_SPEND_DEFAULT_LIMIT, _AUTHOR_LLM_SPEND_MAX_LIMIT),+ )+++def build_llm_spend_coverage(+ *,+ curated: CuratedGitHubSource,+ repo: str | None = None,+ date_from: str | None = None,+ date_to: str | None = None,+) -> LLMSpendCoverage:+ owner, name = _require_repo(repo or curated.repository)+ parsed_from, parsed_to = _parse_window(curated.team, date_from, date_to, default=_DEFAULT_WINDOW)+ return query_llm_spend_coverage(+ curated=curated,+ repo_owner=owner,+ repo_name=name,+ date_from=parsed_from,+ date_to=parsed_to or timezone.now(),+ )++def build_ci_cards(*, curated: CuratedGitHubSource) -> CICardSummary:return query_ci_cards(curated=curated)diff --git a/products/engineering_analytics/backend/logic/queries/llm_spend.py b/products/engineering_analytics/backend/logic/queries/llm_spend.pyindex 23929cbe1f50..983e7d1ddcb0 100644--- a/products/engineering_analytics/backend/logic/queries/llm_spend.py+++ b/products/engineering_analytics/backend/logic/queries/llm_spend.py@@ -1,4 +1,4 @@-"""HogQL for the agent LLM token spend attributed to one PR.+"""HogQL for the agent LLM token spend attributed to PRs.Attribution is by git **branch**, not head SHA: a coding agent stamps ``$ai_git_branch`` on its``$ai_generation`` events at capture time — before the PR exists — and the ``github_pull_requests``@@ -23,16 +23,25 @@ for a session whose first non-base branch stamp is this PR's head ref H, the folBase-ref stamps at or after the first feature stamp are neutral, not credited: switching back to thebase branch ends attribution. Events stamped with any other branch never count toward H. Events with nosession and no trace id have no group, so they can only count via their own direct H stamp. The window-and repo guard stay the OUTER filter on every counted event: only in-window candidates form the groups.+and repo guard stay the OUTER filter on every counted event. Each PR evaluates a group over the stamps+inside its own window, so a batch read whose scan spans several PRs credits each PR exactly what its+single-PR read does."""-from datetime import timedelta+from collections.abc import Sequence+from datetime import datetime, timedeltafrom django.utils import timezonefrom posthog.hogql import ast-from products.engineering_analytics.backend.facade.contracts import PRLLMSpend+from products.engineering_analytics.backend.facade.contracts import (+ AuthorLLMSpendList,+ AuthorPRLLMSpend,+ LLMSpendCoverage,+ PRLLMSpend,+ PRState,+)from products.engineering_analytics.backend.logic.queries._curated import CuratedGitHubSourcefrom products.engineering_analytics.backend.logic.queries._pr_header import pr_header_placeholders, pr_header_query@@ -44,6 +53,46 @@ _LEAD_DAYS = 14_HEADER = pr_header_query("head_branch, base_branch, created_at, merged_at, closed_at")+# The newest snapshot row per PR, with the same tie-break as _pr_header (updated_at, then created_at).+# argMax skips a NULL argument, so a reopened PR would keep an older snapshot's closure date; taking the+# whole row as one tuple keeps the newest snapshot's NULLs. __PR_FILTER__ prunes raw snapshots and must+# only test immutable columns; __PR_WINDOW__ tests the newest snapshot, because an older open snapshot of+# a since-closed PR would otherwise pass a window check its current row fails.+# A PR is dropped when it has no branch to join on, no created_at to place the window (created_at comes+# from parseDateTimeBestEffort, which yields NULL on a malformed value), or a head that is its own base+# (a fork PR from default to default): the direct rule would sweep every default-branch generation in+# the window onto it, and no attribution beats wrong attribution. Open PRs are still accruing spend, so+# their window ends at now(); a closed/merged PR's ends at its close.+_PRS = """+ prs AS (+ SELECT+ number,+ head_branch,+ base_branch,+ opened_at - toIntervalDay({lead_days}) AS window_start,+ coalesce(ended_at, now()) AS window_end+ FROM (+ SELECT+ number,+ argMax(+ tuple(pr.head_branch, pr.base_branch, pr.created_at, coalesce(pr.merged_at, pr.closed_at)),+ version+ ) AS latest,+ tupleElement(latest, 1) AS head_branch,+ tupleElement(latest, 2) AS base_branch,+ tupleElement(latest, 3) AS opened_at,+ tupleElement(latest, 4) AS ended_at+ FROM (+ SELECT *, tuple(updated_at, created_at) AS version+ FROM __PR_SOURCE__+ WHERE lower(repo_owner) = {repo_owner} AND lower(repo_name) = {repo_name} AND __PR_FILTER__+ ) AS pr+ GROUP BY number+ )+ WHERE head_branch != '' AND head_branch != base_branch AND opened_at IS NOT NULL AND __PR_WINDOW__+ )+"""+# The repo guard keeps events that stamped no $ai_git_repo (older agents that only carried the branch)# while still rejecting a same-named branch in a different repo once the repo is stamped. The compare# is case-insensitive: GitHub repo names are, and the stamped slug comes from a clone URL whose casing@@ -53,69 +102,236 @@ _HEADER = pr_header_query("head_branch, base_branch, created_at, merged_at, clos_CANDIDATES = """candidates AS (SELECT+ uuid,timestamp AS ts,coalesce(properties.$ai_git_branch, '') AS branch,+ lower(coalesce(properties.$ai_git_repo, '')) AS repo,coalesce(nullIf(properties.$ai_session_id, ''), nullIf(properties.$ai_trace_id, ''), '') AS group_key,toFloat(properties.$ai_total_cost_usd) AS cost,- toInt(properties.$ai_input_tokens) AS input_tokens,- toInt(properties.$ai_output_tokens) AS output_tokens+ coalesce(toInt(properties.$ai_input_tokens), 0) AS input_tokens,+ coalesce(toInt(properties.$ai_output_tokens), 0) AS output_tokens,+ coalesce(toInt(properties.$ai_cache_read_input_tokens), 0) AS cache_read_input_tokens,+ coalesce(toInt(properties.$ai_cache_creation_input_tokens), 0) AS cache_creation_input_tokensFROM eventsWHERE event = '$ai_generation'AND (coalesce(properties.$ai_git_repo, '') = '' OR lower(properties.$ai_git_repo) = {repo_full})- AND timestamp >= {window_start}- AND timestamp <= {window_end}+ AND timestamp >= {scan_start}+ AND timestamp <= {scan_end})"""-# Eligible groups only: a group counts toward H once it holds at least one H stamp (the HAVING). Per-# group, `stamps` is the timestamp-sorted array of (ts, branch) for its stamped events; `feature` is-# the first stamp whose branch is not the base ref M — the "first feature stamp" that anchors the-# prefix (before it) and the carry-forward (at/after it).+# One row per (group, PR) where the group stamps the PR's head inside the PR's window. Per row, `stamps`+# is the timestamp-sorted array of (ts, branch) for the group's stamped events inside that window;+# `feature` is the first stamp whose branch is not the PR's base ref — the "first feature stamp" that+# anchors the prefix (before it) and the carry-forward (at/after it). Stamps outside the window are cut+# before `feature` is picked, because a batch scan spans every requested PR and an out-of-window feature+# stamp would otherwise turn this PR's in-window base-ref prefix neutral._GROUP_STAMPS = """group_stamps AS (SELECTgroup_key,+ number,+ base,stamps,tupleElement(feature, 1) AS feature_ts,tupleElement(feature, 2) AS feature_branchFROM (SELECTgroup_key,- arraySort(x -> tupleElement(x, 1), groupArray(tuple(ts, branch))) AS stamps,- arrayFirst(s -> tupleElement(s, 2) != {base}, stamps) AS feature- FROM candidates- WHERE branch != '' AND group_key != ''- GROUP BY group_key- HAVING countIf(branch = {branch}) > 0+ number,+ base,+ stamps,+ arrayFirst(s -> tupleElement(s, 2) != base, stamps) AS feature+ FROM (+ SELECT+ g.group_key AS group_key,+ p.number AS number,+ p.head_branch AS head,+ p.base_branch AS base,+ arrayFilter(+ s -> tupleElement(s, 1) >= p.window_start AND tupleElement(s, 1) <= p.window_end, g.stamps+ ) AS stamps+ FROM (+ SELECT group_key, stamps, arrayJoin(branches) AS stamped_branch+ FROM (+ SELECT+ group_key,+ arraySort(x -> tupleElement(x, 1), groupArray(tuple(ts, branch))) AS stamps,+ groupUniqArray(branch) AS branches+ FROM candidates+ WHERE branch != '' AND group_key != ''+ GROUP BY group_key+ )+ ) AS g+ INNER JOIN prs AS p ON g.stamped_branch = p.head_branch+ )+ WHERE has(arrayMap(s -> tupleElement(s, 2), stamps), head)))"""-# One pass over candidates (ClickHouse re-evaluates a WITH subquery per reference, so a UNION of-# candidate scans would read events once per arm). Each candidate row counts at most once, when any-# rule matches: its own H stamp (rule 1), or — via its eligible group's stamps — the prefix (rule 2)-# or the carry-forward (rule 3). LEFT ANY JOIN because group_stamps is unique per key; ungrouped rows-# and rows in ineligible groups join to nothing (g.group_key = ''), so only rule 1 can pass for them.-# Every counted event flows from `candidates`, which already pins the window and repo guard.-_SPEND = """- WITH- __CANDIDATES__,- __GROUP_STAMPS__+# Each candidate yields up to two attribution keys of (branch, PR number): its own stamp, valid for any+# PR with that head (rule 1, number 0), and, per PR its group was evaluated for, the branch the prefix+# (rule 2) or the carry-forward (rule 3) credits for that PR. The two keys never name the same PR for one+# event: the derived key is empty whenever the event is stamped with a non-base branch. Ungrouped+# candidates join to no group_stamps row (g.group_key = ''), so only their own stamp can match. The+# DISTINCT collapses the copies a candidate gets from being evaluated for several PRs, so each event+# counts once per PR. Every counted event flows from `candidates`, which already pins the scan window+# and repo guard; the PR's own window is checked here.+_MATCHED = """+ matched AS (+ SELECT DISTINCT+ p.number AS number,+ a.uuid AS uuid,+ a.cost AS cost,+ a.input_tokens AS input_tokens,+ a.output_tokens AS output_tokens,+ a.cache_read_input_tokens AS cache_read_input_tokens,+ a.cache_creation_input_tokens AS cache_creation_input_tokens+ FROM (+ SELECT+ c.uuid AS uuid,+ c.ts AS ts,+ c.cost AS cost,+ c.input_tokens AS input_tokens,+ c.output_tokens AS output_tokens,+ c.cache_read_input_tokens AS cache_read_input_tokens,+ c.cache_creation_input_tokens AS cache_creation_input_tokens,+ arrayJoin([+ tuple(c.branch, 0),+ tuple(+ multiIf(+ g.group_key = '', '',+ (c.branch = '' OR c.branch = g.base) AND c.ts < g.feature_ts, g.feature_branch,+ c.branch = '' AND c.ts >= g.feature_ts,+ tupleElement(arrayLast(s -> tupleElement(s, 1) <= c.ts, g.stamps), 2),+ ''+ ),+ g.number+ )+ ]) AS attribution+ FROM candidates AS c+ LEFT JOIN group_stamps AS g ON c.group_key = g.group_key+ ) AS a+ INNER JOIN prs AS p ON tupleElement(a.attribution, 1) = p.head_branch+ WHERE (tupleElement(a.attribution, 2) = 0 OR tupleElement(a.attribution, 2) = p.number)+ AND a.ts >= p.window_start+ AND a.ts <= p.window_end+ )+"""++_WITH = "WITH " + ",".join([_PRS, _CANDIDATES, _GROUP_STAMPS, _MATCHED])++_SPEND_BY_PR = (+ _WITH+ + """SELECT- sum(c.cost) AS cost_usd,- sum(c.input_tokens) AS input_tokens,- sum(c.output_tokens) AS output_tokens,- count() AS generations+ m.number,+ sum(m.cost),+ sum(m.input_tokens),+ sum(m.output_tokens),+ sum(m.cache_read_input_tokens),+ sum(m.cache_creation_input_tokens),+ max(m.input_tokens + m.cache_read_input_tokens + m.cache_creation_input_tokens),+ count()+ FROM matched AS m+ GROUP BY m.number+ LIMIT {result_limit}+"""+)++_COVERAGE = (+ _WITH+ + """+ SELECT+ sum(c.cost),+ sumIf(c.cost, c.uuid IN (SELECT uuid FROM matched)),+ count(),+ countIf(c.uuid IN (SELECT uuid FROM matched))FROM candidates AS c- LEFT ANY JOIN group_stamps AS g ON c.group_key = g.group_key- WHERE c.branch = {branch}- OR (g.group_key != ''- AND (- ((c.branch = '' OR c.branch = {base}) AND c.ts < g.feature_ts AND g.feature_branch = {branch})- OR (c.branch = '' AND c.ts >= g.feature_ts- AND tupleElement(arrayLast(s -> tupleElement(s, 1) <= c.ts, g.stamps), 2) = {branch})- ))-""".replace("__CANDIDATES__", _CANDIDATES).replace("__GROUP_STAMPS__", _GROUP_STAMPS)+ WHERE c.repo = {repo_full}+"""+)++_AUTHOR_PRS = """+ SELECT number, title, state, head_branch, opened_at, ended_at+ FROM (+ SELECT+ number,+ argMax(+ tuple(pr.title, pr.state, pr.head_branch, pr.created_at, coalesce(pr.merged_at, pr.closed_at)),+ version+ ) AS latest,+ tupleElement(latest, 1) AS title,+ tupleElement(latest, 2) AS state,+ tupleElement(latest, 3) AS head_branch,+ tupleElement(latest, 4) AS opened_at,+ tupleElement(latest, 5) AS ended_at+ FROM (+ SELECT *, tuple(updated_at, created_at) AS version+ FROM __PR_SOURCE__+ WHERE lower(repo_owner) = {repo_owner} AND lower(repo_name) = {repo_name} AND author_handle = {author}+ ) AS pr+ GROUP BY number+ )+ WHERE opened_at IS NOT NULL AND opened_at <= {date_to} AND coalesce(ended_at, now()) >= {date_from}+ ORDER BY opened_at DESC, number DESC+ LIMIT {limit}+"""+++def _attribution_placeholders(+ *, repo_owner: str, repo_name: str, scan_start: datetime, scan_end: datetime+) -> dict[str, ast.Expr]:+ return {+ "repo_owner": ast.Constant(value=repo_owner.lower()),+ "repo_name": ast.Constant(value=repo_name.lower()),+ "repo_full": ast.Constant(value=f"{repo_owner}/{repo_name}".lower()),+ "lead_days": ast.Constant(value=_LEAD_DAYS),+ "scan_start": ast.Constant(value=scan_start),+ "scan_end": ast.Constant(value=scan_end),+ }+++def query_llm_spend_by_pr(+ *,+ curated: CuratedGitHubSource,+ repo_owner: str,+ repo_name: str,+ pr_numbers: Sequence[int],+ scan_start: datetime,+ scan_end: datetime,+) -> dict[int, PRLLMSpend]:+ if not pr_numbers:+ return {}+ placeholders = _attribution_placeholders(+ repo_owner=repo_owner, repo_name=repo_name, scan_start=scan_start, scan_end=scan_end+ )+ placeholders["pr_numbers"] = ast.Tuple(exprs=[ast.Constant(value=number) for number in pr_numbers])+ placeholders["result_limit"] = ast.Constant(value=len(pr_numbers))+ response = curated.run(+ _SPEND_BY_PR.replace("__PR_SOURCE__", curated.pr_source())+ .replace("__PR_FILTER__", "number IN {pr_numbers}")+ .replace("__PR_WINDOW__", "true"),+ query_type="engineering_analytics.pr_llm_spend",+ placeholders=placeholders,+ )+ spend: dict[int, PRLLMSpend] = {}+ for number, cost_usd, input_tokens, output_tokens, cache_read, cache_creation, peak, generations in (+ response.results or []+ ):+ if not generations:+ continue+ spend[int(number)] = PRLLMSpend(+ cost_usd=float(cost_usd or 0.0),+ input_tokens=int(input_tokens or 0),+ output_tokens=int(output_tokens or 0),+ generations=int(generations),+ cache_read_input_tokens=int(cache_read or 0),+ cache_creation_input_tokens=int(cache_creation or 0),+ peak_context_tokens=int(peak or 0),+ )+ return spenddef query_pr_llm_spend(@@ -133,42 +349,100 @@ def query_pr_llm_spend(if not header.results:return Nonehead_branch, base_branch, created_at, merged_at, closed_at = header.results[0]- # No branch means nothing to join on; no created_at means the window can't be placed (created_at- # comes from parseDateTimeBestEffort, which yields NULL on a malformed value).- if not head_branch or created_at is None:- return None- # A PR whose head is its own base (a fork PR from default to default) can't be branch-attributed:- # the direct rule would sweep every default-branch generation in the window onto it. No- # attribution beats wrong attribution.- if head_branch == base_branch:+ if not head_branch or created_at is None or head_branch == base_branch:return None+ # None when nothing matched, so the endpoint returns llm_spend=null and the UI hides the row.+ return query_llm_spend_by_pr(+ curated=curated,+ repo_owner=repo_owner,+ repo_name=repo_name,+ pr_numbers=[pr_number],+ scan_start=created_at - timedelta(days=_LEAD_DAYS),+ scan_end=merged_at or closed_at or timezone.now(),+ ).get(pr_number)- # Open PRs are still accruing spend, so cap at now(); a closed/merged PR caps at its close.- window_end = merged_at or closed_at or timezone.now()++def query_author_llm_spend(+ *,+ curated: CuratedGitHubSource,+ author: str,+ repo_owner: str,+ repo_name: str,+ date_from: datetime,+ date_to: datetime,+ limit: int,+) -> AuthorLLMSpendList:response = curated.run(- _SPEND,- query_type="engineering_analytics.pr_llm_spend",+ _AUTHOR_PRS.replace("__PR_SOURCE__", curated.pr_source()),+ query_type="engineering_analytics.author_llm_spend.prs",placeholders={- "branch": ast.Constant(value=head_branch),- # base_branch can be empty on a malformed snapshot; '' is a branch no event ever stamps, so- # the base-neutral prefix rule simply falls back to "unstamped only", never mis-crediting.- "base": ast.Constant(value=base_branch or ""),- "repo_full": ast.Constant(value=f"{repo_owner}/{repo_name}".lower()),- "window_start": ast.Constant(value=created_at - timedelta(days=_LEAD_DAYS)),- "window_end": ast.Constant(value=window_end),+ "repo_owner": ast.Constant(value=repo_owner.lower()),+ "repo_name": ast.Constant(value=repo_name.lower()),+ "author": ast.Constant(value=author),+ "date_from": ast.Constant(value=date_from),+ "date_to": ast.Constant(value=date_to),+ "limit": ast.Constant(value=limit + 1),},)- rows = response.results or []+ rows = list(response.results or [])+ truncated = len(rows) > limit+ rows = rows[:limit]if not rows:- return None- cost_usd, input_tokens, output_tokens, generations = rows[0]- generations = int(generations or 0)- # None when nothing matched, so the endpoint returns llm_spend=null and the UI hides the row.- if generations == 0:- return None- return PRLLMSpend(+ return AuthorLLMSpendList(items=[], truncated=False, limit=limit)++ now = timezone.now()+ spend = query_llm_spend_by_pr(+ curated=curated,+ repo_owner=repo_owner,+ repo_name=repo_name,+ pr_numbers=[row[0] for row in rows],+ scan_start=min(row[4] for row in rows) - timedelta(days=_LEAD_DAYS),+ scan_end=max(row[5] or now for row in rows),+ )+ return AuthorLLMSpendList(+ items=[+ AuthorPRLLMSpend(+ number=int(number),+ title=title or "",+ state=PRState(state),+ head_branch=head_branch or "",+ created_at=created_at,+ llm_spend=spend.get(int(number)),+ )+ for number, title, state, head_branch, created_at, _ended_at in rows+ ],+ truncated=truncated,+ limit=limit,+ )+++def query_llm_spend_coverage(+ *,+ curated: CuratedGitHubSource,+ repo_owner: str,+ repo_name: str,+ date_from: datetime,+ date_to: datetime,+) -> LLMSpendCoverage:+ placeholders = _attribution_placeholders(+ repo_owner=repo_owner, repo_name=repo_name, scan_start=date_from, scan_end=date_to+ )+ placeholders["date_from"] = ast.Constant(value=date_from)+ placeholders["date_to"] = ast.Constant(value=date_to)+ response = curated.run(+ _COVERAGE.replace("__PR_SOURCE__", curated.pr_source())+ .replace("__PR_FILTER__", "created_at - toIntervalDay({lead_days}) <= {date_to}")+ .replace(+ "__PR_WINDOW__",+ "opened_at - toIntervalDay({lead_days}) <= {date_to} AND coalesce(ended_at, now()) >= {date_from}",+ ),+ query_type="engineering_analytics.llm_spend_coverage",+ placeholders=placeholders,+ )+ cost_usd, attributed_cost_usd, generations, attributed_generations = (response.results or [[0, 0, 0, 0]])[0]+ return LLMSpendCoverage(cost_usd=float(cost_usd or 0.0),- input_tokens=int(input_tokens or 0),- output_tokens=int(output_tokens or 0),- generations=generations,+ attributed_cost_usd=float(attributed_cost_usd or 0.0),+ generations=int(generations or 0),+ attributed_generations=int(attributed_generations or 0),)diff --git a/products/engineering_analytics/backend/presentation/serializers/pull_requests.py b/products/engineering_analytics/backend/presentation/serializers/pull_requests.pyindex e7d37614a11c..c6f442dc469b 100644--- a/products/engineering_analytics/backend/presentation/serializers/pull_requests.py+++ b/products/engineering_analytics/backend/presentation/serializers/pull_requests.py@@ -5,10 +5,13 @@ from rest_framework_dataclasses.serializers import DataclassSerializerfrom products.engineering_analytics.backend.facade.contracts import (AttentionPullRequestList,Author,+ AuthorLLMSpendList,+ AuthorPRLLMSpend,BranchPRMatch,CICardSummary,CIFailureLogs,CIStatusRollup,+ LLMSpendCoverage,PRCostSummary,PRLifecycle,PRLifecycleEvent,@@ -155,6 +158,69 @@ class PRLLMSpendSerializer(DataclassSerializer):"generations": {"help_text": "Number of $ai_generation events attributed to this PR by git branch ($ai_git_branch).",},+ "cache_read_input_tokens": {+ "help_text": "Total prompt tokens read from the provider's prompt cache across the attributed "+ "generations ($ai_cache_read_input_tokens). Not included in input_tokens.",+ },+ "cache_creation_input_tokens": {+ "help_text": "Total prompt tokens written to the provider's prompt cache across the attributed "+ "generations ($ai_cache_creation_input_tokens). Not included in input_tokens.",+ },+ "peak_context_tokens": {+ "help_text": "Largest prompt any single attributed generation sent: input plus cache read plus "+ "cache write tokens.",+ },+ }+++class AuthorPRLLMSpendSerializer(DataclassSerializer):+ llm_spend = PRLLMSpendSerializer(+ allow_null=True,+ help_text="Agent LLM token spend attributed to this pull request, by the same rule as `pr_cost`, or null "+ "when no generation matched.",+ )++ class Meta:+ dataclass = AuthorPRLLMSpend+ extra_kwargs = {+ "number": {"help_text": "Pull request number."},+ "title": {"help_text": "Pull request title."},+ "state": {"help_text": "Pull request state: open, closed, or merged."},+ "head_branch": {"help_text": "The pull request's head branch, which attribution joins on."},+ "created_at": {"help_text": "When the pull request was opened."},+ }+++class AuthorLLMSpendListSerializer(DataclassSerializer):+ items = AuthorPRLLMSpendSerializer(+ many=True,+ help_text="The author's pull requests active in the window, newest first, capped at `limit`. Each "+ "spend covers the pull request's whole attribution window, so it can include spend from before "+ "date_from.",+ )++ class Meta:+ dataclass = AuthorLLMSpendList+ extra_kwargs = {+ "truncated": {"help_text": "True when more pull requests matched than `limit`."},+ "limit": {"help_text": "Maximum number of pull requests returned in `items`."},+ }+++class LLMSpendCoverageSerializer(DataclassSerializer):+ class Meta:+ dataclass = LLMSpendCoverage+ extra_kwargs = {+ "cost_usd": {+ "help_text": "Total cost in USD of the $ai_generation events stamped with this repository "+ "($ai_git_repo) in the window. Generations with no repository stamp are not counted.",+ },+ "attributed_cost_usd": {+ "help_text": "The part of cost_usd that the branch and session attribution credits to a pull "+ "request. A generation credited to several pull requests counts once.",+ },+ "generations": {"help_text": "Number of $ai_generation events in cost_usd."},+ "attributed_generations": {"help_text": "Number of $ai_generation events in attributed_cost_usd."},}diff --git a/products/engineering_analytics/backend/presentation/views/pull_requests.py b/products/engineering_analytics/backend/presentation/views/pull_requests.pyindex 7b08c9aa2d9f..b1a7468eef4c 100644--- a/products/engineering_analytics/backend/presentation/views/pull_requests.py+++ b/products/engineering_analytics/backend/presentation/views/pull_requests.py@@ -11,9 +11,11 @@ from products.engineering_analytics.backend.facade import apifrom products.engineering_analytics.backend.facade.contracts import PRStatefrom products.engineering_analytics.backend.presentation.serializers.pull_requests import (AttentionPullRequestListSerializer,+ AuthorLLMSpendListSerializer,BranchPRMatchSerializer,CICardSummarySerializer,CIFailureLogsSerializer,+ LLMSpendCoverageSerializer,PRCostSummarySerializer,PRLifecycleSerializer,PullRequestListSerializer,@@ -44,6 +46,8 @@ class PullRequestActionsMixin(EngineeringAnalyticsViewSetBase):"ci_failure_logs","pr_cost","author_workflow_costs",+ "author_llm_spend",+ "llm_spend_coverage",]@extend_schema(@@ -479,3 +483,85 @@ class PullRequestActionsMixin(EngineeringAnalyticsViewSetBase):except ValueError as exc:return _bad_request(exc, fallback="Invalid author, date, or source_id")return Response(WorkflowCostSerializer(instance=costs, many=True).data)++ @extend_schema(+ operation_id="engineering_analytics_author_llm_spend",+ parameters=[+ OpenApiParameter(+ name="author",+ type=OpenApiTypes.STR,+ location=OpenApiParameter.QUERY,+ required=True,+ description="GitHub handle whose pull requests to list.",+ ),+ _DATE_FROM,+ _DATE_TO,+ OpenApiParameter(+ name="limit",+ type=OpenApiTypes.INT,+ location=OpenApiParameter.QUERY,+ required=False,+ description="Maximum pull requests to return, 1 to 200. Defaults to 100.",+ ),+ _SOURCE_ID,+ _REPO,+ ],+ responses={+ 200: AuthorLLMSpendListSerializer,+ 400: OpenApiResponse(description="Missing author, or invalid date, limit, repo or source_id."),+ },+ description=(+ "One author's pull requests active in a window (date_from default -30d), newest first, each with the "+ "agent LLM token spend attributed to it by git branch and AI session, the same rule as `pr_cost`."+ ),+ )+ @action(detail=False, methods=["get"], pagination_class=None)+ def author_llm_spend(self, request: Request, **kwargs) -> Response:+ author = request.query_params.get("author")+ if not author:+ return Response({"detail": "author is required"}, status=status.HTTP_400_BAD_REQUEST)+ try:+ limit = _optional_int_param(request, "limit")+ if limit is not None and limit < 1:+ raise ValueError("limit must be at least 1")+ result = api.list_author_llm_spend(+ team=self.team,+ author=author,+ date_from=request.query_params.get("date_from") or None,+ date_to=request.query_params.get("date_to") or None,+ limit=limit,+ source_id=request.query_params.get("source_id") or None,+ repo=request.query_params.get("repo") or None,+ user_access_control=self.user_access_control,+ )+ except ValueError as exc:+ return _bad_request(exc, fallback="Invalid author, date, limit, repo or source_id")+ return Response(AuthorLLMSpendListSerializer(instance=result).data)++ @extend_schema(+ operation_id="engineering_analytics_llm_spend_coverage",+ parameters=[_DATE_FROM, _DATE_TO, _SOURCE_ID, _REPO],+ responses={+ 200: LLMSpendCoverageSerializer,+ 400: OpenApiResponse(description="Invalid date, repo or source_id."),+ },+ description=(+ "How much of a repository's agent LLM spend in a window (date_from default -30d) the pull request "+ "attribution reaches: the cost and count of $ai_generation events stamped with the repository, and "+ "the part credited to any pull request."+ ),+ )+ @action(detail=False, methods=["get"], pagination_class=None)+ def llm_spend_coverage(self, request: Request, **kwargs) -> Response:+ try:+ result = api.get_llm_spend_coverage(+ team=self.team,+ date_from=request.query_params.get("date_from") or None,+ date_to=request.query_params.get("date_to") or None,+ source_id=request.query_params.get("source_id") or None,+ repo=request.query_params.get("repo") or None,+ user_access_control=self.user_access_control,+ )+ except ValueError as exc:+ return _bad_request(exc, fallback="Invalid date, repo or source_id")+ return Response(LLMSpendCoverageSerializer(instance=result).data)diff --git a/products/engineering_analytics/backend/tests/test_logic_pull_requests.py b/products/engineering_analytics/backend/tests/test_logic_pull_requests.pyindex 4750f40eed06..e3ce6740e148 100644--- a/products/engineering_analytics/backend/tests/test_logic_pull_requests.py+++ b/products/engineering_analytics/backend/tests/test_logic_pull_requests.py@@ -756,6 +756,8 @@ class TestPRLLMSpendWarehouse(_WarehouseMixin, BaseTest):cost: float,input_tokens: int = 0,output_tokens: int = 0,+ cache_read_tokens: int | None = 0,+ cache_creation_tokens: int | None = 0,repo: str | None = None,session: str | None = None,trace: str | None = None,@@ -768,6 +770,10 @@ class TestPRLLMSpendWarehouse(_WarehouseMixin, BaseTest):"$ai_input_tokens": input_tokens,"$ai_output_tokens": output_tokens,}+ if cache_read_tokens is not None:+ props["$ai_cache_read_input_tokens"] = cache_read_tokens+ if cache_creation_tokens is not None:+ props["$ai_cache_creation_input_tokens"] = cache_creation_tokensif branch is not None:props["$ai_git_branch"] = branchif repo is not None:@@ -815,9 +821,33 @@ class TestPRLLMSpendWarehouse(_WarehouseMixin, BaseTest):self._seed_pr(80, branch)# Matches: on-branch, in-window; one with no repo stamped, one with the repo stamped in clone-URL# casing (the repo compare is case-insensitive, like GitHub repo names).- self._generation(branch=branch, days_ago=4, cost=1.0, input_tokens=100, output_tokens=50)self._generation(- branch=branch, days_ago=10, cost=2.0, input_tokens=200, output_tokens=80, repo="posthog/POSTHOG"+ branch=branch,+ days_ago=4,+ cost=1.0,+ input_tokens=100,+ output_tokens=50,+ cache_read_tokens=5000,+ cache_creation_tokens=700,+ )+ self._generation(+ branch=branch,+ days_ago=10,+ cost=2.0,+ input_tokens=200,+ output_tokens=80,+ cache_read_tokens=3000,+ cache_creation_tokens=2000,+ repo="posthog/POSTHOG",+ )+ # An agent that reports no cache usage omits both cache properties; it still sets the peak.+ self._generation(+ branch=branch,+ days_ago=3,+ cost=4.0,+ input_tokens=9000,+ cache_read_tokens=None,+ cache_creation_tokens=None,)# Excluded: wrong repo, wrong branch, before the lead window, after merge, wrong event type.self._generation(branch=branch, days_ago=4, cost=99.0, repo="other/repo")@@ -829,10 +859,130 @@ class TestPRLLMSpendWarehouse(_WarehouseMixin, BaseTest):cost = api.get_pr_cost(team=self.team, pr_number=80, repo="PostHog/posthog")assert cost.llm_spend is not None- assert cost.llm_spend.generations == 2- assert cost.llm_spend.cost_usd == pytest.approx(3.0)- assert cost.llm_spend.input_tokens == 300+ assert cost.llm_spend.generations == 3+ assert cost.llm_spend.cost_usd == pytest.approx(7.0)+ assert cost.llm_spend.input_tokens == 9300assert cost.llm_spend.output_tokens == 130+ assert cost.llm_spend.cache_read_input_tokens == 8000+ assert cost.llm_spend.cache_creation_input_tokens == 2700+ assert cost.llm_spend.peak_context_tokens == 9000++ def _seed_stacked_prs(self) -> None:+ self._create_table(+ "github_pull_requests",+ PULL_REQUESTS_COLUMNS,+ [+ _pr_row(+ 91,+ "alice",+ "closed",+ 0,+ _ago(5),+ merged_at=_ago(1),+ head_sha="sha91",+ head_ref="feat/a",+ base_ref="master",+ ),+ _pr_row(92, "alice", "open", 0, _ago(4), head_sha="sha92", head_ref="feat/b", base_ref="feat/a"),+ _pr_row(93, "bob", "open", 0, _ago(3), head_sha="sha93", head_ref="feat/c", base_ref="master"),+ # #94 closed before the coverage window; its older open snapshot must not keep it open.+ _pr_row(94, "bob", "open", 0, _ago(50), head_sha="sha94", head_ref="feat/stale", base_ref="master"),+ {+ **_pr_row(+ 94,+ "bob",+ "closed",+ 0,+ _ago(50),+ closed_at=_ago(40),+ head_sha="sha94",+ head_ref="feat/stale",+ base_ref="master",+ ),+ "updated_at": _ago(40),+ },+ ],+ )+ self._create_table(+ "github_workflow_runs",+ WORKFLOW_RUNS_COLUMNS,+ [_run_row(9100, "CI", "sha91", "completed", "success", _ago(4), _ago(4), pr_number=91)],+ )+ repo = "PostHog/posthog"+ # One session builds feat/a off master, then stacks feat/b on it. For #92 the base is feat/a, so its+ # first feature stamp is the master one and it takes no prefix; for #91 the prefix is the master stamp.+ self._generation(branch="master", days_ago=10, cost=1.0, session="s1", repo=repo)+ self._generation(branch="feat/a", days_ago=9, cost=2.0, session="s1", repo=repo)+ self._generation(branch=None, days_ago=8.5, cost=4.0, session="s1", repo=repo)+ self._generation(branch="feat/b", days_ago=8, cost=8.0, session="s1", repo=repo)+ self._generation(branch=None, days_ago=7, cost=16.0, session="s1", repo=repo)+ self._generation(branch="feat/c", days_ago=2, cost=32.0, session="s2", repo=repo)+ self._generation(branch="scratch", days_ago=2, cost=64.0, repo=repo)+ self._generation(branch="feat/stale", days_ago=5, cost=256.0, repo=repo)+ # No repo stamp: the repo guard still credits it to #91, but coverage cannot place it in a repo.+ self._generation(branch="feat/a", days_ago=6, cost=128.0)+ flush_persons_and_events()++ def test_author_llm_spend_matches_per_pr_attribution_across_stacked_bases(self) -> None:+ self._seed_stacked_prs()++ result = api.list_author_llm_spend(team=self.team, author="alice", repo="PostHog/posthog")++ assert [(item.number, item.head_branch) for item in result.items] == [(92, "feat/b"), (91, "feat/a")]+ spend = {item.number: item.llm_spend for item in result.items}+ assert {number: s.generations for number, s in spend.items() if s} == {91: 4, 92: 2}+ assert {number: s.cost_usd for number, s in spend.items() if s} == pytest.approx({91: 135.0, 92: 24.0})+ for number in (91, 92):+ assert spend[number] == api.get_pr_cost(team=self.team, pr_number=number, repo="PostHog/posthog").llm_spend++ @parameterized.expand([("canonical_casing", "PostHog/posthog"), ("lowercase", "posthog/posthog")])+ def test_author_llm_spend_matches_per_pr_attribution_across_pr_windows(self, _name: str, repo: str) -> None:+ self._create_table(+ "github_pull_requests",+ PULL_REQUESTS_COLUMNS,+ [+ _pr_row(95, "alice", "closed", 0, _ago(5), merged_at=_ago(1), head_ref="feat/x", base_ref="master"),+ # An older PR stretches the batch scan back past #95's window.+ _pr_row(96, "alice", "closed", 0, _ago(28), closed_at=_ago(26), head_ref="feat/y", base_ref="master"),+ # #97 closed, then reopened: the newest snapshot has no closure date.+ _pr_row(97, "alice", "closed", 0, _ago(10), closed_at=_ago(8), head_ref="feat/z", base_ref="master"),+ {+ **_pr_row(97, "alice", "open", 0, _ago(10), head_ref="feat/z", base_ref="master"),+ "updated_at": _ago(3),+ },+ ],+ )+ self._create_table(+ "github_workflow_runs",+ WORKFLOW_RUNS_COLUMNS,+ [_run_row(9500, "CI", "sha95", "completed", "success", _ago(4), _ago(4), pr_number=95)],+ )+ # A feat/x stamp before #95's window must not decide #95's first feature stamp, so the master+ # exploration inside the window stays #95's prefix.+ self._generation(branch="feat/x", days_ago=22, cost=1.0, session="s1")+ self._generation(branch="master", days_ago=15, cost=2.0, session="s1")+ self._generation(branch="feat/x", days_ago=10, cost=4.0, session="s1")+ self._generation(branch="feat/z", days_ago=5, cost=8.0, session="s2")+ flush_persons_and_events()++ result = api.list_author_llm_spend(team=self.team, author="alice", repo=repo)++ assert [item.number for item in result.items] == [95, 97, 96]+ spend = {item.number: item.llm_spend for item in result.items}+ assert {number: s.generations for number, s in spend.items() if s} == {95: 2, 97: 1}+ assert {number: s.cost_usd for number, s in spend.items() if s} == pytest.approx({95: 6.0, 97: 8.0})+ for number in (95, 97):+ assert spend[number] == api.get_pr_cost(team=self.team, pr_number=number, repo="PostHog/posthog").llm_spend++ @parameterized.expand([("canonical_casing", "PostHog/posthog"), ("lowercase", "posthog/posthog")])+ def test_llm_spend_coverage_counts_repo_stamped_generations_once(self, _name: str, repo: str) -> None:+ self._seed_stacked_prs()++ coverage = api.get_llm_spend_coverage(team=self.team, repo=repo)++ assert (coverage.generations, coverage.attributed_generations) == (8, 6)+ assert coverage.cost_usd == pytest.approx(383.0)+ assert coverage.attributed_cost_usd == pytest.approx(63.0)def test_llm_spend_none_when_head_is_base(self) -> None:self._seed_pr(84, "master", base_ref="master")diff --git a/products/engineering_analytics/backend/tests/test_presentation.py b/products/engineering_analytics/backend/tests/test_presentation.pyindex 701e89336f75..a6c2f59cfdf4 100644--- a/products/engineering_analytics/backend/tests/test_presentation.py+++ b/products/engineering_analytics/backend/tests/test_presentation.py@@ -581,7 +581,15 @@ class TestEngineeringAnalyticsAPI(APIBaseTest):by_run=[contracts.RunCost(run_id=9100, run_attempt=1, billable_minutes=2510.0, estimated_cost_usd=61.2),],- llm_spend=contracts.PRLLMSpend(cost_usd=1.5, input_tokens=1200, output_tokens=340, generations=4),+ llm_spend=contracts.PRLLMSpend(+ cost_usd=1.5,+ input_tokens=1200,+ output_tokens=340,+ generations=4,+ cache_read_input_tokens=90000,+ cache_creation_input_tokens=8000,+ peak_context_tokens=45000,+ ),)with mock.patch(f"{_VIEWS}.get_pr_cost", return_value=summary) as getter:response = self.client.get(self._url("pr_cost"), {"pr_number": "10", "repo": "PostHog/posthog"})@@ -595,6 +603,9 @@ class TestEngineeringAnalyticsAPI(APIBaseTest):"input_tokens": 1200,"output_tokens": 340,"generations": 4,+ "cache_read_input_tokens": 90000,+ "cache_creation_input_tokens": 8000,+ "peak_context_tokens": 45000,}assert getter.call_args.kwargs["pr_number"] == 10assert getter.call_args.kwargs["repo"] == "PostHog/posthog"diff --git a/products/engineering_analytics/frontend/generated/api.schemas.ts b/products/engineering_analytics/frontend/generated/api.schemas.tsindex dd2c25317afd..1566648eab2f 100644--- a/products/engineering_analytics/frontend/generated/api.schemas.ts+++ b/products/engineering_analytics/frontend/generated/api.schemas.ts@@ -247,6 +247,51 @@ export interface AuthorFrictionDetailApi {pr_count: number}+export interface PRLLMSpendApi {+ /** Total agent LLM token cost in USD attributed to this PR (sum of $ai_total_cost_usd over the matched $ai_generation events). */+ cost_usd: number+ /** Total input (prompt) tokens across the attributed generations. */+ input_tokens: number+ /** Total output (completion) tokens across the attributed generations. */+ output_tokens: number+ /** Number of $ai_generation events attributed to this PR by git branch ($ai_git_branch). */+ generations: number+ /** Total prompt tokens read from the provider's prompt cache across the attributed generations ($ai_cache_read_input_tokens). Not included in input_tokens. */+ cache_read_input_tokens: number+ /** Total prompt tokens written to the provider's prompt cache across the attributed generations ($ai_cache_creation_input_tokens). Not included in input_tokens. */+ cache_creation_input_tokens: number+ /** Largest prompt any single attributed generation sent: input plus cache read plus cache write tokens. */+ peak_context_tokens: number+}++export interface AuthorPRLLMSpendApi {+ /** Agent LLM token spend attributed to this pull request, by the same rule as `pr_cost`, or null when no generation matched. */+ llm_spend: PRLLMSpendApi | null+ /** Pull request number. */+ number: number+ /** Pull request title. */+ title: string+ /** Pull request state: open, closed, or merged.+ *+ * * `open` - Open+ * * `closed` - Closed+ * * `merged` - Merged */+ state: PRStateEnumApi+ /** The pull request's head branch, which attribution joins on. */+ head_branch: string+ /** When the pull request was opened. */+ created_at: string+}++export interface AuthorLLMSpendListApi {+ /** The author's pull requests active in the window, newest first, capped at `limit`. Each spend covers the pull request's whole attribution window, so it can include spend from before date_from. */+ items: AuthorPRLLMSpendApi[]+ /** True when more pull requests matched than `limit`. */+ truncated: boolean+ /** Maximum number of pull requests returned in `items`. */+ limit: number+}+export interface WorkflowCostApi {/** GitHub Actions workflow name this cost is for. */workflow_name: string@@ -1199,6 +1244,17 @@ export interface JobLogInsightsApi {log_truncated?: boolean}+export interface LLMSpendCoverageApi {+ /** Total cost in USD of the $ai_generation events stamped with this repository ($ai_git_repo) in the window. Generations with no repository stamp are not counted. */+ cost_usd: number+ /** The part of cost_usd that the branch and session attribution credits to a pull request. A generation credited to several pull requests counts once. */+ attributed_cost_usd: number+ /** Number of $ai_generation events in cost_usd. */+ generations: number+ /** Number of $ai_generation events in attributed_cost_usd. */+ attributed_generations: number+}+export interface MasterFailureGroupApi {/** Repository the failures occurred in. */repo: RepoRefApi@@ -1240,17 +1296,6 @@ export interface RunCostApi {ci_engine?: CIEngineEnumApi | null}-export interface PRLLMSpendApi {- /** Total agent LLM token cost in USD attributed to this PR (sum of $ai_total_cost_usd over the matched $ai_generation events). */- cost_usd: number- /** Total input (prompt) tokens across the attributed generations. */- input_tokens: number- /** Total output (completion) tokens across the attributed generations. */- output_tokens: number- /** Number of $ai_generation events attributed to this PR by git branch ($ai_git_branch). */- generations: number-}-export interface PRCostSummaryApi {/** Same spend broken down per workflow. */by_workflow: WorkflowCostApi[]@@ -2689,6 +2734,33 @@ export type EngineeringAnalyticsAuthorFrictionDetailParams = {source_id?: string}+export type EngineeringAnalyticsAuthorLlmSpendParams = {+ /**+ * GitHub handle whose pull requests to list.+ */+ author: string+ /**+ * Window start: relative ('-30d', '-8w') or ISO8601. Defaults to -30d.+ */+ date_from?: string+ /**+ * Window end: relative or ISO8601. Defaults to now.+ */+ date_to?: string+ /**+ * Maximum pull requests to return, 1 to 200. Defaults to 100.+ */+ limit?: number+ /**+ * 'owner/name' repository to scope to when the selected source syncs several repositories (from the `sources` list). Defaults to the source's first repository.+ */+ repo?: string+ /**+ * Connected GitHub data warehouse source to read from. Defaults to the oldest connected GitHub source when the team has more than one.+ */+ source_id?: string+}+export type EngineeringAnalyticsAuthorWorkflowCostsParams = {/*** GitHub handle whose CI spend to break down.@@ -3048,6 +3120,25 @@ export const EngineeringAnalyticsJobLogInsightsCiEngine = {DepotCi: 'depot_ci',} as const+export type EngineeringAnalyticsLlmSpendCoverageParams = {+ /**+ * Window start: relative ('-30d', '-8w') or ISO8601. Defaults to -30d.+ */+ date_from?: string+ /**+ * Window end: relative or ISO8601. Defaults to now.+ */+ date_to?: string+ /**+ * 'owner/name' repository to scope to when the selected source syncs several repositories (from the `sources` list). Defaults to the source's first repository.+ */+ repo?: string+ /**+ * Connected GitHub data warehouse source to read from. Defaults to the oldest connected GitHub source when the team has more than one.+ */+ source_id?: string+}+export type EngineeringAnalyticsMasterFailuresParams = {/*** Optional exact git branch (head_branch) to scope results to, e.g. 'main'. Omit or leave blank to aggregate across all branches.diff --git a/products/engineering_analytics/frontend/generated/api.ts b/products/engineering_analytics/frontend/generated/api.tsindex 01a7129314fa..6a8f6f24a83e 100644--- a/products/engineering_analytics/frontend/generated/api.ts+++ b/products/engineering_analytics/frontend/generated/api.ts@@ -12,6 +12,7 @@ import type {AttentionPullRequestListApi,AuthorFrictionDetailApi,AuthorFrictionListApi,+ AuthorLLMSpendListApi,BranchPRMatchApi,BrokenTestsResultApi,CICardSummaryApi,@@ -27,6 +28,7 @@ import type {EngineeringAnalyticsAttentionPullRequestsParams,EngineeringAnalyticsAuthorFrictionDetailParams,EngineeringAnalyticsAuthorFrictionParams,+ EngineeringAnalyticsAuthorLlmSpendParams,EngineeringAnalyticsAuthorWorkflowCostsParams,EngineeringAnalyticsBrokenTestsParams,EngineeringAnalyticsCiCardsParams,@@ -40,6 +42,7 @@ import type {EngineeringAnalyticsFlakyTestsParams,EngineeringAnalyticsJobAggregatesParams,EngineeringAnalyticsJobLogInsightsParams,+ EngineeringAnalyticsLlmSpendCoverageParams,EngineeringAnalyticsMasterFailuresParams,EngineeringAnalyticsPrCostParams,EngineeringAnalyticsPrLifecycleParams,@@ -65,6 +68,7 @@ import type {FlakyTestListApi,GitHubSourceApi,JobLogInsightsApi,+ LLMSpendCoverageApi,MasterFailureGroupApi,PRCostSummaryApi,PRLifecycleApi,@@ -188,6 +192,39 @@ export const engineeringAnalyticsAuthorFrictionDetail = async (})}+export const getEngineeringAnalyticsAuthorLlmSpendUrl = (+ projectId: string,+ params: EngineeringAnalyticsAuthorLlmSpendParams+) => {+ const normalizedParams = new URLSearchParams()++ Object.entries(params || {}).forEach(([key, value]) => {+ if (value !== undefined) {+ normalizedParams.append(key, value === null ? 'null' : String(value))+ }+ })++ const stringifiedParams = normalizedParams.toString()++ return stringifiedParams.length > 0+ ? `/api/projects/${projectId}/engineering_analytics/author_llm_spend/?${stringifiedParams}`+ : `/api/projects/${projectId}/engineering_analytics/author_llm_spend/`+}++/**+ * One author's pull requests active in a window (date_from default -30d), newest first, each with the agent LLM token spend attributed to it by git branch and AI session, the same rule as `pr_cost`.+ */+export const engineeringAnalyticsAuthorLlmSpend = async (+ projectId: string,+ params: EngineeringAnalyticsAuthorLlmSpendParams,+ options?: RequestInit+): Promise<AuthorLLMSpendListApi> => {+ return apiMutator<AuthorLLMSpendListApi>(getEngineeringAnalyticsAuthorLlmSpendUrl(projectId, params), {+ ...options,+ method: 'GET',+ })+}+export const getEngineeringAnalyticsAuthorWorkflowCostsUrl = (projectId: string,params: EngineeringAnalyticsAuthorWorkflowCostsParams@@ -657,6 +694,39 @@ export const engineeringAnalyticsJobLogInsights = async (})}+export const getEngineeringAnalyticsLlmSpendCoverageUrl = (+ projectId: string,+ params?: EngineeringAnalyticsLlmSpendCoverageParams+) => {+ const normalizedParams = new URLSearchParams()++ Object.entries(params || {}).forEach(([key, value]) => {+ if (value !== undefined) {+ normalizedParams.append(key, value === null ? 'null' : String(value))+ }+ })++ const stringifiedParams = normalizedParams.toString()++ return stringifiedParams.length > 0+ ? `/api/projects/${projectId}/engineering_analytics/llm_spend_coverage/?${stringifiedParams}`+ : `/api/projects/${projectId}/engineering_analytics/llm_spend_coverage/`+}++/**+ * How much of a repository's agent LLM spend in a window (date_from default -30d) the pull request attribution reaches: the cost and count of $ai_generation events stamped with the repository, and the part credited to any pull request.+ */+export const engineeringAnalyticsLlmSpendCoverage = async (+ projectId: string,+ params?: EngineeringAnalyticsLlmSpendCoverageParams,+ options?: RequestInit+): Promise<LLMSpendCoverageApi> => {+ return apiMutator<LLMSpendCoverageApi>(getEngineeringAnalyticsLlmSpendCoverageUrl(projectId, params), {+ ...options,+ method: 'GET',+ })+}+export const getEngineeringAnalyticsMasterFailuresUrl = (projectId: string,params?: EngineeringAnalyticsMasterFailuresParamsdiff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex ba972c8f9209..600db7cea164 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -14607,6 +14607,51 @@ export namespace Schemas {has_membership_data: boolean;}+ export interface PRLLMSpend {+ /** Total agent LLM token cost in USD attributed to this PR (sum of $ai_total_cost_usd over the matched $ai_generation events). */+ cost_usd: number;+ /** Total input (prompt) tokens across the attributed generations. */+ input_tokens: number;+ /** Total output (completion) tokens across the attributed generations. */+ output_tokens: number;+ /** Number of $ai_generation events attributed to this PR by git branch ($ai_git_branch). */+ generations: number;+ /** Total prompt tokens read from the provider's prompt cache across the attributed generations ($ai_cache_read_input_tokens). Not included in input_tokens. */+ cache_read_input_tokens: number;+ /** Total prompt tokens written to the provider's prompt cache across the attributed generations ($ai_cache_creation_input_tokens). Not included in input_tokens. */+ cache_creation_input_tokens: number;+ /** Largest prompt any single attributed generation sent: input plus cache read plus cache write tokens. */+ peak_context_tokens: number;+ }++ export interface AuthorPRLLMSpend {+ /** Agent LLM token spend attributed to this pull request, by the same rule as `pr_cost`, or null when no generation matched. */+ llm_spend: PRLLMSpend | null;+ /** Pull request number. */+ number: number;+ /** Pull request title. */+ title: string;+ /** Pull request state: open, closed, or merged.+ *+ * * `open` - Open+ * * `closed` - Closed+ * * `merged` - Merged */+ state: PRStateEnum;+ /** The pull request's head branch, which attribution joins on. */+ head_branch: string;+ /** When the pull request was opened. */+ created_at: string;+ }++ export interface AuthorLLMSpendList {+ /** The author's pull requests active in the window, newest first, capped at `limit`. Each spend covers the pull request's whole attribution window, so it can include spend from before date_from. */+ items: AuthorPRLLMSpend[];+ /** True when more pull requests matched than `limit`. */+ truncated: boolean;+ /** Maximum number of pull requests returned in `items`. */+ limit: number;+ }+export type AutocompleteCompletionItemKind = typeof AutocompleteCompletionItemKind[keyof typeof AutocompleteCompletionItemKind];@@ -67449,6 +67494,17 @@ export namespace Schemas {results: LLMSkillSearchResult[];}+ export interface LLMSpendCoverage {+ /** Total cost in USD of the $ai_generation events stamped with this repository ($ai_git_repo) in the window. Generations with no repository stamp are not counted. */+ cost_usd: number;+ /** The part of cost_usd that the branch and session attribution credits to a pull request. A generation credited to several pull requests counts once. */+ attributed_cost_usd: number;+ /** Number of $ai_generation events in cost_usd. */+ generations: number;+ /** Number of $ai_generation events in attributed_cost_usd. */+ attributed_generations: number;+ }+export interface LLMTaggerConfig {/*** Prompt instructing the LLM how to tag generations@@ -76090,17 +76146,6 @@ export namespace Schemas {ci_engine?: CIEngineEnum | null;}- export interface PRLLMSpend {- /** Total agent LLM token cost in USD attributed to this PR (sum of $ai_total_cost_usd over the matched $ai_generation events). */- cost_usd: number;- /** Total input (prompt) tokens across the attributed generations. */- input_tokens: number;- /** Total output (completion) tokens across the attributed generations. */- output_tokens: number;- /** Number of $ai_generation events attributed to this PR by git branch ($ai_git_branch). */- generations: number;- }-export interface PRCostSummary {/** Same spend broken down per workflow. */by_workflow: WorkflowCost[];@@ -133078,6 +133123,33 @@ export namespace Schemas {source_id?: string;};+ export type EngineeringAnalyticsAuthorLlmSpendParams = {+ /**+ * GitHub handle whose pull requests to list.+ */+ author: string;+ /**+ * Window start: relative ('-30d', '-8w') or ISO8601. Defaults to -30d.+ */+ date_from?: string;+ /**+ * Window end: relative or ISO8601. Defaults to now.+ */+ date_to?: string;+ /**+ * Maximum pull requests to return, 1 to 200. Defaults to 100.+ */+ limit?: number;+ /**+ * 'owner/name' repository to scope to when the selected source syncs several repositories (from the `sources` list). Defaults to the source's first repository.+ */+ repo?: string;+ /**+ * Connected GitHub data warehouse source to read from. Defaults to the oldest connected GitHub source when the team has more than one.+ */+ source_id?: string;+ };+export type EngineeringAnalyticsAuthorWorkflowCostsParams = {/*** GitHub handle whose CI spend to break down.@@ -133437,6 +133509,25 @@ export namespace Schemas {DepotCi: 'depot_ci',} as const;+ export type EngineeringAnalyticsLlmSpendCoverageParams = {+ /**+ * Window start: relative ('-30d', '-8w') or ISO8601. Defaults to -30d.+ */+ date_from?: string;+ /**+ * Window end: relative or ISO8601. Defaults to now.+ */+ date_to?: string;+ /**+ * 'owner/name' repository to scope to when the selected source syncs several repositories (from the `sources` list). Defaults to the source's first repository.+ */+ repo?: string;+ /**+ * Connected GitHub data warehouse source to read from. Defaults to the oldest connected GitHub source when the team has more than one.+ */+ source_id?: string;+ };+export type EngineeringAnalyticsMasterFailuresParams = {/*** Optional exact git branch (head_branch) to scope results to, e.g. 'main'. Omit or leave blank to aggregate across all branches.diff --git a/tools/hogli-commands/hogli_commands/agent_usage.py b/tools/hogli-commands/hogli_commands/agent_usage.pynew file mode 100644index 000000000000..476d0a55242e--- /dev/null+++ b/tools/hogli-commands/hogli_commands/agent_usage.py@@ -0,0 +1,147 @@+from __future__ import annotations++import os+import subprocess++import click++from hogli_commands import ci_insights++_SETUP_HINT = (+ "Agent token usage reaches PostHog only from agents that send $ai_generation events with $ai_git_branch. "+ "For Claude Code, set up the PostHog plugin's LLM analytics ingestion (/posthog:llma-cc-setup)."+)+++def _gh_login() -> str | None:+ try:+ result = subprocess.run(+ ["gh", "api", "user", "--jq", ".login"], capture_output=True, text=True, timeout=10.0, check=False+ )+ except (OSError, subprocess.SubprocessError):+ return None+ login = result.stdout.strip()+ return login if result.returncode == 0 and login else None+++def _format_tokens(value: float) -> str:+ for unit, size in (("B", 1e9), ("M", 1e6), ("k", 1e3)):+ if value >= size:+ return f"{value / size:.1f}{unit}"+ return f"{value:.0f}"+++def _render(+ spend: ci_insights._AuthorLLMSpendList, coverage: ci_insights._LLMSpendCoverage, *, author: str, days: int+) -> None:+ rows = [item for item in spend["items"] if item["llm_spend"] is not None]+ if not rows:+ click.echo(f"No agent LLM spend is attributed to {author}'s pull requests in the last {days} days.")+ click.echo(_SETUP_HINT)+ return+ rows.sort(key=lambda item: item["llm_spend"]["cost_usd"] if item["llm_spend"] else 0.0, reverse=True)+ headers = ("cost", "generations", "input", "cache write", "cache read", "output", "peak ctx")+ cells = [+ (+ f"${usage['cost_usd']:.2f}",+ str(usage["generations"]),+ _format_tokens(usage["input_tokens"]),+ _format_tokens(usage["cache_creation_input_tokens"]),+ _format_tokens(usage["cache_read_input_tokens"]),+ _format_tokens(usage["output_tokens"]),+ _format_tokens(usage["peak_context_tokens"]),+ )+ for item in rows+ if (usage := item["llm_spend"]) is not None+ ]+ widths = [max(len(header), *(len(row[index]) for row in cells)) for index, header in enumerate(headers)]+ click.echo(" ".join(header.rjust(width) for header, width in zip(headers, widths)) + " pull request")+ for item, row in zip(rows, cells):+ label = ci_insights._terminal_text(f"#{item['number']} {item['title']}")[:70]+ click.echo(" ".join(cell.rjust(width) for cell, width in zip(row, widths)) + " " + label)++ total = sum(item["llm_spend"]["cost_usd"] for item in rows if item["llm_spend"])+ listed = f"{len(spend['items'])}+" if spend["truncated"] else str(len(spend["items"]))+ click.echo(f"\n${total:.2f} across {len(rows)} of {listed} pull requests active in the last {days} days.")+ if coverage["cost_usd"] > 0:+ click.echo(+ f"Repo coverage: {coverage['attributed_cost_usd'] / coverage['cost_usd']:.0%} of "+ f"${coverage['cost_usd']:.2f} repo-stamped agent spend maps to a pull request."+ )++[email protected](name="agent:usage")[email protected]("--author", help="GitHub handle whose pull requests to show. Defaults to your gh login.")+ "--days", type=click.IntRange(min=1, max=366), default=30, show_default=True, help="Look back this many days."+)+ "--limit", type=click.IntRange(min=1, max=200), default=100, show_default=True, help="Pull requests to read."+)[email protected]("--repo", help="'owner/name' to read; defaults to the origin remote.")+ "--project",+ default=lambda: os.environ.get("POSTHOG_CI_INSIGHTS_PROJECT_ID") or ci_insights._DEFAULT_PROJECT_ID,+ help="PostHog project id holding the GitHub source.",+)+ "--host",+ default=lambda: os.environ.get("POSTHOG_CI_INSIGHTS_HOST") or ci_insights._DEFAULT_HOST,+ help="PostHog host to read from.",+)+ "--timeout",+ type=click.FloatRange(min=0, min_open=True),+ default=60.0,+ show_default=True,+ help="Per-request timeout in seconds.",+)[email protected]("--json", "as_json", is_flag=True, help="Shorthand for --format json.")+ "--format",+ "output_format",+ type=click.Choice(["auto", "text", "json"]),+ default="auto",+ show_default=True,+ help="'auto' emits JSON when stdout is not a terminal.",+)+def cmd_agent_usage(+ author: str | None,+ days: int,+ limit: int,+ repo: str | None,+ project: str,+ host: str,+ timeout: float,+ as_json: bool,+ output_format: str,+) -> None:+ """Show agent LLM token spend per pull request, from engineering analytics.++ Spend is attributed to a pull request by the git branch and AI session on each+ $ai_generation event, the same rule as the pull request page. Repo coverage is the+ share of agent spend stamped with this repo that maps to any pull request.+ """+ options = ci_insights._Options(+ repo=repo,+ project=project,+ host=host,+ timeout=timeout,+ output_format="json" if as_json else output_format,+ limit=limit,+ )+ try:+ login = author or _gh_login()+ if not login:+ raise ci_insights._ApiError("Could not read your GitHub login from gh. Pass --author.")+ api = ci_insights._resolve_source(options.api(), repo)+ spend = api.author_llm_spend(author=login, days=days, limit=limit)+ coverage = api.llm_spend_coverage(days=days)+ except ci_insights._ApiError as error:+ ci_insights._fail(error)++ if options.emits_json():+ ci_insights._emit_json(+ {"author": login, "repo": api.repo, "days": days, "pull_requests": spend, "repo_coverage": coverage}+ )+ _render(spend, coverage, author=login, days=days)diff --git a/tools/hogli-commands/hogli_commands/ci_insights.py b/tools/hogli-commands/hogli_commands/ci_insights.pyindex cd6fff8f5ae9..07a46c576514 100644--- a/tools/hogli-commands/hogli_commands/ci_insights.py+++ b/tools/hogli-commands/hogli_commands/ci_insights.py@@ -246,6 +246,38 @@ class _RunFailureLogs(TypedDict):truncated: bool+class _PRLLMSpend(TypedDict):+ cost_usd: float+ input_tokens: int+ output_tokens: int+ generations: int+ cache_read_input_tokens: int+ cache_creation_input_tokens: int+ peak_context_tokens: int+++class _AuthorPRLLMSpend(TypedDict):+ number: int+ title: str+ state: str+ head_branch: str+ created_at: str+ llm_spend: _PRLLMSpend | None+++class _AuthorLLMSpendList(TypedDict):+ items: list[_AuthorPRLLMSpend]+ truncated: bool+ limit: int+++class _LLMSpendCoverage(TypedDict):+ cost_usd: float+ attributed_cost_usd: float+ generations: int+ attributed_generations: int++def _invalid_payload(action: str, path: str, expected: str) -> NoReturn:raise _ApiError(f"{action} returned an invalid payload: {path} must be {expected}.")@@ -292,6 +324,10 @@ def _validate_payload(value: object, expected: object, action: str, path: str =if type(value) is not bool:_invalid_payload(action, path, "a boolean")return+ if expected is float:+ if type(value) not in {int, float}:+ _invalid_payload(action, path, "a number")+ returnif expected is str:if not isinstance(value, str):_invalid_payload(action, path, "a string")@@ -337,6 +373,16 @@ def _parse_run_failure_logs(value: object, action: str) -> _RunFailureLogs:return cast(_RunFailureLogs, value)+def _parse_author_llm_spend(value: object, action: str) -> _AuthorLLMSpendList:+ _validate_payload(value, _AuthorLLMSpendList, action)+ return cast(_AuthorLLMSpendList, value)+++def _parse_llm_spend_coverage(value: object, action: str) -> _LLMSpendCoverage:+ _validate_payload(value, _LLMSpendCoverage, action)+ return cast(_LLMSpendCoverage, value)++def _terminal_text(value: object) -> str:return "".join(character for character in str(value) if unicodedata.category(character) not in {"Cc", "Cf"})@@ -491,6 +537,14 @@ class _Api:action = "run_failure_logs"return _parse_run_failure_logs(self._get(action, run_id=run_id), action)+ def author_llm_spend(self, *, author: str, days: int, limit: int) -> _AuthorLLMSpendList:+ action = "author_llm_spend"+ return _parse_author_llm_spend(self._get(action, author=author, date_from=f"-{days}d", limit=limit), action)++ def llm_spend_coverage(self, *, days: int) -> _LLMSpendCoverage:+ action = "llm_spend_coverage"+ return _parse_llm_spend_coverage(self._get(action, date_from=f"-{days}d"), action)+def _git(*args: str) -> str | None:"""Trimmed stdout of a git command, None on any failure."""diff --git a/tools/hogli-commands/hogli_commands/tests/test_agent_usage.py b/tools/hogli-commands/hogli_commands/tests/test_agent_usage.pynew file mode 100644index 000000000000..e75b08b4ab94--- /dev/null+++ b/tools/hogli-commands/hogli_commands/tests/test_agent_usage.py@@ -0,0 +1,116 @@+from __future__ import annotations++import json+from typing import Any++import pytest+from unittest.mock import patch++from click.testing import CliRunner+from hogli_commands import agent_usage, ci_insights, posthog_auth++_SOURCES = [{"id": "src-good", "repo": "PostHog/posthog", "prefix": "eng_analytics", "synced": True}]+++def _usage(cost: float, generations: int) -> dict[str, Any]:+ return {+ "cost_usd": cost,+ "input_tokens": 1200,+ "output_tokens": 3400,+ "generations": generations,+ "cache_read_input_tokens": 2_500_000,+ "cache_creation_input_tokens": 180_000,+ "peak_context_tokens": 140_000,+ }+++def _pr(number: int, title: str, usage: dict[str, Any] | None) -> dict[str, Any]:+ return {+ "number": number,+ "title": title,+ "state": "merged",+ "head_branch": f"feat/{number}",+ "created_at": "2026-10-20T12:00:00Z",+ "llm_spend": usage,+ }+++class _Response:+ def __init__(self, payload: Any) -> None:+ self.status_code = 200+ self._payload = payload+ self.text = json.dumps(payload)++ def json(self) -> Any:+ return self._payload+++class _Recorder:+ def __init__(self, items: list[dict[str, Any]]) -> None:+ self.calls: dict[str, dict[str, Any]] = {}+ self._payloads = {+ "sources": _SOURCES,+ "author_llm_spend": {"items": items, "truncated": False, "limit": 100},+ "llm_spend_coverage": {+ "cost_usd": 40,+ "attributed_cost_usd": 30.0,+ "generations": 900,+ "attributed_generations": 700,+ },+ }++ def __call__(self, url: str, *, token: str, params: dict[str, Any], timeout: float) -> _Response:+ action = url.rstrip("/").rsplit("/", 1)[-1]+ self.calls[action] = params+ return _Response(self._payloads[action])++[email protected](autouse=True)+def signed_in(monkeypatch: pytest.MonkeyPatch) -> None:+ for var in posthog_auth.KEY_ENV_VARS:+ monkeypatch.delenv(var, raising=False)+ monkeypatch.setenv("POSTHOG_PERSONAL_API_KEY", "phx_test_key_do_not_leak")+++def _invoke(argv: list[str], recorder: _Recorder) -> Any:+ with (+ patch.object(ci_insights, "_request", recorder),+ patch.object(ci_insights, "_git", return_value="[email protected]:PostHog/posthog.git"),+ patch.object(agent_usage, "_gh_login", return_value="alice"),+ ):+ return CliRunner().invoke(agent_usage.cmd_agent_usage, argv)+++def test_reads_the_callers_spend_and_lists_only_attributed_prs_by_cost() -> None:+ recorder = _Recorder(+ [+ _pr(12, "small change", _usage(2.5, 10)),+ _pr(11, "no agent work", None),+ _pr(10, "big change", _usage(27.5, 90)),+ ]+ )++ result = _invoke(["--days", "14", "--format", "text"], recorder)++ assert result.exit_code == 0, result.output+ assert recorder.calls["author_llm_spend"] == {+ "source_id": "src-good",+ "repo": "PostHog/posthog",+ "author": "alice",+ "date_from": "-14d",+ "limit": 100,+ }+ assert recorder.calls["llm_spend_coverage"]["date_from"] == "-14d"+ lines = result.output.splitlines()+ pr_lines = [line for line in lines if line.rstrip().endswith("change")]+ assert [line.split(" ")[-1] for line in pr_lines] == ["#10 big change", "#12 small change"]+ assert "no agent work" not in result.output+ assert "$30.00 across 2 of 3 pull requests" in result.output+ assert "Repo coverage: 75% of $40.00" in result.output+++def test_no_attributed_spend_points_at_the_ingestion_setup() -> None:+ result = _invoke(["--format", "text"], _Recorder([_pr(11, "no agent work", None)]))++ assert result.exit_code == 0, result.output+ assert "/posthog:llma-cc-setup" in result.output