Files
PaperClipAI/ui/src/pages/AgentAuditTab.test.ts
T
DottaandClaude Opus 5.5 6dc36700b1 fix(costs): mark unpriced usage in the agent, model and project cost reports (#20889)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Cost reports show what agents and projects spent
> - A run can have usage with no known price; Paperclip saves it with
costStatus "unpriced" and costCents 0
> - The summary and the provider, biller and user reports count those
events, but the agent, agent-model and project reports did not
> - An agent that read the by-agent-model report saw a native run at
costCents 0 and told the person it cost $0.00
> - This pull request adds unpricedEventCount to those three reports and
tells agents what it means
> - The benefit is that missing charges are reported as unpriced, not as
free

## Linked Issues or Issue Description

**What happened**
In the chat eval budgets-costs.spend-breakdown, the person asked "How
much did Launch cost this week, broken down by agent?". The week
included a real native run whose usage had no known price (data-quality
reason missing_price). The agent read /costs/by-agent-model and
/costs/by-project. Both returned costCents 0 for that usage and no
unpriced count. The reply showed "1 native run ($0.00)" and gave the
total as complete.

**Expected behavior**
The reports say which rows include unpriced usage. The agent says that
usage is unpriced, marks the total incomplete, and links the record.

**Steps to reproduce**
1. Record one priced and one unpriced metered cost event for an agent in
a project.
2. Call GET /api/companies/{companyId}/costs/by-agent, /by-agent-model
and /by-project.
3. See costCents counts only the priced event, and no field shows the
unpriced event.

## What Changed

- `costService.byAgent`, `byAgentModel` and `byProject` return
`unpricedEventCount` (same filter as the summary: unpriced and not
subscription_included).
- Shared types `CostByAgent`, `CostByAgentModel` and `CostByProject` add
`unpricedEventCount`.
- The OpenAPI catalog entries for the cost reports that count unpriced
usage now say that costCents counts priced usage only, that unpriced
usage must be reported as unpriced (never $0.00), and that
/costs/data-quality lists those runs.
- Tests: embedded-Postgres service test (red on master, green here) and
an OpenAPI description test. A UI test fixture adds the new field.

## Verification

- `npx vitest run src/__tests__/costs-service.test.ts` (19 passed). The
new test fails with the master service.
- `npx vitest run src/__tests__/openapi-routes.test.ts
src/services/native-runtime/runner-api.test.ts` (2146 passed).
- `tsc --noEmit` for packages/shared and server.

## Risks

- Low risk. One added aggregate column per report and an added response
field. The UI does not read it yet.

## Model Used

- Anthropic Claude Opus 5.5 (`claude-opus-5-5`), tool use and code
execution in Claude Code.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 13:36:03 -06:00

82 lines
2.7 KiB
TypeScript

import { describe, expect, it } from "vitest";
import type { BudgetOverview, CostByAgent, CostByAgentModel } from "@paperclipai/shared";
import { agentBudgetSummary } from "./AgentBudgetsPanel";
import { agentCostRow, agentModelCostRows } from "./AgentCostsPanel";
const agent = {
id: "agent-1",
name: "Atlas",
status: "active",
pauseReason: null,
budgetMonthlyCents: 10_000,
spentMonthlyCents: 2_500,
} as const;
function costRow(agentId: string | null, costCents: number): CostByAgent {
return {
agentId,
agentName: agentId,
agentStatus: "active",
eventCount: 1,
estimatedEventCount: 0,
unpricedEventCount: 0,
costCents,
inputTokens: 10,
cachedInputTokens: 0,
outputTokens: 5,
apiRunCount: 1,
subscriptionRunCount: 0,
subscriptionCachedInputTokens: 0,
subscriptionInputTokens: 0,
subscriptionOutputTokens: 0,
};
}
function modelRow(agentId: string, model: string, costCents: number): CostByAgentModel {
return {
agentId,
agentName: agentId,
provider: "anthropic",
biller: "anthropic",
billingType: "metered_api",
model,
eventCount: 1,
estimatedEventCount: 0,
unpricedEventCount: 0,
costCents,
inputTokens: 1,
cachedInputTokens: 0,
outputTokens: 1,
};
}
describe("agent-scoped audit data", () => {
it("keeps only the agent's cost row and orders its models by spend", () => {
expect(agentCostRow([costRow("agent-2", 900), costRow("agent-1", 300), costRow(null, 50)], "agent-1")?.costCents).toBe(300);
expect(agentCostRow([costRow("agent-2", 900)], "agent-1")).toBeNull();
expect(
agentModelCostRows([modelRow("agent-1", "small", 20), modelRow("agent-2", "other", 90), modelRow("agent-1", "large", 80)], "agent-1").map((row) => row.model),
).toEqual(["large", "small"]);
});
it("uses the agent's own budget policy and falls back to its monthly budget", () => {
const policy = { ...agentBudgetSummary(undefined, agent, "company-1"), policyId: "policy-1", amount: 50_000 };
const overview = {
companyId: "company-1",
policies: [{ ...policy, scopeId: "agent-2", policyId: "policy-2" }, policy],
activeIncidents: [],
pausedAgentCount: 0,
pausedProjectCount: 0,
pendingApprovalCount: 0,
} satisfies BudgetOverview;
expect(agentBudgetSummary(overview, agent, "company-1").policyId).toBe("policy-1");
const fallback = agentBudgetSummary(undefined, agent, "company-1");
expect(fallback.scopeType).toBe("agent");
expect(fallback.amount).toBe(10_000);
expect(fallback.remainingAmount).toBe(7_500);
expect(fallback.utilizationPercent).toBe(25);
expect(fallback.status).toBe("ok");
});
});