Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. πŸ”₯

fix(billing): track hosted MCP charges whole on MCP credits (#5474)

Rakshith Ramprakash016c6aeb9cd3
diff --git a/apps/api/src/__tests__/routes/check-credits-middleware.test.ts b/apps/api/src/__tests__/routes/check-credits-middleware.test.ts
index 08679b2a4..a7fbbbc83 100644
--- a/apps/api/src/__tests__/routes/check-credits-middleware.test.ts
+++ b/apps/api/src/__tests__/routes/check-credits-middleware.test.ts
@@ -15,7 +15,7 @@ vi.mock("../../lib/logger", () => ({
vi.mock("../../services/autumn/autumn.service", () => ({
autumnService: {
checkCredits: vi.fn(),
- mcpCreditsAvailable: vi.fn(),
+ hostedMcpFeatureId: async () => "MCP_CREDITS",
},
CREDITS_FEATURE_ID: "CREDITS",
}));
@@ -50,10 +50,6 @@ import { getTeamBalance } from "../../services/autumn/usage";
const checkCreditsMock = autumnService.checkCredits as MockedFunction<
typeof autumnService.checkCredits
>;
-const mcpCreditsAvailableMock =
- autumnService.mcpCreditsAvailable as MockedFunction<
- typeof autumnService.mcpCreditsAvailable
- >;
const getTeamBalanceMock = getTeamBalance as MockedFunction<
typeof getTeamBalance
>;
@@ -423,9 +419,8 @@ describe("checkCreditsMiddleware – MCP credits", () => {
vi.clearAllMocks();
});
- it("runs a hosted MCP call the team's credits can't cover when its MCP credits can", async () => {
- checkCreditsMock.mockResolvedValue({ allowed: false, remaining: 0 });
- mcpCreditsAvailableMock.mockResolvedValue(1);
+ it("gates a hosted MCP call on MCP credits, the balance it is billed to", async () => {
+ checkCreditsMock.mockResolvedValue({ allowed: true, remaining: 100 });
const req = buildReq({
path: "/v2/scrape",
@@ -437,104 +432,70 @@ describe("checkCreditsMiddleware – MCP credits", () => {
expect(res.status).not.toHaveBeenCalled();
expect(nextCalled).toBe(true);
- expect(req.account.remainingCredits).toBe(Infinity);
- expect(mcpCreditsAvailableMock).toHaveBeenCalledExactlyOnceWith({
+ expect(checkCreditsMock).toHaveBeenCalledExactlyOnceWith({
teamId: "team_test",
orgId: "org_test",
value: 1,
properties: {
- source: "checkCreditsMiddleware:mcp",
+ source: "checkCreditsMiddleware",
path: "/v2/scrape",
apiKeyId: 7,
},
+ featureId: "MCP_CREDITS",
});
});
- it("keeps the full crawl limit when MCP credits cover it", async () => {
- checkCreditsMock.mockResolvedValue({ allowed: false, remaining: 5 });
- mcpCreditsAvailableMock.mockResolvedValue(100);
+ it("refuses a hosted MCP call MCP credits and what they fall back to can't cover", async () => {
+ checkCreditsMock.mockResolvedValue({ allowed: false, remaining: 0 });
- const req = buildReq({ auth: hostedMcpAuth, body: { limit: 100 } });
- const { res } = await runMiddleware(req, spendsMcpCredits);
+ const req = buildReq({ auth: hostedMcpAuth });
+ const { res, nextCalled } = await runMiddleware(req, spendsMcpCredits);
- expect(res.status).not.toHaveBeenCalled();
- expect(req.body.limit).toBe(100);
- expect(mcpCreditsAvailableMock).toHaveBeenCalledWith(
- expect.objectContaining({ value: 100 }),
- );
+ expect(res.status).toHaveBeenCalledWith(402);
+ expect(nextCalled).toBe(false);
});
- it("clamps the crawl limit as today when MCP credits cover only part of it", async () => {
+ it("clamps the crawl limit against MCP credits on the re-check too", async () => {
checkCreditsMock
.mockResolvedValueOnce({ allowed: false, remaining: 5 })
.mockResolvedValueOnce({ allowed: true, remaining: 5 });
- mcpCreditsAvailableMock.mockResolvedValue(40);
const req = buildReq({ auth: hostedMcpAuth, body: { limit: 100 } });
const { res } = await runMiddleware(req, spendsMcpCredits);
expect(res.status).not.toHaveBeenCalled();
expect(req.body.limit).toBe(5);
+ expect(checkCreditsMock).toHaveBeenCalledTimes(2);
+ for (const [check] of checkCreditsMock.mock.calls) {
+ expect(check.featureId).toBe("MCP_CREDITS");
+ }
});
- // Per-key spend caps limit regular credits only.
- it("runs a hosted MCP call on MCP credits when its key is over its spend cap", async () => {
- // A per-key cap denies while the team balance stays healthy.
- checkCreditsMock.mockResolvedValue({ allowed: false, remaining: 5000 });
- mcpCreditsAvailableMock.mockResolvedValue(1);
+ it("gates a call the hosted MCP did not make on the route's own feature", async () => {
+ checkCreditsMock.mockResolvedValue({ allowed: true, remaining: 100 });
- const req = buildReq({
- path: "/v2/scrape",
- body: {},
- auth: hostedMcpAuth,
- acuc: { api_key_id: 7 },
- });
+ const req = buildReq({ body: { limit: 100, origin: "mcp-fastmcp" } });
const { res, nextCalled } = await runMiddleware(req, spendsMcpCredits);
expect(res.status).not.toHaveBeenCalled();
expect(nextCalled).toBe(true);
- });
- it("falls back to the existing gate once MCP credits can't cover the call", async () => {
- checkCreditsMock.mockResolvedValue({ allowed: false, remaining: 0 });
- mcpCreditsAvailableMock.mockResolvedValue(0);
-
- const req = buildReq({ auth: hostedMcpAuth });
- const { res, nextCalled } = await runMiddleware(req, spendsMcpCredits);
-
- expect(res.status).toHaveBeenCalledWith(402);
- expect(nextCalled).toBe(false);
+ expect(checkCreditsMock).toHaveBeenCalledWith(
+ expect.objectContaining({ featureId: "CREDITS" }),
+ );
});
- it("never looks at MCP credits when the team's own credits allow the call", async () => {
+ it("gates a hosted MCP call on the route's own feature where billing doesn't use MCP credits", async () => {
checkCreditsMock.mockResolvedValue({ allowed: true, remaining: 100 });
- const req = buildReq({ auth: hostedMcpAuth });
- const { res } = await runMiddleware(req, spendsMcpCredits);
-
- expect(res.status).not.toHaveBeenCalled();
- expect(mcpCreditsAvailableMock).not.toHaveBeenCalled();
- });
-
- it("never looks at MCP credits for a call the hosted MCP did not make", async () => {
- checkCreditsMock.mockResolvedValue({ allowed: false, remaining: 0 });
-
- const req = buildReq({ body: { limit: 100, origin: "mcp-fastmcp" } });
- const { res } = await runMiddleware(req, spendsMcpCredits);
-
- expect(res.status).toHaveBeenCalledWith(402);
- expect(mcpCreditsAvailableMock).not.toHaveBeenCalled();
- });
-
- it("never looks at MCP credits on a route whose billing doesn't spend them", async () => {
- checkCreditsMock.mockResolvedValue({ allowed: false, remaining: 0 });
- mcpCreditsAvailableMock.mockResolvedValue(100);
-
const req = buildReq({ path: "/v2/extract", auth: hostedMcpAuth });
const { res, nextCalled } = await runMiddleware(req);
- expect(res.status).toHaveBeenCalledWith(402);
- expect(nextCalled).toBe(false);
- expect(mcpCreditsAvailableMock).not.toHaveBeenCalled();
+ expect(res.status).not.toHaveBeenCalled();
+ expect(nextCalled).toBe(true);
+
+ expect(checkCreditsMock).toHaveBeenCalledWith(
+ expect.objectContaining({ featureId: "CREDITS" }),
+ );
});
});
diff --git a/apps/api/src/__tests__/routes/research-credit-gate.routes.test.ts b/apps/api/src/__tests__/routes/research-credit-gate.routes.test.ts
index 6a0ec8dcb..7551d35ee 100644
--- a/apps/api/src/__tests__/routes/research-credit-gate.routes.test.ts
+++ b/apps/api/src/__tests__/routes/research-credit-gate.routes.test.ts
@@ -34,13 +34,15 @@ vi.mock("../../services/billing/credit_billing", () => ({
}));
vi.mock("../../services/autumn/autumn.service", () => ({
- autumnService: { checkCredits: vi.fn(), mcpCreditsAvailable: vi.fn() },
+ autumnService: {
+ checkCredits: vi.fn(),
+ hostedMcpFeatureId: async () => "MCP_CREDITS",
+ },
CREDITS_FEATURE_ID: "CREDITS",
SEARCH_CREDITS_FEATURE_ID: "SEARCH_CREDITS",
}));
const checkCredits = vi.mocked(autumnService.checkCredits);
-const mcpCreditsAvailable = vi.mocked(autumnService.mcpCreditsAvailable);
function appWith(orgId: string | undefined, hostedMcp = false) {
const app = express();
@@ -116,16 +118,14 @@ describe.each([
expect(checkCredits).not.toHaveBeenCalled();
});
- it("serves a hosted MCP search the team's MCP credits can cover", async () => {
- checkCredits.mockResolvedValue({ allowed: false, remaining: 0 });
- mcpCreditsAvailable.mockResolvedValue(2);
+ it("gates a hosted MCP search on MCP credits", async () => {
+ checkCredits.mockResolvedValue({ allowed: true, remaining: 500 });
const res = await request(appWith("org-test", true)).get(path);
expect(res.statusCode).toBe(200);
- expect(upstreamCalls).toHaveLength(1);
- expect(mcpCreditsAvailable).toHaveBeenCalledWith(
- expect.objectContaining({ teamId: "team-test", value: 2 }),
+ expect(checkCredits).toHaveBeenCalledExactlyOnceWith(
+ expect.objectContaining({ value: 2, featureId: "MCP_CREDITS" }),
);
});
});
diff --git a/apps/api/src/controllers/v2/__tests__/agent-credit-gate.test.ts b/apps/api/src/controllers/v2/__tests__/agent-credit-gate.test.ts
index bc88cb7b5..44013c43a 100644
--- a/apps/api/src/controllers/v2/__tests__/agent-credit-gate.test.ts
+++ b/apps/api/src/controllers/v2/__tests__/agent-credit-gate.test.ts
@@ -5,8 +5,9 @@ vi.mock("../../../services/autumn/autumn.service", () => ({
autumnService: {
checkCredits: vi.fn(),
hasAutoTopUp: vi.fn(),
- mcpCreditsAvailable: vi.fn(),
+ hostedMcpFeatureId: async () => "MCP_CREDITS",
},
+ CREDITS_FEATURE_ID: "CREDITS",
}));
import { gateAgentCredits } from "../agent-credit-gate";
@@ -14,7 +15,6 @@ import { autumnService } from "../../../services/autumn/autumn.service";
const checkCredits = vi.mocked(autumnService.checkCredits);
const hasAutoTopUp = vi.mocked(autumnService.hasAutoTopUp);
-const mcpCreditsAvailable = vi.mocked(autumnService.mcpCreditsAvailable);
const gate = (requestedMaxCredits?: number, orgId: string | null = "org-1") =>
gateAgentCredits({
@@ -34,7 +34,6 @@ describe("gateAgentCredits", () => {
beforeEach(() => {
vi.clearAllMocks();
hasAutoTopUp.mockResolvedValue(false);
- mcpCreditsAvailable.mockResolvedValue(1000);
});
it("blocks a team without overage at a zero balance", async () => {
@@ -201,7 +200,12 @@ describe("gateAgentCredits", () => {
});
describe("gateAgentCredits for a hosted MCP run", () => {
- const hosted = (requestedMaxCredits?: number) =>
+ beforeEach(() => {
+ vi.clearAllMocks();
+ hasAutoTopUp.mockResolvedValue(false);
+ });
+
+ const hostedGate = (requestedMaxCredits?: number) =>
gateAgentCredits({
teamId: "team-1",
orgId: "org-1",
@@ -210,149 +214,33 @@ describe("gateAgentCredits for a hosted MCP run", () => {
hostedMcp: true,
});
- beforeEach(() => {
- vi.clearAllMocks();
- hasAutoTopUp.mockResolvedValue(false);
- mcpCreditsAvailable.mockResolvedValue(0);
- });
-
- it("starts a run MCP credits pay for at a zero balance, capped at them", async () => {
- checkCredits.mockResolvedValueOnce({ allowed: false, remaining: 0 });
- mcpCreditsAvailable.mockResolvedValueOnce(300);
+ it("checks MCP credits, the balance its charges are tracked on", async () => {
+ checkCredits
+ .mockResolvedValueOnce({ allowed: true, remaining: 300 })
+ .mockResolvedValueOnce({ allowed: false, remaining: 300 });
- expect(await hosted()).toEqual({
+ expect(await hostedGate(1000)).toEqual({
allowed: true,
maxCredits: 300,
stopOnInsufficientCredits: true,
});
- expect(mcpCreditsAvailable).toHaveBeenCalledWith(
- expect.objectContaining({
- teamId: "team-1",
- orgId: "org-1",
- value: 2500,
- }),
- );
- });
-
- it("adds MCP credits to what is left of the balance below the minimum", async () => {
- checkCredits.mockResolvedValueOnce({ allowed: false, remaining: 12 });
- mcpCreditsAvailable.mockResolvedValueOnce(10);
-
- expect(await hosted(5000)).toEqual({
- allowed: true,
- maxCredits: 22,
- stopOnInsufficientCredits: true,
- });
+ expect(checkCredits).toHaveBeenCalledTimes(2);
+ for (const [check] of checkCredits.mock.calls) {
+ expect(check.featureId).toBe("MCP_CREDITS");
+ }
});
- it("admits a cap below the minimum when MCP credits clear the minimum", async () => {
+ it("refuses like any run when the check refuses", async () => {
checkCredits.mockResolvedValueOnce({ allowed: false, remaining: 0 });
- mcpCreditsAvailable.mockResolvedValueOnce(20);
- expect(await hosted(10)).toEqual({
- allowed: true,
- maxCredits: 10,
- stopOnInsufficientCredits: true,
- });
- expect(mcpCreditsAvailable).toHaveBeenCalledWith(
- expect.objectContaining({ value: 20 }),
- );
+ expect(await hostedGate()).toEqual({ allowed: false, balance: 0 });
});
- it("leaves a non-positive maxCredits to the balance alone", async () => {
+ it("leaves the feature to the default for a run the hosted MCP did not start", async () => {
checkCredits.mockResolvedValueOnce({ allowed: false, remaining: 0 });
- mcpCreditsAvailable.mockResolvedValue(1000);
- expect(await hosted(-1)).toEqual({ allowed: false, balance: 0 });
- expect(mcpCreditsAvailable).not.toHaveBeenCalled();
- });
+ await gate();
- it("refuses when the balance and MCP credits together are below the minimum", async () => {
- checkCredits.mockResolvedValueOnce({ allowed: false, remaining: 12 });
- mcpCreditsAvailable.mockResolvedValueOnce(7);
-
- expect(await hosted()).toEqual({ allowed: false, balance: 12 });
- });
-
- it("counts MCP credits alone when a spend limit refused with balance left", async () => {
- checkCredits.mockResolvedValueOnce({ allowed: false, remaining: 5000 });
- mcpCreditsAvailable.mockResolvedValueOnce(19);
- expect(await hosted()).toEqual({ allowed: false, balance: 5000 });
-
- checkCredits.mockResolvedValueOnce({ allowed: false, remaining: 5000 });
- mcpCreditsAvailable.mockResolvedValueOnce(100);
- expect(await hosted()).toEqual({
- allowed: true,
- maxCredits: 100,
- stopOnInsufficientCredits: true,
- });
- });
-
- it("raises the balance cap by the MCP credits, up to the request", async () => {
- checkCredits
- .mockResolvedValueOnce({ allowed: true, remaining: 640.6 })
- .mockResolvedValueOnce({ allowed: false, remaining: 640.6 });
- mcpCreditsAvailable.mockResolvedValueOnce(300);
- expect(await hosted()).toEqual({
- allowed: true,
- maxCredits: 940,
- stopOnInsufficientCredits: true,
- });
-
- checkCredits
- .mockResolvedValueOnce({ allowed: true, remaining: 640 })
- .mockResolvedValueOnce({ allowed: false, remaining: 640 });
- mcpCreditsAvailable.mockResolvedValueOnce(2500);
- expect(await hosted(1000)).toMatchObject({ maxCredits: 1000 });
- });
-
- it("keeps the balance cap when MCP credits are used up", async () => {
- checkCredits
- .mockResolvedValueOnce({ allowed: true, remaining: 640 })
- .mockResolvedValueOnce({ allowed: false, remaining: 640 });
-
- expect(await hosted()).toEqual({
- allowed: true,
- maxCredits: 640,
- stopOnInsufficientCredits: true,
- });
- });
-
- it("starts a capped run MCP credits pay for once the balance is spent", async () => {
- checkCredits
- .mockResolvedValueOnce({ allowed: true, remaining: 0 })
- .mockResolvedValueOnce({ allowed: false, remaining: 0 });
- mcpCreditsAvailable.mockResolvedValueOnce(50);
- expect(await hosted()).toMatchObject({ allowed: true, maxCredits: 50 });
-
- checkCredits
- .mockResolvedValueOnce({ allowed: true, remaining: 0 })
- .mockResolvedValueOnce({ allowed: false, remaining: 0 });
- expect(await hosted()).toEqual({ allowed: false, balance: 0 });
- });
-
- it("runs as requested wherever the balance alone already does", async () => {
- checkCredits
- .mockResolvedValueOnce({ allowed: true, remaining: 100 })
- .mockResolvedValueOnce({ allowed: true, remaining: 100 });
- expect(await hosted(8000)).toEqual(asRequested(8000));
-
- checkCredits
- .mockResolvedValueOnce({ allowed: true, remaining: 100 })
- .mockResolvedValueOnce({ allowed: false, remaining: 100 });
- hasAutoTopUp.mockResolvedValueOnce(true);
- expect(await hosted(4000)).toEqual(asRequested(4000));
-
- checkCredits.mockResolvedValueOnce(null);
- expect(await hosted(4000)).toEqual(asRequested(4000));
- expect(mcpCreditsAvailable).not.toHaveBeenCalled();
- });
-
- it("never asks about MCP credits for a run the hosted MCP did not start", async () => {
- checkCredits.mockResolvedValueOnce({ allowed: false, remaining: 0 });
- mcpCreditsAvailable.mockResolvedValue(1000);
-
- expect(await gate()).toEqual({ allowed: false, balance: 0 });
- expect(mcpCreditsAvailable).not.toHaveBeenCalled();
+ expect(checkCredits.mock.calls[0][0].featureId).toBeUndefined();
});
});
diff --git a/apps/api/src/controllers/v2/__tests__/browser-mcp-credits.test.ts b/apps/api/src/controllers/v2/__tests__/browser-mcp-credits.test.ts
index 8ca9fc27d..a45a636c4 100644
--- a/apps/api/src/controllers/v2/__tests__/browser-mcp-credits.test.ts
+++ b/apps/api/src/controllers/v2/__tests__/browser-mcp-credits.test.ts
@@ -2,7 +2,6 @@ import type { Response } from "express";
const mocks = vi.hoisted(() => ({
checkCredits: vi.fn(),
- mcpCreditsAvailable: vi.fn(),
markHostedMcp: vi.fn(),
cameFromHostedMcp: vi.fn(),
usedPrompt: vi.fn(),
@@ -29,8 +28,9 @@ vi.mock("../../../lib/logger", () => {
vi.mock("../../../services/autumn/autumn.service", () => ({
autumnService: {
checkCredits: mocks.checkCredits,
- mcpCreditsAvailable: mocks.mcpCreditsAvailable,
+ hostedMcpFeatureId: async () => "MCP_CREDITS",
},
+ CREDITS_FEATURE_ID: "CREDITS",
DEFAULT_TEAM_LIMITS: { concurrency_limit: 2 },
}));
@@ -175,7 +175,6 @@ const session = {
beforeEach(() => {
vi.clearAllMocks();
mocks.checkCredits.mockResolvedValue({ allowed: true, remaining: 1000 });
- mocks.mcpCreditsAvailable.mockResolvedValue(0);
mocks.markHostedMcp.mockResolvedValue(undefined);
mocks.cameFromHostedMcp.mockResolvedValue(false);
mocks.usedPrompt.mockResolvedValue(false);
@@ -185,50 +184,36 @@ beforeEach(() => {
});
describe("browser sessions from hosted MCP calls", () => {
- it("lets MCP credits pay for a session the balance alone refuses", async () => {
- mocks.checkCredits.mockResolvedValue({ allowed: false, remaining: 5 });
- mocks.mcpCreditsAvailable.mockResolvedValue(15);
-
+ it("checks MCP credits for a hosted session and marks it hosted", async () => {
const res = await create(true);
expect(res.statusCode).toBe(200);
- expect(mocks.mcpCreditsAvailable).toHaveBeenCalledWith(
- expect.objectContaining({ teamId: TEAM_ID, orgId: "org-1", value: 20 }),
+ expect(mocks.checkCredits).toHaveBeenCalledWith(
+ expect.objectContaining({
+ teamId: TEAM_ID,
+ orgId: "org-1",
+ value: 20,
+ featureId: "MCP_CREDITS",
+ }),
);
expect(mocks.markHostedMcp).toHaveBeenCalledWith(res.body.id);
});
- it("refuses a session the balance and MCP credits cannot cover together", async () => {
+ it("refuses a hosted session the MCP credits check refuses", async () => {
mocks.checkCredits.mockResolvedValue({ allowed: false, remaining: 5 });
- mocks.mcpCreditsAvailable.mockResolvedValue(14);
expect((await create(true)).statusCode).toBe(402);
expect(mocks.markHostedMcp).not.toHaveBeenCalled();
});
- it("counts MCP credits alone when a spend limit refused with balance left", async () => {
- mocks.checkCredits.mockResolvedValue({ allowed: false, remaining: 500 });
- mocks.mcpCreditsAvailable.mockResolvedValue(19);
- expect((await create(true)).statusCode).toBe(402);
-
- mocks.mcpCreditsAvailable.mockResolvedValue(20);
- expect((await create(true)).statusCode).toBe(200);
- });
-
- it("marks a hosted session even when the balance covers it", async () => {
- const res = await create(true);
- expect(res.statusCode).toBe(200);
- expect(mocks.mcpCreditsAvailable).not.toHaveBeenCalled();
- expect(mocks.markHostedMcp).toHaveBeenCalledWith(res.body.id);
- });
-
it("leaves a session not from the hosted MCP as it was", async () => {
mocks.checkCredits.mockResolvedValue({ allowed: false, remaining: 5 });
- mocks.mcpCreditsAvailable.mockResolvedValue(1000);
expect((await create(false)).statusCode).toBe(402);
mocks.checkCredits.mockResolvedValue({ allowed: true, remaining: 1000 });
expect((await create(false)).statusCode).toBe(200);
- expect(mocks.mcpCreditsAvailable).not.toHaveBeenCalled();
+ for (const [check] of mocks.checkCredits.mock.calls) {
+ expect(check.featureId).toBeUndefined();
+ }
expect(mocks.markHostedMcp).not.toHaveBeenCalled();
});
@@ -237,22 +222,16 @@ describe("browser sessions from hosted MCP calls", () => {
expect((await create(true)).statusCode).toBe(200);
});
- it("lets MCP credits pay for a prompt the balance alone refuses", async () => {
- mocks.checkCredits.mockResolvedValue({ allowed: false, remaining: 0 });
- mocks.mcpCreditsAvailable.mockResolvedValue(70);
- await expect(
- reserveBrowserPromptCredits(makeReq(true) as any, session),
- ).resolves.toBeUndefined();
-
- expect(mocks.markHostedMcp).toHaveBeenCalledWith("session-1");
+ it("checks MCP credits for a hosted prompt", async () => {
+ await reserveBrowserPromptCredits(makeReq(true) as any, session);
+ expect(mocks.checkCredits).toHaveBeenLastCalledWith(
+ expect.objectContaining({ value: 70, featureId: "MCP_CREDITS" }),
+ );
- mocks.mcpCreditsAvailable.mockResolvedValue(69);
+ mocks.checkCredits.mockResolvedValue({ allowed: false, remaining: 0 });
await expect(
reserveBrowserPromptCredits(makeReq(true) as any, session),
).rejects.toMatchObject({ status: 402 });
- await expect(
- reserveBrowserPromptCredits(makeReq(false) as any, session),
- ).rejects.toMatchObject({ status: 402 });
});
it("marks the session hosted on a hosted prompt the balance covers", async () => {
@@ -288,50 +267,4 @@ describe("browser sessions from hosted MCP calls", () => {
});
},
);
-
- it("finishes billing a hosted agent session before settlement returns", async () => {
- mocks.cameFromHostedMcp.mockResolvedValue(true);
- let billed = false;
- let billedBeforeTally = false;
- mocks.billTeam.mockImplementation(async () => {
- await new Promise(resolve => setTimeout(resolve, 20));
- billed = true;
- return { success: true };
- });
- mocks.recordRequestCredits.mockImplementation(async () => {
- billedBeforeTally = billed;
- return true;
- });
- mocks.settleOnce.mockResolvedValue({
- creditsBilled: 10,
- newlySettled: true,
- });
- await settleBrowserSession({ ...session, request_id: "agent-run" }, {
- status: "stopped",
- created_at: 1000,
- ended_at: 1300,
- } as any);
- expect(billed).toBe(true);
- expect(billedBeforeTally).toBe(true);
- });
-
- it("names the agent run on an agent session's charge", async () => {
- mocks.cameFromHostedMcp.mockResolvedValue(true);
- mocks.settleOnce.mockResolvedValue({
- creditsBilled: 10,
- newlySettled: true,
- });
- await settleBrowserSession({ ...session, request_id: "agent-run" }, {
- status: "stopped",
- created_at: 1000,
- ended_at: 1300,
- } as any);
- expect(mocks.billTeam).toHaveBeenCalledWith(TEAM_ID, "org-1", 10, null, {
- endpoint: "agent",
- jobId: "agent-run",
- chargeId: "session-1:destroy",
- agentRequestId: "agent-run",
- hostedMcp: true,
- });
- });
});
diff --git a/apps/api/src/controllers/v2/agent-credit-gate.ts b/apps/api/src/controllers/v2/agent-credit-gate.ts
index a7aba51a3..e3e4086d4 100644
--- a/apps/api/src/controllers/v2/agent-credit-gate.ts
+++ b/apps/api/src/controllers/v2/agent-credit-gate.ts
@@ -1,4 +1,7 @@
-import { autumnService } from "../../services/autumn/autumn.service";
+import {
+ autumnService,
+ CREDITS_FEATURE_ID,
+} from "../../services/autumn/autumn.service";
import { logger } from "../../lib/logger";
export const AGENT_DEFAULT_MAX_CREDITS = 2500;
@@ -28,9 +31,7 @@ export type AgentCreditGate =
* - otherwise: `maxCredits` is capped at the balance, and the agent service
* is told to stop the run if the balance runs out mid-flight.
*
- * A hosted MCP run counts the team's MCP credits on top of its balance, since
- * its charges spend them first: they can admit a run the balance alone would
- * refuse, and raise the cap, but never lower anything the balance allows.
+ * A hosted MCP run is gated on MCP credits, which its charges are tracked on.
*
* Fails open: whenever Autumn or firebill can't answer, the run starts as
* requested.
@@ -55,59 +56,26 @@ export async function gateAgentCredits({
};
if (!orgId || teamId === CREDIT_CHECK_EXEMPT_TEAM_ID) return asRequested;
+ const feature = hostedMcp
+ ? {
+ featureId: await autumnService.hostedMcpFeatureId(
+ teamId,
+ CREDITS_FEATURE_ID,
+ ),
+ }
+ : {};
const check = (value: number, source: string) =>
autumnService.checkCredits({
teamId,
orgId,
value,
properties: { source, path: "/agent", apiKeyId },
+ ...feature,
});
- const requested = requestedMaxCredits ?? AGENT_DEFAULT_MAX_CREDITS;
- const mcpCredits = () =>
- hostedMcp && requested > 0
- ? autumnService.mcpCreditsAvailable({
- teamId,
- orgId,
- // Never below the minimum, which a smaller cap still has to clear.
- value: Math.max(requested, AGENT_MIN_CREDITS),
- properties: {
- source: "agentCreditGate:mcp",
- path: "/agent",
- apiKeyId,
- },
- })
- : Promise.resolve(0);
- // Capped at what the balance and the MCP credits can pay together, and
- // stopped mid-run once they can't cover it.
- const capped = (balance: number, mcp: number): AgentCreditGate => {
- const maxCredits = Math.min(requested, Math.max(balance, 0) + mcp);
- if (maxCredits < requested) {
- logger.info("Capping agent maxCredits at the team's balance", {
- teamId,
- requestedMaxCredits,
- maxCredits,
- mcpCredits: mcp,
- });
- }
- return { allowed: true, maxCredits, stopOnInsufficientCredits: true };
- };
-
const minimum = await check(AGENT_MIN_CREDITS, "agentCreditGate");
if (minimum === null) return asRequested;
- if (!minimum.allowed) {
- // What is left of the balance counts only when the balance is why the
- // check refused; a spend limit refuses with plenty left, and the MCP
- // credits alone may still pay for the run.
- const fromBalance =
- minimum.remaining < AGENT_MIN_CREDITS
- ? Math.max(Math.floor(minimum.remaining), 0)
- : 0;
- const mcp = await mcpCredits();
- if (fromBalance + mcp < AGENT_MIN_CREDITS)
- return { allowed: false, balance: minimum.remaining };
- return capped(fromBalance, mcp);
- }
+ if (!minimum.allowed) return { allowed: false, balance: minimum.remaining };
// An unlimited balance comes back as int64 max. Probing past it overflows
// the billing service's integer, so treat it as unlimited and skip the probe.
@@ -120,7 +88,16 @@ export async function gateAgentCredits({
);
if (pastBalance === null || pastBalance.allowed) return asRequested;
if (await autumnService.hasAutoTopUp(orgId)) return asRequested;
- const mcp = await mcpCredits();
- if (balance <= 0 && mcp <= 0) return { allowed: false, balance };
- return capped(balance, mcp);
+ if (balance <= 0) return { allowed: false, balance };
+
+ const requested = requestedMaxCredits ?? AGENT_DEFAULT_MAX_CREDITS;
+ const maxCredits = Math.min(requested, balance);
+ if (maxCredits < requested) {
+ logger.info("Capping agent maxCredits at the team's balance", {
+ teamId,
+ requestedMaxCredits,
+ maxCredits,
+ });
+ }
+ return { allowed: true, maxCredits, stopOnInsufficientCredits: true };
}
diff --git a/apps/api/src/controllers/v2/agent.ts b/apps/api/src/controllers/v2/agent.ts
index a0a291c23..c28f0924d 100644
--- a/apps/api/src/controllers/v2/agent.ts
+++ b/apps/api/src/controllers/v2/agent.ts
@@ -1,8 +1,5 @@
import { v7 as uuidv7 } from "uuid";
-import {
- AGENT_REQUEST_CREDITS_SHARDS,
- initializeRequestCredits,
-} from "../../lib/request-credits-store";
+import { AGENT_REQUEST_CREDITS_SHARDS } from "../../lib/request-credits-store";
import { Response } from "express";
import {
AgentRequest,
@@ -28,7 +25,6 @@ import {
import { UnsafeDomainBlockedError } from "../../lib/threat-protection/error";
import { calculateThreatScanCredits } from "../../lib/scrape-billing";
import { billTeam } from "../../services/billing/credit_billing";
-import { agentMcpCreditsRequestId } from "../../services/billing/agent-mcp-credits";
import { emitRejectedScrapeActivityEvents } from "../../lib/siem-logging";
import { fetchAgentThread, threadErrorFor } from "./agent-thread";
import {
@@ -298,19 +294,6 @@ export async function agentController(
: {}),
});
- // The tally a hosted MCP run's calls add their MCP credits to, so a failed
- // run can give that part back to MCP credits (see tallyAgentMcpCredits).
- if (req.auth.hostedMcp) {
- await initializeRequestCredits(
- agentMcpCreditsRequestId(agentId),
- AGENT_REQUEST_CREDITS_SHARDS,
- ).catch(error =>
- logger.error("Failed to initialize the agent run's MCP credits tally", {
- error,
- }),
- );
- }
-
const passthrough = await fetch(
config.EXTRACT_V3_BETA_URL + "/internal/extracts",
{
diff --git a/apps/api/src/controllers/v2/batch-scrape.ts b/apps/api/src/controllers/v2/batch-scrape.ts
index 1b6b2795b..285ac3fea 100644
--- a/apps/api/src/controllers/v2/batch-scrape.ts
+++ b/apps/api/src/controllers/v2/batch-scrape.ts
@@ -147,11 +147,7 @@ export async function batchScrapeController(
const id = req.body.appendToId ?? uuidv7();
const billing: BillingMetadata = {
...(req.body.__agentInterop
- ? {
- endpoint: "agent" as const,
- jobId: id,
- agentRequestId: req.body.__agentInterop.requestId,
- }
+ ? { endpoint: "agent" as const, jobId: id }
: { endpoint: "batch_scrape" as const, jobId: id }),
externalRequestId: externalRequestId(req),
hostedMcp: req.auth.hostedMcp,
@@ -305,7 +301,7 @@ export async function batchScrapeController(
threatScanCredits > 0 &&
(req.body.__agentInterop?.shouldBill ?? true)
) {
- const charge = billTeam(
+ billTeam(
req.auth.team_id,
req.acuc?.org_id ?? null,
threatScanCredits,
@@ -322,8 +318,6 @@ export async function batchScrapeController(
`Failed to bill team ${req.auth.team_id} for ${threatScanCredits} threat scan credit(s): ${error}`,
);
});
- // See search: a hosted MCP agent run's MCP credits part lands first.
- if (billing.hostedMcp && billing.agentRequestId) await charge;
}
emitRejectedScrapeActivityEvents(
blocked.map(blockedUrl => ({
diff --git a/apps/api/src/controllers/v2/parse.ts b/apps/api/src/controllers/v2/parse.ts
index 6090959a6..6ea287874 100644
--- a/apps/api/src/controllers/v2/parse.ts
+++ b/apps/api/src/controllers/v2/parse.ts
@@ -372,11 +372,7 @@ export async function parseController(
(req.body.zeroDataRetention ?? false);
const billing: BillingMetadata = {
...(req.body.__agentInterop
- ? {
- endpoint: "agent" as const,
- jobId,
- agentRequestId: req.body.__agentInterop.requestId,
- }
+ ? { endpoint: "agent" as const, jobId }
: { endpoint: "parse" as const, jobId }),
externalRequestId: externalRequestId(req),
hostedMcp: req.auth.hostedMcp,
diff --git a/apps/api/src/controllers/v2/scrape.ts b/apps/api/src/controllers/v2/scrape.ts
index a18555631..1f89cd99a 100644
--- a/apps/api/src/controllers/v2/scrape.ts
+++ b/apps/api/src/controllers/v2/scrape.ts
@@ -222,11 +222,7 @@ export async function scrapeController(
});
const billing: BillingMetadata = {
...(req.body.__agentInterop
- ? {
- endpoint: "agent" as const,
- jobId,
- agentRequestId: req.body.__agentInterop.requestId,
- }
+ ? { endpoint: "agent" as const, jobId }
: { endpoint: "scrape" as const, jobId }),
externalRequestId: externalRequestId(req),
hostedMcp: req.auth.hostedMcp,
diff --git a/apps/api/src/controllers/v2/search.ts b/apps/api/src/controllers/v2/search.ts
index e2dc76a90..6f43f2d41 100644
--- a/apps/api/src/controllers/v2/search.ts
+++ b/apps/api/src/controllers/v2/search.ts
@@ -312,11 +312,7 @@ async function searchControllerInner(
const agentRequestId = req.body.__agentInterop?.requestId ?? null;
const billing: BillingMetadata = {
...(req.body.__agentInterop
- ? {
- endpoint: "agent" as const,
- jobId,
- agentRequestId: req.body.__agentInterop.requestId,
- }
+ ? { endpoint: "agent" as const, jobId }
: { endpoint: "search" as const, jobId }),
externalRequestId: externalRequestId(req),
hostedMcp: req.auth.hostedMcp,
@@ -527,7 +523,7 @@ async function searchControllerInner(
// Bill team for search credits only (scrape jobs bill themselves)
if (!isSearchPreview && shouldBill) {
- const charge = billTeam(
+ billTeam(
req.auth.team_id,
req.acuc?.org_id ?? null,
result.searchCredits,
@@ -540,9 +536,6 @@ async function searchControllerInner(
error,
}),
);
- // A hosted MCP agent run's call lands its MCP credits part before it
- // answers, so the run never reads its credits without it.
- if (billing.hostedMcp && billing.agentRequestId) await charge;
}
if (reservedKeylessCredits > 0) {
diff --git a/apps/api/src/lib/browser-lifecycle.ts b/apps/api/src/lib/browser-lifecycle.ts
index de66ca72f..ccefef1c4 100644
--- a/apps/api/src/lib/browser-lifecycle.ts
+++ b/apps/api/src/lib/browser-lifecycle.ts
@@ -35,6 +35,7 @@ import {
} from "../services/worker/nuq-router";
import {
autumnService,
+ CREDITS_FEATURE_ID,
DEFAULT_TEAM_LIMITS,
} from "../services/autumn/autumn.service";
import { billTeam } from "../services/billing/credit_billing";
@@ -184,17 +185,16 @@ async function createBrowserSessionInternal(
path: req.path,
apiKeyId: req.acuc?.api_key_id ?? null,
},
+ ...(req.auth.hostedMcp
+ ? {
+ featureId: await autumnService.hostedMcpFeatureId(
+ req.auth.team_id,
+ CREDITS_FEATURE_ID,
+ ),
+ }
+ : {}),
});
- if (
- credit !== null &&
- !credit.allowed &&
- !(await mcpCreditsCover(
- req,
- credit.remaining,
- estimatedCredits,
- "browserCreate:mcp",
- ))
- )
+ if (credit !== null && !credit.allowed)
throw new BrowserSessionError(
402,
`Insufficient credits for a ${options.ttl}s browser session (requires ~${estimatedCredits} credits).`,
@@ -297,31 +297,6 @@ async function createBrowserSessionInternal(
}
}
-/**
- * Whether a hosted MCP call's MCP credits pay for what its balance refused.
- * What is left of the balance counts only when the balance is why the check
- * refused; a spend limit refuses with plenty left.
- */
-async function mcpCreditsCover(
- req: RequestWithAuth<any, any, any>,
- remaining: number,
- value: number,
- source: string,
-): Promise<boolean> {
- if (!req.auth.hostedMcp || !req.acuc?.org_id) return false;
- const mcp = await autumnService.mcpCreditsAvailable({
- teamId: req.auth.team_id,
- orgId: req.acuc.org_id,
- value,
- properties: {
- source,
- path: req.path,
- apiKeyId: req.acuc.api_key_id ?? null,
- },
- });
- return (remaining < value ? Math.max(remaining, 0) : 0) + mcp >= value;
-}
-
export async function settleBrowserSession(
session: BrowserSessionRow,
browser: HangarBrowser,
@@ -387,7 +362,7 @@ async function settleBrowserSessionInternal(
: undefined;
if (session.should_bill) {
const hostedMcp = await didBrowserSessionComeFromHostedMcp(session.id);
- const charge = billTeam(
+ billTeam(
session.team_id,
await orgIdForTeam(session.team_id),
creditsBilled,
@@ -400,7 +375,6 @@ async function settleBrowserSessionInternal(
: "browser",
jobId: agentRequestId ?? session.id,
chargeId: `${session.id}:destroy`,
- ...(agentRequestId ? { agentRequestId } : {}),
...(hostedMcp ? { hostedMcp: true } : {}),
},
).catch(error =>
@@ -410,10 +384,6 @@ async function settleBrowserSessionInternal(
error,
}),
);
- // A hosted MCP agent run's charge lands, MCP credits part included,
- // before the run's credits below, so the run never reads one without
- // the other.
- if (hostedMcp && agentRequestId) await charge;
}
if (agentRequestId) {
await recordRequestCredits({
@@ -472,17 +442,16 @@ export async function reserveBrowserPromptCredits(
path: req.path,
apiKeyId: req.acuc?.api_key_id ?? null,
},
+ ...(req.auth.hostedMcp
+ ? {
+ featureId: await autumnService.hostedMcpFeatureId(
+ req.auth.team_id,
+ CREDITS_FEATURE_ID,
+ ),
+ }
+ : {}),
});
- if (
- credit !== null &&
- !credit.allowed &&
- !(await mcpCreditsCover(
- req,
- credit.remaining,
- credits,
- "browserPrompt:mcp",
- ))
- )
+ if (credit !== null && !credit.allowed)
throw new BrowserSessionError(
402,
"Insufficient credits for a browser prompt session.",
diff --git a/apps/api/src/routes/shared.ts b/apps/api/src/routes/shared.ts
index 5b8efe929..2ff4eb5b1 100644
--- a/apps/api/src/routes/shared.ts
+++ b/apps/api/src/routes/shared.ts
@@ -48,7 +48,7 @@ import { resolveIntegration } from "../utils/integration";
export function checkCreditsMiddleware(
_minimum?: number,
featureId: string = CREDITS_FEATURE_ID,
- // Set on routes whose billing spends a hosted MCP call's MCP credits first.
+ // Set on routes that bill a hosted MCP call to MCP credits.
{ mcpCredits = false }: { mcpCredits?: boolean } = {},
): (req: RequestWithAuth, res: Response, next: NextFunction) => void {
return (req, res, next) => {
@@ -159,6 +159,11 @@ export function checkCreditsMiddleware(
}
const requestedCredits = minimum ?? 1;
+ // Gated on the balance its charge will be tracked on.
+ const gateFeatureId =
+ mcpCredits && req.auth.hostedMcp
+ ? await autumnService.hostedMcpFeatureId(req.auth.team_id, featureId)
+ : featureId;
// No org means no billable identity to gate: keyless and preview teams
// carry none, and neither does the DB-authentication bypass. `checkCredits`
@@ -181,7 +186,7 @@ export function checkCreditsMiddleware(
path: req.path,
apiKeyId: req.acuc?.api_key_id ?? null,
},
- featureId,
+ featureId: gateFeatureId,
});
// Autumn is the source of truth for credits. If it's unavailable
@@ -193,27 +198,6 @@ export function checkCreditsMiddleware(
return next();
}
- // A hosted MCP call the team's balance can't cover still runs when its
- // MCP credits can, which billing then spends first (see billTeam).
- if (
- !autumnResult.allowed &&
- mcpCredits &&
- req.auth.hostedMcp &&
- (await autumnService.mcpCreditsAvailable({
- teamId: req.auth.team_id,
- orgId,
- value: requestedCredits,
- properties: {
- source: "checkCreditsMiddleware:mcp",
- path: req.path,
- apiKeyId: req.acuc?.api_key_id ?? null,
- },
- })) >= requestedCredits
- ) {
- req.account = { remainingCredits: Infinity };
- return next();
- }
-
const success = autumnResult.allowed;
// When Autumn allows the request (including overage), don't let a
// small remaining balance clamp downstream limits (e.g. crawl).
@@ -243,7 +227,7 @@ export function checkCreditsMiddleware(
path: req.path,
apiKeyId: req.acuc?.api_key_id ?? null,
},
- featureId,
+ featureId: gateFeatureId,
});
if (clampedResult === null || clampedResult.allowed) {
diff --git a/apps/api/src/services/alexandria/retrieve.test.ts b/apps/api/src/services/alexandria/retrieve.test.ts
index d099f3ffc..140729b2e 100644
--- a/apps/api/src/services/alexandria/retrieve.test.ts
+++ b/apps/api/src/services/alexandria/retrieve.test.ts
@@ -17,8 +17,6 @@ const mocks = vi.hoisted(() => {
lock: vi.fn(),
finalize: vi.fn(),
refund: vi.fn(),
- mcpAvailable: vi.fn(),
- refundMcp: vi.fn(),
billAdd: vi.fn(),
report: vi.fn(),
initCredits: vi.fn(),
@@ -43,14 +41,12 @@ vi.mock("../../lib/exchange", () => ({
vi.mock("../autumn/autumn.service", () => ({
autumnService: {
lockCredits: mocks.lock,
+ hostedMcpFeatureId: async () => "MCP_CREDITS",
finalizeCreditsLock: mocks.finalize,
refundCredits: mocks.refund,
- mcpCreditsAvailable: mocks.mcpAvailable,
- refundHostedMcpCredits: mocks.refundMcp,
isRoutedThroughFirebill: async () => false,
},
featureIdForBillingEndpoint: () => "credits",
- MCP_CREDITS_FEATURE_ID: "mcp_credits",
}));
import { retrieveProviders } from "./retrieve";
@@ -96,8 +92,6 @@ beforeEach(() => {
mocks.lock.mockResolvedValue({ status: "locked", lockId: "held" });
mocks.finalize.mockResolvedValue(true);
mocks.refund.mockResolvedValue(undefined);
- mocks.mcpAvailable.mockResolvedValue(0);
- mocks.refundMcp.mockResolvedValue(true);
mocks.billAdd.mockResolvedValue({});
mocks.report.mockResolvedValue(true);
mocks.initCredits.mockResolvedValue(true);
@@ -157,6 +151,31 @@ it("quotes, reserves, executes within budget, settles actual usage, records once
expect(mocks.billAdd).toHaveBeenCalledTimes(1);
});
+it("holds and settles a hosted MCP call's whole budget on MCP credits", async () => {
+ expect(await run({ hostedMcp: true })).toMatchObject({
+ status: 200,
+ executed: true,
+ });
+ expect(mocks.lock).toHaveBeenCalledExactlyOnceWith(
+ expect.objectContaining({ value: 5, featureId: "MCP_CREDITS" }),
+ );
+ expect(mocks.finalize).toHaveBeenCalledExactlyOnceWith(
+ expect.objectContaining({
+ overrideValue: 3,
+ heldValue: 5,
+ featureId: "MCP_CREDITS",
+ }),
+ );
+ expect(mocks.billAdd).toHaveBeenCalledWith(
+ "bill_team",
+ expect.objectContaining({
+ credits: 3,
+ billing: expect.objectContaining({ hostedMcp: true }),
+ }),
+ expect.anything(),
+ );
+});
+
it("refuses a different payload under the same x-request-id", async () => {
await run();
const other = await run({
@@ -287,6 +306,14 @@ it("refunds the direct-Autumn charge and leaves the Exchange usage pending when
expect(mocks.report).not.toHaveBeenCalled();
});
+it("refunds a hosted MCP charge to MCP credits when the ledger enqueue fails", async () => {
+ mocks.billAdd.mockRejectedValue(new Error("queue down"));
+ expect((await run({ hostedMcp: true })).status).toBe(200);
+ expect(mocks.refund).toHaveBeenCalledExactlyOnceWith(
+ expect.objectContaining({ value: 3, featureId: "MCP_CREDITS" }),
+ );
+});
+
it("treats a team with no org as a skipped hold and executes nothing", async () => {
const refused = await run({ orgId: null });
expect(refused.status).toBe(503);
@@ -761,195 +788,3 @@ it("does not retry the write when the credit store is unavailable", async () =>
});
expect(mocks.recordCredits).toHaveBeenCalledTimes(1);
});
-
-describe("hosted MCP calls", () => {
- const hosted = (overrides: Record<string, unknown> = {}) =>
- run({ hostedMcp: true, ...overrides });
- const locks = () => mocks.lock.mock.calls.map(([arg]) => arg);
- const settles = () => mocks.finalize.mock.calls.map(([arg]) => arg);
- const billing = () => mocks.billAdd.mock.calls[0][1].billing;
-
- beforeEach(() => {
- mocks.lock.mockImplementation(async arg => ({
- status: "locked",
- lockId: arg.lockId,
- }));
- });
-
- it("holds and settles on the MCP credits alone when they cover the call", async () => {
- mocks.mcpAvailable.mockResolvedValue(5);
- expect((await hosted()).status).toBe(200);
- expect(mocks.mcpAvailable).toHaveBeenCalledWith(
- expect.objectContaining({ teamId: "team", orgId: "org", value: 5 }),
- );
- expect(locks()).toEqual([
- expect.objectContaining({
- value: 5,
- featureId: "mcp_credits",
- lockId: expect.stringMatching(/_mcp$/),
- }),
- ]);
- expect(settles()).toEqual([
- expect.objectContaining({
- lockId: locks()[0].lockId,
- action: "confirm",
- overrideValue: 3,
- heldValue: 5,
- featureId: "mcp_credits",
- }),
- ]);
- expect(billing()).toEqual(
- expect.objectContaining({ hostedMcp: true, mcpCredits: 3 }),
- );
- });
-
- it("splits the hold and the settle, MCP credits first, when they cover part", async () => {
- mocks.mcpAvailable.mockResolvedValue(2);
- expect((await hosted()).status).toBe(200);
- expect(locks()).toEqual([
- expect.objectContaining({ value: 2, featureId: "mcp_credits" }),
- expect.objectContaining({ value: 3, featureId: "credits" }),
- ]);
- expect(settles()).toEqual(
- expect.arrayContaining([
- expect.objectContaining({
- featureId: "mcp_credits",
- action: "confirm",
- overrideValue: 2,
- heldValue: 2,
- }),
- expect.objectContaining({
- featureId: "credits",
- action: "confirm",
- overrideValue: 1,
- heldValue: 3,
- }),
- ]),
- );
- expect(settles()).toHaveLength(2);
- expect(billing()).toEqual(
- expect.objectContaining({ hostedMcp: true, mcpCredits: 2 }),
- );
- });
-
- it("releases the regular hold when the MCP part pays the whole charge", async () => {
- mocks.mcpAvailable.mockResolvedValue(4);
- expect((await hosted()).status).toBe(200);
- expect(settles()).toEqual(
- expect.arrayContaining([
- expect.objectContaining({
- featureId: "mcp_credits",
- overrideValue: 3,
- heldValue: 4,
- }),
- expect.objectContaining({ featureId: "credits", action: "release" }),
- ]),
- );
- expect(billing()).toEqual(expect.objectContaining({ mcpCredits: 3 }));
- });
-
- it("holds and settles on regular credits alone once the MCP credits are used up", async () => {
- expect((await hosted()).status).toBe(200);
- expect(locks()).toEqual([
- expect.objectContaining({ value: 5, featureId: "credits" }),
- ]);
- expect(settles()).toEqual([
- expect.objectContaining({ featureId: "credits", overrideValue: 3 }),
- ]);
- expect(billing()).not.toHaveProperty("mcpCredits");
- });
-
- it("hands the MCP hold back when the rest cannot be held", async () => {
- mocks.mcpAvailable.mockResolvedValue(2);
- mocks.lock
- .mockImplementationOnce(async arg => ({
- status: "locked",
- lockId: arg.lockId,
- }))
- .mockResolvedValueOnce({ status: "denied" });
- const refused = await hosted();
- expect(refused.status).toBe(402);
- expect(executions()).toHaveLength(0);
- expect(settles()).toEqual([
- expect.objectContaining({
- featureId: "mcp_credits",
- action: "release",
- heldValue: 2,
- }),
- ]);
- });
-
- it.each([
- ["the MCP part", "mcp_credits", 1, 0],
- ["the rest", "credits", 2, 2],
- ])(
- "records only what landed when %s does not settle",
- async (_, failing, credits, mcpCredits) => {
- mocks.mcpAvailable.mockResolvedValue(2);
- mocks.finalize.mockImplementation(async arg => arg.featureId !== failing);
- expect((await hosted({ agentRequestId: "agent-run" })).status).toBe(200);
- expect(mocks.billAdd).toHaveBeenCalledWith(
- "bill_team",
- expect.objectContaining({
- credits,
- billing: expect.objectContaining(
- mcpCredits > 0 ? { mcpCredits } : {},
- ),
- }),
- expect.anything(),
- );
- if (mcpCredits === 0) expect(billing()).not.toHaveProperty("mcpCredits");
- expect(mocks.recordCredits).toHaveBeenCalledWith(
- expect.objectContaining({ requestId: "agent-run", credits }),
- );
- expect(mocks.report).not.toHaveBeenCalled();
- },
- );
-
- it("releases both holds on a definitive refusal", async () => {
- mocks.mcpAvailable.mockResolvedValue(2);
- exchangeAnswers({ error: "nope" }, 400);
- expect((await hosted()).status).toBe(400);
- expect(settles()).toHaveLength(2);
- expect(settles().every(settle => settle.action === "release")).toBe(true);
- });
-
- it("refunds each part to its own balance when the ledger enqueue fails", async () => {
- mocks.mcpAvailable.mockResolvedValue(2);
- mocks.billAdd.mockRejectedValue(new Error("queue down"));
- expect((await hosted()).status).toBe(200);
- expect(mocks.refundMcp).toHaveBeenCalledWith(
- expect.objectContaining({ value: 3, mcpCredits: 2, orgId: "org" }),
- );
- expect(mocks.refund).not.toHaveBeenCalled();
- });
-
- it("adds the MCP part of an agent call to the run's MCP tally", async () => {
- mocks.mcpAvailable.mockResolvedValue(2);
- await hosted({ agentRequestId: "agent-run" });
- expect(mocks.recordCredits).toHaveBeenCalledWith({
- requestId: "agent-run",
- jobId: expect.stringMatching(/^alexandria:/),
- credits: 3,
- });
- expect(mocks.recordCredits).toHaveBeenCalledWith({
- requestId: "agent-run:mcp",
- jobId: expect.stringMatching(/^alexandria:/),
- credits: 2,
- });
- });
-
- it("leaves a call not from the hosted MCP as it was", async () => {
- mocks.mcpAvailable.mockResolvedValue(5);
- expect((await run({ agentRequestId: "agent-run" })).status).toBe(200);
- expect(mocks.mcpAvailable).not.toHaveBeenCalled();
- expect(locks()).toEqual([
- expect.objectContaining({ value: 5, featureId: "credits" }),
- ]);
- expect(billing()).toEqual({
- endpoint: "scrape",
- chargeId: expect.any(String),
- });
- expect(mocks.recordCredits).toHaveBeenCalledTimes(1);
- });
-});
diff --git a/apps/api/src/services/alexandria/retrieve.ts b/apps/api/src/services/alexandria/retrieve.ts
index 4110e6303..70ec3a1f1 100644
--- a/apps/api/src/services/alexandria/retrieve.ts
+++ b/apps/api/src/services/alexandria/retrieve.ts
@@ -12,9 +12,7 @@ import { logger } from "../../lib/logger";
import {
autumnService,
featureIdForBillingEndpoint,
- MCP_CREDITS_FEATURE_ID,
} from "../autumn/autumn.service";
-import { tallyAgentMcpCredits } from "../billing/agent-mcp-credits";
import type { LockCreditsResult } from "../autumn/types";
import {
AGENT_REQUEST_CREDITS_SHARDS,
@@ -54,10 +52,6 @@ type Retrieval = {
scrapeId: string;
lockId?: string;
maximumCredits?: number;
- // The part of the hold a hosted MCP call's MCP credits took, under its own
- // lock; the rest of maximumCredits is held under lockId.
- mcpLockId?: string;
- mcpHeldCredits?: number;
response?: ExchangeResponse;
// Set instead of response under zero data retention.
responseWithheld?: boolean;
@@ -113,7 +107,7 @@ export async function retrieveProviders(input: {
requestId: string;
/** The agent run this call belongs to: its credits join that run's call credits. */
agentRequestId?: string;
- /** A verified hosted MCP call: its MCP credits are held and spent first. */
+ /** A verified hosted MCP call: held and charged on MCP credits. */
hostedMcp?: boolean;
scrapeId: string;
timeoutMs: number;
@@ -264,7 +258,12 @@ export async function retrieveProviders(input: {
return notExecuted(unresolved(id));
}
- const featureId = featureIdForBillingEndpoint("scrape");
+ const featureId = input.hostedMcp
+ ? await autumnService.hostedMcpFeatureId(
+ input.teamId,
+ featureIdForBillingEndpoint("scrape"),
+ )
+ : featureIdForBillingEndpoint("scrape");
const properties = {
source: "alexandria",
endpoint: "scrape",
@@ -283,16 +282,10 @@ export async function retrieveProviders(input: {
};
let lockId: string | undefined;
- let mcpLockId: string | undefined;
- let mcpHeldCredits = 0;
let maximumCredits: number;
let refundable = false;
- const settle = async (
- lock: string,
- heldValue: number,
- credits: number,
- lockFeatureId: string,
- ) => {
+ const finalize = async (credits: number) => {
+ if (!lockId) return true;
const settled = await autumnService.finalizeCreditsLock({
// Omitted without an org: the settle goes straight to Autumn, the route
// an unnameable org already took.
@@ -300,46 +293,22 @@ export async function retrieveProviders(input: {
input.orgId !== null
? { teamId: input.teamId, orgId: input.orgId }
: undefined,
- lockId: lock,
+ lockId,
action: credits > 0 ? "confirm" : "release",
...(credits > 0 ? { overrideValue: credits } : {}),
- heldValue,
- featureId: lockFeatureId,
+ heldValue: maximumCredits,
+ featureId,
properties,
});
if (!settled)
logger.error("Provider credits were not settled; hold expires unbilled", {
chargeId: id,
teamId: input.teamId,
- lockId: lock,
+ lockId,
credits,
});
return settled;
};
- // The MCP credits pay first, up to what they hold; the rest settles on the
- // scrape feature's hold.
- const mcpPart = (credits: number) => Math.min(credits, mcpHeldCredits);
- const finalizeParts = (credits: number) =>
- Promise.all([
- mcpLockId
- ? settle(
- mcpLockId,
- mcpHeldCredits,
- mcpPart(credits),
- MCP_CREDITS_FEATURE_ID,
- )
- : true,
- lockId
- ? settle(
- lockId,
- maximumCredits - mcpHeldCredits,
- credits - mcpPart(credits),
- featureId,
- )
- : true,
- ]);
- const finalize = async (credits: number) =>
- (await finalizeParts(credits)).every(Boolean);
try {
const denied = await authorizeProviders(
@@ -422,45 +391,16 @@ export async function retrieveProviders(input: {
input.teamId,
input.orgId,
));
- if (input.hostedMcp) {
- const mcpCredits = await autumnService.mcpCreditsAvailable({
- teamId: input.teamId,
- orgId: input.orgId,
- value: maximumCredits,
- properties,
- });
- const mcpHold =
- mcpCredits > 0
- ? await autumnService.lockCredits({
- teamId: input.teamId,
- orgId: input.orgId,
- value: mcpCredits,
- lockId: `alexandria_${id}_mcp`,
- expiresAt: record.deadline + HOLD_GRACE_MS,
- featureId: MCP_CREDITS_FEATURE_ID,
- properties,
- })
- : undefined;
- if (mcpHold?.status === "locked") {
- mcpLockId = mcpHold.lockId;
- mcpHeldCredits = mcpCredits;
- }
- }
- // Held in full on the MCP credits, nothing is left to hold here.
- hold =
- mcpHeldCredits < maximumCredits
- ? await autumnService.lockCredits({
- teamId: input.teamId,
- orgId: input.orgId,
- value: maximumCredits - mcpHeldCredits,
- lockId: `alexandria_${id}`,
- expiresAt: record.deadline + HOLD_GRACE_MS,
- featureId,
- properties,
- })
- : { status: "locked", lockId: "" };
+ hold = await autumnService.lockCredits({
+ teamId: input.teamId,
+ orgId: input.orgId,
+ value: maximumCredits,
+ lockId: `alexandria_${id}`,
+ expiresAt: record.deadline + HOLD_GRACE_MS,
+ featureId,
+ properties,
+ });
}
- if (hold.status !== "locked" && mcpLockId) await finalize(0);
if (hold.status === "denied")
return refuse(
hold.reason === "gate_unavailable"
@@ -482,20 +422,16 @@ export async function retrieveProviders(input: {
"Credit reservation is unavailable. No provider was executed.",
),
);
- lockId = hold.lockId || undefined;
+ lockId = hold.lockId;
}
// The record is written before the provider runs, so a failed write means
// nothing ran: the catch below hands back the index calls and the hold.
record.lockId = lockId;
record.maximumCredits = maximumCredits;
- if (mcpLockId) {
- record.mcpLockId = mcpLockId;
- record.mcpHeldCredits = mcpHeldCredits;
- }
await write(record);
} catch (error) {
- if (lockId || mcpLockId) await finalize(0).catch(() => false);
+ if (lockId) await finalize(0).catch(() => false);
await releaseIndexCalls();
await redisRateLimitClient.del(key).catch(() => {});
throw error;
@@ -567,16 +503,10 @@ export async function retrieveProviders(input: {
if (!receiptMatches)
return fail("Alexandria receipt did not match the request");
- const [mcpSettled, restSettled] = await finalizeParts(answer.creditsCost);
- const settled = mcpSettled && restSettled;
- // A split settle that landed in part records the part that landed; the
- // rest goes unbilled, as a whole settle that does not land does.
- const mcpCredits = mcpSettled ? mcpPart(answer.creditsCost) : 0;
- const credits =
- mcpCredits +
- (restSettled ? answer.creditsCost - mcpPart(answer.creditsCost) : 0);
+ const credits = answer.creditsCost;
+ const settled = await finalize(credits);
const recorded =
- billable && credits > 0
+ settled && billable && credits > 0
? await recordLedgerUsage(
id,
input.teamId,
@@ -584,19 +514,14 @@ export async function retrieveProviders(input: {
input.apiKeyId,
credits,
refundable,
- mcpCredits,
+ input.hostedMcp === true,
)
: settled;
- const charged = billable && credits > 0 && (recorded || !refundable);
- if (charged && input.agentRequestId) {
+ const charged =
+ settled && billable && credits > 0 && (recorded || !refundable);
+ if (charged && input.agentRequestId)
await recordAgentCredits(input.agentRequestId, id, credits);
- await tallyAgentMcpCredits(
- { endpoint: "agent", agentRequestId: input.agentRequestId },
- `alexandria:${id}`,
- mcpCredits,
- );
- }
- if (!billable || (settled && recorded))
+ if (!billable || recorded)
void reportExchangeUsageBilling({
requestId: id,
status: billable ? "confirmed" : "void",
@@ -653,7 +578,7 @@ async function recordLedgerUsage(
apiKeyId: number | null,
credits: number,
refundable: boolean,
- mcpCredits: number,
+ hostedMcp: boolean,
): Promise<boolean> {
for (let attempt = 1; ; attempt++) {
try {
@@ -666,7 +591,7 @@ async function recordLedgerUsage(
billing: {
endpoint: "scrape",
chargeId: id,
- ...(mcpCredits > 0 ? { hostedMcp: true, mcpCredits } : {}),
+ ...(hostedMcp ? { hostedMcp: true } : {}),
},
is_extract: false,
timestamp: new Date().toISOString(),
@@ -690,24 +615,24 @@ async function recordLedgerUsage(
);
// refundable is only ever set with an org in hand; named again so the
// type says so.
- if (refundable && orgId !== null) {
- const refund = {
+ if (refundable && orgId !== null)
+ await autumnService.refundCredits({
teamId,
orgId,
value: credits,
- featureId: featureIdForBillingEndpoint("scrape"),
+ featureId: hostedMcp
+ ? await autumnService.hostedMcpFeatureId(
+ teamId,
+ featureIdForBillingEndpoint("scrape"),
+ )
+ : featureIdForBillingEndpoint("scrape"),
properties: {
source: "alexandria",
endpoint: "scrape",
chargeId: id,
},
idempotencyKey: `fc:refund:scrape:${id}`,
- };
- // A split charge refunds each part to the balance it was settled on.
- await (mcpCredits > 0
- ? autumnService.refundHostedMcpCredits({ ...refund, mcpCredits })
- : autumnService.refundCredits(refund));
- }
+ });
return false;
}
}
diff --git a/apps/api/src/services/autumn/__tests__/mcp-credits.test.ts b/apps/api/src/services/autumn/__tests__/mcp-credits.test.ts
index 1f02cce58..df0f6e67a 100644
--- a/apps/api/src/services/autumn/__tests__/mcp-credits.test.ts
+++ b/apps/api/src/services/autumn/__tests__/mcp-credits.test.ts
@@ -1,44 +1,31 @@
/**
- * Unit tests for hosted MCP charges in AutumnService: how much the team's MCP
- * credits can pay (mcpCreditsAvailable), and how a charge is split between
- * them and the endpoint's own feature (trackHostedMcpCredits).
+ * Unit tests for hosted MCP charges in AutumnService: a hosted charge is made
+ * whole on MCP credits at the customer level, and Autumn decides which
+ * balances pay for it.
*/
import { vi } from "vitest";
-const { mockCheck, mockTrack, mockAutumnClient, state, redis } = vi.hoisted(
- () => {
- const store = new Map<string, string>();
- const redis = {
- store,
- get: vi.fn(async (key: string) => store.get(key) ?? null),
- set: vi.fn(async (key: string, value: unknown, ...args: unknown[]) => {
- if (args.includes("NX") && store.has(key)) return null;
- store.set(key, String(value));
- return "OK";
- }),
- };
- const mockCheck = vi.fn<(args: any) => Promise<any>>();
- const mockTrack = vi.fn<(args: any) => Promise<void>>();
- const mockAutumnClient = {
- customers: { getOrCreate: vi.fn(), get: vi.fn() },
- entities: {
- get: vi.fn().mockResolvedValue({ id: "team-1" }),
- create: vi.fn(),
- },
- balances: { finalize: vi.fn() },
- check: mockCheck,
- track: mockTrack,
- };
- return {
- mockCheck,
- mockTrack,
- mockAutumnClient,
- state: { gatewayStubRow: null as unknown },
- redis,
- };
- },
-);
+const { mockCheck, mockTrack, mockAutumnClient, state } = vi.hoisted(() => {
+ const mockCheck = vi.fn<(args: any) => Promise<any>>();
+ const mockTrack = vi.fn<(args: any) => Promise<any>>();
+ const mockAutumnClient = {
+ customers: { getOrCreate: vi.fn(), get: vi.fn() },
+ entities: {
+ get: vi.fn().mockResolvedValue({ id: "team-1" }),
+ create: vi.fn(),
+ },
+ balances: { finalize: vi.fn() },
+ check: mockCheck,
+ track: mockTrack,
+ };
+ return {
+ mockCheck,
+ mockTrack,
+ mockAutumnClient,
+ state: { firebill: false, gateway: false },
+ };
+});
vi.mock("../client", () => ({
autumnClient: mockAutumnClient,
@@ -46,28 +33,34 @@ vi.mock("../client", () => ({
vi.mock("../../queue-service", () => ({
getFirebillTrackRetryQueue: () => ({ add: vi.fn() }),
- getRedisConnection: () => redis,
}));
vi.mock("../../../db/connection", () => ({
- get dbRr() {
- return {
- select: () => ({
- from: () => ({
- where: () => ({
- limit: () =>
- Promise.resolve(
- state.gatewayStubRow ? [state.gatewayStubRow] : [],
- ),
- }),
+ dbRr: {
+ select: () => ({
+ from: () => ({
+ where: () => ({
+ limit: () =>
+ Promise.resolve(state.gateway ? [{ team_id: "team-1" }] : []),
}),
}),
- };
+ }),
},
}));
vi.mock("../../../config", () => ({
- config: { MCP_CREDITS_FEATURE_ID: "MCP_CREDITS_FROM_CONFIG" },
+ get config() {
+ return {
+ MCP_CREDITS_FEATURE_ID: "MCP_CREDITS_FROM_CONFIG",
+ ...(state.firebill
+ ? {
+ FIREBILL_URL: "http://firebill.test",
+ FIREBILL_SECRET: "fb-secret",
+ FIREBILL_ORG_IDS: ["org-1"],
+ }
+ : {}),
+ };
+ },
}));
import { AutumnService, MCP_CREDITS_FEATURE_ID } from "../autumn.service";
@@ -76,364 +69,179 @@ const charge = {
teamId: "team-1",
orgId: "org-1",
value: 5,
- properties: { source: "billTeam", endpoint: "scrape" },
- featureId: "CREDITS",
- idempotencyKey: "fc:track:scrape:job-1",
- externalRequestId: "op-1",
+ properties: { source: "billTeam", endpoint: "search" },
+ featureId: "SEARCH_CREDITS",
+ idempotencyKey: "fc:track:search:job-1",
};
-const CREDITS_POOL = 1000;
-
-/**
- * The customer's balances: MCP credits' own (null when the plan has none) and
- * the CREDITS pool. As in Autumn, MCP credits draw from the CREDITS credit
- * system once their own balance runs short, so a check on them answers with
- * the pool.
- */
-function mcpBalance(remaining: number | null, unlimited = false) {
- mockAutumnClient.customers.get.mockResolvedValue({
- id: "org-1",
- balances: {
- ...(remaining === null
- ? {}
- : {
- [MCP_CREDITS_FEATURE_ID]: {
- featureId: MCP_CREDITS_FEATURE_ID,
- remaining,
- unlimited,
- },
- }),
- CREDITS: {
- featureId: "CREDITS",
- remaining: CREDITS_POOL,
- unlimited: false,
- },
- },
- });
- mockCheck.mockImplementation(async ({ featureId, requiredBalance }) => {
- const own = featureId === MCP_CREDITS_FEATURE_ID ? (remaining ?? 0) : 0;
- const balance =
- own >= requiredBalance
- ? { featureId, remaining: own }
- : { featureId: "CREDITS", remaining: CREDITS_POOL };
- return {
- allowed: balance.remaining >= requiredBalance,
- customerId: "org-1",
- balance,
- };
- });
-}
-
-/** The Autumn track calls a run of fn makes. */
-async function tracksOf(fn: (service: AutumnService) => Promise<unknown>) {
- mockTrack.mockClear();
- await fn(new AutumnService());
- return mockTrack.mock.calls.map(([args]) => args);
-}
-
beforeEach(() => {
vi.clearAllMocks();
- state.gatewayStubRow = null;
- redis.store.clear();
- mockTrack.mockResolvedValue(undefined);
- mcpBalance(100);
+ state.firebill = false;
+ state.gateway = false;
+ mockTrack.mockResolvedValue({});
+ mockCheck.mockResolvedValue({ allowed: true, balance: { remaining: 100 } });
});
-describe("mcpCreditsAvailable", () => {
- it("reads the feature id from config", () => {
- expect(MCP_CREDITS_FEATURE_ID).toBe("MCP_CREDITS_FROM_CONFIG");
- });
-
- it("pays the whole charge when the MCP credits cover it", async () => {
- expect(await new AutumnService().mcpCreditsAvailable(charge)).toBe(5);
- expect(mockAutumnClient.customers.get).toHaveBeenCalledExactlyOnceWith({
- customerId: "org-1",
- });
- });
-
- it("pays a whole charge from unlimited MCP credits", async () => {
- mcpBalance(0, true);
- expect(await new AutumnService().mcpCreditsAvailable(charge)).toBe(5);
- });
-
- it.each([
- ["fall short of the charge", 4, 5, 4],
- ["are used up", 0, 40, 0],
- ])(
- "counts only the MCP credits' own balance when they %s, not the CREDITS pool",
- async (_name, remaining, value, expected) => {
- mcpBalance(remaining);
- expect(
- await new AutumnService().mcpCreditsAvailable({ ...charge, value }),
- ).toBe(expected);
- expect(mockCheck).not.toHaveBeenCalled();
- },
- );
-
- it("pays what is left when the MCP credits fall short", async () => {
- mcpBalance(3.5);
- expect(await new AutumnService().mcpCreditsAvailable(charge)).toBe(3);
- });
-
- it.each([
- ["used up", 0],
- ["missing from the team's plan", null],
- ])("pays nothing when the MCP credits are %s", async (_name, remaining) => {
- mcpBalance(remaining);
- expect(await new AutumnService().mcpCreditsAvailable(charge)).toBe(0);
- });
+it("reads the feature id from config", () => {
+ expect(MCP_CREDITS_FEATURE_ID).toBe("MCP_CREDITS_FROM_CONFIG");
+});
- it("pays nothing when Autumn reports no usable remaining balance", async () => {
- mcpBalance(Number.NaN);
- const service = new AutumnService();
- expect(await service.mcpCreditsAvailable(charge)).toBe(0);
- expect(await service.trackHostedMcpCredits(charge)).toEqual({
- tracked: true,
- mcpCredits: 0,
- });
+describe("hostedMcpFeatureId", () => {
+ it("makes a hosted MCP charge on MCP credits", async () => {
+ expect(
+ await new AutumnService().hostedMcpFeatureId("team-1", "SEARCH_CREDITS"),
+ ).toBe(MCP_CREDITS_FEATURE_ID);
});
- it("pays nothing when Autumn can't answer", async () => {
- mockAutumnClient.customers.get.mockRejectedValue(new Error("ECONNRESET"));
- expect(await new AutumnService().mcpCreditsAvailable(charge)).toBe(0);
- });
+ it("keeps a partner-provisioned team's hosted charge on its own feature", async () => {
+ state.gateway = true;
- it("never asks Autumn for a partner-provisioned or preview team, or a zero charge", async () => {
- const service = new AutumnService();
- state.gatewayStubRow = { team_id: "team-1" };
- expect(await service.mcpCreditsAvailable(charge)).toBe(0);
- state.gatewayStubRow = null;
expect(
- await service.mcpCreditsAvailable({ ...charge, teamId: "preview_1" }),
- ).toBe(0);
- expect(await service.mcpCreditsAvailable({ ...charge, value: 0 })).toBe(0);
- expect(mockAutumnClient.customers.get).not.toHaveBeenCalled();
+ await new AutumnService().hostedMcpFeatureId("team-1", "SEARCH_CREDITS"),
+ ).toBe("SEARCH_CREDITS");
});
});
-describe("trackHostedMcpCredits", () => {
- it("tracks the whole charge on MCP credits that cover it", async () => {
- const tracks = await tracksOf(service =>
- service
- .trackHostedMcpCredits(charge)
- .then(result =>
- expect(result).toEqual({ tracked: true, mcpCredits: 5 }),
- ),
- );
-
- expect(tracks).toEqual([
- expect.objectContaining({
+describe("the entity a charge is made against", () => {
+ it("tracks an MCP credits charge once, whole, on the customer", async () => {
+ expect(
+ await new AutumnService().trackCredits({
+ ...charge,
featureId: MCP_CREDITS_FEATURE_ID,
- value: 5,
- properties: charge.properties,
}),
- ]);
- });
-
- it("tracks the whole charge on the endpoint's feature once the MCP credits are used up", async () => {
- mcpBalance(0);
-
- const tracks = await tracksOf(service =>
- service
- .trackHostedMcpCredits({ ...charge, value: 40 })
- .then(result =>
- expect(result).toEqual({ tracked: true, mcpCredits: 0 }),
- ),
- );
-
- expect(tracks).toEqual([
- expect.objectContaining({ featureId: "CREDITS", value: 40 }),
- ]);
- });
-
- it("splits a charge the MCP credits only partly cover", async () => {
- mcpBalance(3);
-
- const tracks = await tracksOf(service =>
- service
- .trackHostedMcpCredits(charge)
- .then(result =>
- expect(result).toEqual({ tracked: true, mcpCredits: 3 }),
- ),
- );
+ ).toBe(true);
- expect(tracks).toHaveLength(2);
- expect(tracks).toContainEqual(
- expect.objectContaining({ featureId: MCP_CREDITS_FEATURE_ID, value: 3 }),
- );
- expect(tracks).toContainEqual(
- expect.objectContaining({
- featureId: "CREDITS",
- value: 2,
- overageBehavior: "overflow",
- }),
- );
+ expect(mockTrack).toHaveBeenCalledOnce();
+ const [track] = mockTrack.mock.calls[0];
+ expect(track).toMatchObject({
+ customerId: "org-1",
+ featureId: MCP_CREDITS_FEATURE_ID,
+ value: 5,
+ overageBehavior: "overflow",
+ });
+ expect(track.entityId).toBeUndefined();
+ expect(mockAutumnClient.customers.get).not.toHaveBeenCalled();
});
- it("gives the MCP part its own firebill idempotency key", async () => {
- mcpBalance(3);
+ it("refunds MCP credits on the customer and other features on the team", async () => {
const service = new AutumnService();
- const trackCredits = vi.spyOn(service, "trackCredits");
- await service.trackHostedMcpCredits(charge);
-
- expect(trackCredits).toHaveBeenCalledWith(
- expect.objectContaining({
- featureId: MCP_CREDITS_FEATURE_ID,
- idempotencyKey: "fc:track:scrape:job-1:mcp",
- }),
- );
- expect(trackCredits).toHaveBeenCalledWith({ ...charge, value: 2 });
- });
-
- it.each([
- ["used up", 0],
- ["missing from the team's plan", null],
- ])(
- "tracks exactly what trackCredits would when the MCP credits are %s",
- async (_name, remaining) => {
- mcpBalance(remaining);
-
- const hostedMcp = await tracksOf(service =>
- service
- .trackHostedMcpCredits(charge)
- .then(result =>
- expect(result).toEqual({ tracked: true, mcpCredits: 0 }),
- ),
- );
- const regular = await tracksOf(service => service.trackCredits(charge));
-
- expect(hostedMcp).toEqual(regular);
- expect(hostedMcp).toEqual([
- expect.objectContaining({ featureId: "CREDITS", value: 5 }),
- ]);
- },
- );
-
- it("reports an untracked MCP part as not paid by MCP credits", async () => {
- mcpBalance(3);
- mockTrack.mockImplementation(async ({ featureId }) => {
- if (featureId === MCP_CREDITS_FEATURE_ID) throw new Error("track failed");
+ await service.refundCredits({
+ ...charge,
+ featureId: MCP_CREDITS_FEATURE_ID,
});
-
- expect(await new AutumnService().trackHostedMcpCredits(charge)).toEqual({
- tracked: false,
- mcpCredits: 0,
+ await service.refundCredits(charge);
+
+ const [[mcp], [search]] = mockTrack.mock.calls;
+ expect(mcp).toMatchObject({ featureId: MCP_CREDITS_FEATURE_ID, value: -5 });
+ expect(mcp.entityId).toBeUndefined();
+ expect(search).toMatchObject({
+ featureId: "SEARCH_CREDITS",
+ entityId: "team-1",
+ value: -5,
});
});
-});
-
-describe("refundHostedMcpCredits", () => {
- const refund = { ...charge, idempotencyKey: "fc:refund:scrape:job-1" };
- it("refunds each part of a split charge to the balance it was tracked on", async () => {
+ it("checks and holds MCP credits on the customer and other features on the team", async () => {
const service = new AutumnService();
- const refundCredits = vi.spyOn(service, "refundCredits");
-
- expect(
- await service.refundHostedMcpCredits({ ...refund, mcpCredits: 3 }),
- ).toBe(true);
- expect(refundCredits).toHaveBeenCalledTimes(2);
- expect(refundCredits).toHaveBeenCalledWith({
- ...refund,
- value: 3,
+ await service.checkCredits({
+ ...charge,
featureId: MCP_CREDITS_FEATURE_ID,
- idempotencyKey: "fc:refund:scrape:job-1:mcp",
});
- expect(refundCredits).toHaveBeenCalledWith({ ...refund, value: 2 });
- });
-
- it("refunds a charge MCP credits paid in full to them only", async () => {
- const service = new AutumnService();
- const refundCredits = vi.spyOn(service, "refundCredits");
-
- await service.refundHostedMcpCredits({ ...refund, mcpCredits: 5 });
-
- expect(refundCredits).toHaveBeenCalledExactlyOnceWith(
- expect.objectContaining({ value: 5, featureId: MCP_CREDITS_FEATURE_ID }),
- );
- });
-
- it("refunds exactly as refundCredits would when MCP credits paid nothing", async () => {
- const service = new AutumnService();
- const refundCredits = vi.spyOn(service, "refundCredits");
-
- await service.refundHostedMcpCredits({ ...refund, mcpCredits: 0 });
+ await service.lockCredits({ ...charge, featureId: MCP_CREDITS_FEATURE_ID });
+ await service.checkCredits(charge);
- expect(refundCredits).toHaveBeenCalledExactlyOnceWith(refund);
- });
-});
-
-describe("hosted MCP charge retries", () => {
- it("splits a retried charge as the first attempt did, after it used the MCP credits up", async () => {
- mcpBalance(5);
- const first = await tracksOf(service =>
- service.trackHostedMcpCredits(charge),
- );
- mcpBalance(0);
- const retry = await tracksOf(service =>
- service
- .trackHostedMcpCredits(charge)
- .then(result =>
- expect(result).toEqual({ tracked: true, mcpCredits: 5 }),
- ),
- );
-
- expect(retry).toEqual(first);
- expect(retry).toEqual([
- expect.objectContaining({ featureId: MCP_CREDITS_FEATURE_ID, value: 5 }),
- ]);
- });
-
- it("records the allocation once, for 7 days, under the charge's key", async () => {
- mcpBalance(3);
-
- await new AutumnService().trackHostedMcpCredits(charge);
-
- expect(redis.set).toHaveBeenCalledExactlyOnceWith(
- "fc:mcp-credits:allocation:fc:track:scrape:job-1",
- 3,
- "EX",
- 7 * 24 * 60 * 60,
- "NX",
- );
+ const [[check], [hold], [search]] = mockCheck.mock.calls;
+ expect(check.entityId).toBeUndefined();
+ expect(hold).toMatchObject({
+ featureId: MCP_CREDITS_FEATURE_ID,
+ lock: expect.objectContaining({ enabled: true }),
+ });
+ expect(hold.entityId).toBeUndefined();
+ expect(search).toMatchObject({
+ featureId: "SEARCH_CREDITS",
+ entityId: "team-1",
+ });
});
- it("works the split out afresh for a charge without a key", async () => {
- mcpBalance(3);
+ describe("through firebill", () => {
+ const mockFetch = vi.fn<(url: any, init?: any) => Promise<Response>>();
+ const bodies = (path: string) =>
+ mockFetch.mock.calls
+ .filter(([url]) => String(url).endsWith(path))
+ .map(([, init]) => JSON.parse(init.body));
- await new AutumnService().trackHostedMcpCredits({
- ...charge,
- idempotencyKey: undefined,
+ beforeEach(() => {
+ state.firebill = true;
+ mockFetch.mockReset();
+ vi.stubGlobal("fetch", mockFetch);
});
- expect(redis.get).not.toHaveBeenCalled();
- expect(redis.set).not.toHaveBeenCalled();
- });
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ });
- it("works the split out afresh when Redis hangs", async () => {
- vi.useFakeTimers();
- try {
- mcpBalance(3);
- redis.get.mockReturnValueOnce(new Promise<string | null>(() => {}));
+ it("tracks an MCP credits charge with an empty entity", async () => {
+ mockFetch.mockImplementation(
+ async () => new Response(JSON.stringify({ success: true })),
+ );
- const result = new AutumnService().trackHostedMcpCredits(charge);
- await vi.advanceTimersByTimeAsync(1000);
+ expect(
+ await new AutumnService().trackCredits({
+ ...charge,
+ featureId: MCP_CREDITS_FEATURE_ID,
+ }),
+ ).toBe(true);
+
+ expect(mockTrack).not.toHaveBeenCalled();
+ expect(bodies("/v1/track")).toEqual([
+ expect.objectContaining({
+ customer_id: "org-1",
+ entity_id: "",
+ feature_id: MCP_CREDITS_FEATURE_ID,
+ value: 5,
+ }),
+ ]);
+ });
- expect(await result).toEqual({ tracked: true, mcpCredits: 3 });
- } finally {
- vi.useRealTimers();
- }
- });
+ it("checks and holds MCP credits with an empty entity", async () => {
+ mockFetch.mockImplementation(
+ async () =>
+ new Response(
+ JSON.stringify({
+ success: true,
+ allowed: true,
+ remaining: 100,
+ lock_id: "lock-1",
+ }),
+ ),
+ );
+ const service = new AutumnService();
- it("works the split out afresh when Redis can't answer", async () => {
- mcpBalance(3);
- redis.get.mockRejectedValueOnce(new Error("redis down"));
+ await service.checkCredits({
+ ...charge,
+ featureId: MCP_CREDITS_FEATURE_ID,
+ });
+ await service.lockCredits({
+ ...charge,
+ featureId: MCP_CREDITS_FEATURE_ID,
+ lockId: "lock-1",
+ });
- expect(await new AutumnService().trackHostedMcpCredits(charge)).toEqual({
- tracked: true,
- mcpCredits: 3,
+ expect(mockCheck).not.toHaveBeenCalled();
+ expect(bodies("/v1/check")).toEqual([
+ expect.objectContaining({
+ entity_id: "",
+ feature_id: MCP_CREDITS_FEATURE_ID,
+ }),
+ ]);
+ expect(bodies("/v1/lock")).toEqual([
+ expect.objectContaining({
+ entity_id: "",
+ feature_id: MCP_CREDITS_FEATURE_ID,
+ }),
+ ]);
});
});
});
diff --git a/apps/api/src/services/autumn/autumn.service.ts b/apps/api/src/services/autumn/autumn.service.ts
index 2260a60a1..a6da2816a 100644
--- a/apps/api/src/services/autumn/autumn.service.ts
+++ b/apps/api/src/services/autumn/autumn.service.ts
@@ -19,7 +19,6 @@ import {
autumnEntityCreatedInlineTotal,
billingRouteTotal,
} from "./metrics";
-import { getRedisConnection } from "../queue-service";
import type {
CreateEntityParams,
CreateEntityResult,
@@ -47,6 +46,20 @@ export const MCP_CREDITS_FEATURE_ID = config.MCP_CREDITS_FEATURE_ID;
const CONCURRENCY_FEATURE_ID = "CONCURRENCY";
const RATE_LIMIT_FEATURE_ID = "rate_limits";
+/**
+ * The Autumn entity a charge on `featureId` is made against. MCP credits are
+ * granted to the customer rather than to a team, so their charges, holds and
+ * refunds carry no entity: with the team's entity, Autumn spends that team's
+ * own balances first, including a team-scoped CREDITS grant, and leaves the
+ * customer's MCP credits untouched.
+ */
+function billingEntityId(
+ featureId: string,
+ teamId: string,
+): string | undefined {
+ return featureId === MCP_CREDITS_FEATURE_ID ? undefined : teamId;
+}
+
/**
* Coerces a raw Autumn balance figure into a usable non-negative number, or
* null when it's absent or not a sane finite value. These balances feed
@@ -536,7 +549,7 @@ export class AutumnService {
if (routed) {
const result = await firebillCheck({
customerId,
- entityId: teamId,
+ entityId: billingEntityId(featureId, teamId) ?? "",
featureId,
value,
properties,
@@ -551,7 +564,7 @@ export class AutumnService {
const { allowed, balance } = await autumnClient.check({
customerId,
- entityId: teamId,
+ entityId: billingEntityId(featureId, teamId),
featureId,
requiredBalance: value,
properties,
@@ -586,164 +599,15 @@ export class AutumnService {
}
/**
- * How much of a hosted MCP charge of `value` the team's MCP credits
- * (MCP_CREDITS_FEATURE_ID) can pay: all of it, or what is left when that is
- * less. 0 for a team without the feature or with none left, for a
- * partner-provisioned team (its usage is split with the partner on the
- * regular features), and when Autumn can't answer.
- *
- * Read from the feature's own balance on the customer, never from a check:
- * MCP credits draw from the CREDITS credit system once their own balance
- * runs short, so a check on them answers with the CREDITS pool, and the part
- * that pool pays must be charged, gated and recorded as regular usage.
- */
- async mcpCreditsAvailable({
- teamId,
- orgId,
- value,
- }: Pick<
- TrackCreditsParams,
- "teamId" | "orgId" | "value" | "properties"
- >): Promise<number> {
- if (!(value > 0) || !autumnClient || this.isPreviewTeam(teamId)) return 0;
- if (await this.isGatewayProvisioned(teamId)) return 0;
- try {
- const customer = await autumnClient.customers.get({ customerId: orgId });
- const balance = customer.balances?.[MCP_CREDITS_FEATURE_ID];
- if (!balance) return 0;
- if (balance.unlimited) return value;
- if (!Number.isFinite(balance.remaining)) return 0;
- return Math.max(0, Math.min(value, Math.floor(balance.remaining)));
- } catch (error) {
- logger.warn("Failed to read the MCP credits balance", {
- teamId,
- orgId,
- error,
- });
- return 0;
- }
- }
-
- /**
- * The MCP credits part of a keyed hosted MCP charge. The first attempt
- * records it, so a retry under the same key (a stalled job re-running its
- * charge) splits the charge the same way and each part dedupes against its
- * first track, even after that first track used the MCP credits up. Without
- * a key, or when Redis doesn't answer within a second, it is worked out
- * afresh. The shared connection queues commands while Redis is down rather
- * than failing them, hence the bound.
- */
- private async hostedMcpAllocation(params: TrackCreditsParams) {
- if (!params.idempotencyKey) return this.mcpCreditsAvailable(params);
- const key = `fc:mcp-credits:allocation:${params.idempotencyKey}`;
- const redis = getRedisConnection();
- const bounded = async <T>(command: Promise<T>): Promise<T> => {
- let timer: NodeJS.Timeout | undefined;
- try {
- return await Promise.race([
- command,
- new Promise<never>((_, reject) => {
- timer = setTimeout(
- () => reject(new Error("Redis did not answer in time")),
- 1000,
- );
- }),
- ]);
- } finally {
- clearTimeout(timer);
- }
- };
- try {
- const recorded = await bounded(redis.get(key));
- if (recorded !== null) return Number(recorded);
- } catch (error) {
- logger.warn("Failed to read a hosted MCP charge's allocation", {
- key,
- error,
- });
- return this.mcpCreditsAvailable(params);
- }
- const mcpCredits = await this.mcpCreditsAvailable(params);
- try {
- if (
- (await bounded(
- redis.set(key, mcpCredits, "EX", 7 * 24 * 60 * 60, "NX"),
- )) === null
- ) {
- const recorded = await bounded(redis.get(key));
- if (recorded !== null) return Number(recorded);
- }
- } catch (error) {
- logger.warn("Failed to record a hosted MCP charge's allocation", {
- key,
- error,
- });
- }
- return mcpCredits;
- }
-
- /**
- * Tracks a hosted MCP charge: the part the team's MCP credits can pay (see
- * mcpCreditsAvailable) on MCP_CREDITS_FEATURE_ID, and the rest on
- * `featureId`, the endpoint's own feature. When the MCP credits pay nothing,
- * the whole charge is tracked exactly as trackCredits would track it.
- * Resolves whether every part was tracked, and the credits tracked on
- * MCP_CREDITS_FEATURE_ID: those were charged even when `tracked` is false,
- * so a caller reversing the charge reverses them on their own.
+ * The feature a hosted MCP request's charge, hold or check is made on, in
+ * place of `featureId`: MCP credits, which Autumn spends first before
+ * falling back to other balances. A partner-provisioned team keeps
+ * `featureId`: its usage is split with the partner on the regular features.
*/
- async trackHostedMcpCredits(
- params: TrackCreditsParams,
- ): Promise<{ tracked: boolean; mcpCredits: number }> {
- const mcpCredits = await this.hostedMcpAllocation(params);
- const rest = params.value - mcpCredits;
- const [mcpTracked, restTracked] = await Promise.all([
- mcpCredits > 0
- ? this.trackCredits({
- ...params,
- value: mcpCredits,
- featureId: MCP_CREDITS_FEATURE_ID,
- // Its own charge event: the rest keeps the caller's key, and the
- // same key would dedupe one part against the other.
- idempotencyKey:
- params.idempotencyKey && `${params.idempotencyKey}:mcp`,
- })
- : true,
- mcpCredits === 0 || rest > 0
- ? this.trackCredits({ ...params, value: rest })
- : true,
- ]);
- return {
- tracked: mcpTracked && restTracked,
- mcpCredits: mcpTracked ? mcpCredits : 0,
- };
- }
-
- /**
- * Reverses a trackHostedMcpCredits charge: `mcpCredits` back to
- * MCP_CREDITS_FEATURE_ID and the rest to `featureId`, each part keyed as it
- * was tracked. With no MCP credits it is exactly refundCredits. Resolves
- * whether every part was refunded.
- */
- async refundHostedMcpCredits({
- mcpCredits,
- ...params
- }: TrackCreditsParams & { mcpCredits: number }): Promise<boolean> {
- const rest = params.value - mcpCredits;
- const [mcpRefunded, restRefunded] = await Promise.all([
- mcpCredits > 0
- ? this.refundCredits({
- ...params,
- value: mcpCredits,
- featureId: MCP_CREDITS_FEATURE_ID,
- idempotencyKey:
- params.idempotencyKey && `${params.idempotencyKey}:mcp`,
- })
- : true,
- mcpCredits === 0 || rest > 0
- ? this.refundCredits({ ...params, value: rest })
- : true,
- ]);
- return mcpRefunded && restRefunded;
+ async hostedMcpFeatureId(teamId: string, featureId: string): Promise<string> {
+ return (await this.isGatewayProvisioned(teamId))
+ ? featureId
+ : MCP_CREDITS_FEATURE_ID;
}
/**
@@ -816,7 +680,7 @@ export class AutumnService {
if (routed) {
const result = await firebillLock({
customerId,
- entityId: teamId,
+ entityId: billingEntityId(featureId, teamId) ?? "",
featureId,
value,
lockId: resolvedLockId,
@@ -855,10 +719,11 @@ export class AutumnService {
return { status: "denied", reason: "gate_unavailable" };
}
+ const entityId = billingEntityId(featureId, teamId);
const { allowed } = await autumnClient
.check({
customerId,
- entityId: teamId,
+ entityId,
featureId,
requiredBalance: value,
properties,
@@ -892,7 +757,7 @@ export class AutumnService {
logger.info("Autumn lockCredits succeeded", {
customerId,
- entityId: teamId,
+ entityId,
featureId,
value,
lockId: resolvedLockId,
@@ -1029,7 +894,7 @@ export class AutumnService {
return await this.track(
{
customerId,
- entityId: teamId,
+ entityId: billingEntityId(featureId, teamId),
featureId,
value,
properties,
@@ -1125,7 +990,7 @@ export class AutumnService {
return await this.track(
{
customerId,
- entityId: teamId,
+ entityId: billingEntityId(featureId, teamId),
featureId,
value: -value,
properties: { ...properties, source: "autumn_refund" },
diff --git a/apps/api/src/services/autumn/firebill.ts b/apps/api/src/services/autumn/firebill.ts
index 06fe07915..0ad4b232e 100644
--- a/apps/api/src/services/autumn/firebill.ts
+++ b/apps/api/src/services/autumn/firebill.ts
@@ -588,7 +588,8 @@ async function firebillAttempt(
// direct-Autumn path below.
body: JSON.stringify({
customer_id: customerId,
- entity_id: entityId,
+ // Required by firebill; empty for a customer-level charge.
+ entity_id: entityId ?? "",
feature_id: featureId,
value: Math.abs(value),
properties,
diff --git a/apps/api/src/services/billing/__tests__/batch_billing.test.ts b/apps/api/src/services/billing/__tests__/batch_billing.test.ts
index d1b41827e..cfe223432 100644
--- a/apps/api/src/services/billing/__tests__/batch_billing.test.ts
+++ b/apps/api/src/services/billing/__tests__/batch_billing.test.ts
@@ -10,7 +10,6 @@ const {
refundCredits,
billTeam7,
getACUCTeam,
- reportExchangeBilling,
} = vi.hoisted(() => {
const logger: any = {
info: vi.fn(),
@@ -25,14 +24,9 @@ const {
refundCredits: vi.fn<(args: any) => Promise<void>>(),
billTeam7: vi.fn<(params: any) => Promise<{ api_key: string }[]>>(),
getACUCTeam: vi.fn<(teamId: string) => Promise<any>>(),
- reportExchangeBilling: vi.fn<(args: any) => Promise<boolean>>(),
};
});
-vi.mock("../../../lib/exchange", () => ({
- reportExchangeBilling,
-}));
-
vi.mock("../../../lib/logger", () => ({
logger,
}));
@@ -45,10 +39,10 @@ vi.mock("../../autumn/autumn.service", () => ({
autumnService: {
trackCredits,
refundCredits,
+ hostedMcpFeatureId: async () => "MCP_CREDITS",
},
featureIdForBillingEndpoint: (endpoint?: string) =>
endpoint === "search" ? "SEARCH_CREDITS" : "CREDITS",
- MCP_CREDITS_FEATURE_ID: "MCP_CREDITS",
}));
vi.mock("../../../db/rpc", () => ({
@@ -127,7 +121,6 @@ vi.mock("../../queue-service", () => ({
import {
processBillingBatch,
- queueBillingOperation,
startBillingBatchProcessing,
} from "../batch_billing";
@@ -409,177 +402,65 @@ describe("processBillingBatch", () => {
});
});
-describe("startBillingBatchProcessing", () => {
- afterEach(() => {
- vi.useRealTimers();
- });
-
- it("logs a Redis error on a tick and keeps processing on the next one", async () => {
- vi.useFakeTimers();
- const loadingError = new Error(
- "LOADING Redis is loading the dataset in memory",
- );
- redis.llen.mockRejectedValueOnce(loadingError);
-
- startBillingBatchProcessing();
-
- await vi.advanceTimersByTimeAsync(15000);
- expect(logger.error).toHaveBeenCalledWith(
- "Error in periodic billing batch processing",
- { error: loadingError },
- );
- expect(redis.set).not.toHaveBeenCalled();
-
- queue = [makeOp()];
- await vi.advanceTimersByTimeAsync(15000);
- expect(billTeam7).toHaveBeenCalledTimes(1);
- expect(queue).toHaveLength(0);
- });
-});
-
-describe("MCP credits on the ledger", () => {
- it("records the part MCP credits paid as its own tagged row", async () => {
- await queueBillingOperation(
- "team-1",
- "org-1",
- 5,
- 123,
- { endpoint: "scrape", hostedMcp: true, mcpCredits: 3 },
- false,
- true,
- );
- expect(queue).toHaveLength(1);
-
- await processBillingBatch();
+describe("hosted MCP charges", () => {
+ const hosted = (overrides: Record<string, unknown> = {}) =>
+ makeOp({
+ credits: 5,
+ billing: { endpoint: "scrape", hostedMcp: true },
+ autumnTrackInRequest: true,
+ ...overrides,
+ });
- expect(billTeam7).toHaveBeenCalledTimes(2);
- expect(billTeam7).toHaveBeenCalledWith(
- expect.objectContaining({ credits: 2, subscription_id: null }),
- );
- expect(billTeam7).toHaveBeenCalledWith(
- expect.objectContaining({ credits: 3, subscription_id: "mcp_credits" }),
- );
- });
+ it("writes a hosted charge as one ordinary ledger row", async () => {
+ queue = [hosted()];
- it("records a charge MCP credits paid in full as one tagged row", async () => {
- await queueBillingOperation(
- "team-1",
- "org-1",
- 5,
- 123,
- { endpoint: "scrape", hostedMcp: true, mcpCredits: 5 },
- false,
- true,
- );
await processBillingBatch();
expect(billTeam7).toHaveBeenCalledExactlyOnceWith(
- expect.objectContaining({ credits: 5, subscription_id: "mcp_credits" }),
+ expect.objectContaining({ credits: 5, subscription_id: null }),
);
});
- it("records a charge MCP credits paid nothing of as today's untagged row", async () => {
- await queueBillingOperation(
- "team-1",
- "org-1",
- 5,
- 123,
- { endpoint: "scrape", hostedMcp: true },
- false,
- true,
- );
- await processBillingBatch();
-
- expect(billTeam7).toHaveBeenCalledExactlyOnceWith({
- team_id: "team-1",
- subscription_id: null,
- credits: 5,
- api_key_id: 123,
- is_extract: false,
- });
- });
-
- it("refunds each part to the balance it was tracked on when the ledger fails", async () => {
- await queueBillingOperation(
- "team-1",
- "org-1",
- 5,
- 123,
- { endpoint: "scrape", hostedMcp: true, mcpCredits: 3 },
- false,
- true,
- );
+ it("refunds a hosted charge and a regular one each to the feature it was charged on", async () => {
+ queue = [hosted(), makeOp({ autumnTrackInRequest: true })];
billTeam7.mockRejectedValue(new Error("db failed"));
await processBillingBatch();
expect(refundCredits).toHaveBeenCalledTimes(2);
expect(refundCredits).toHaveBeenCalledWith(
- expect.objectContaining({ value: 2, featureId: "CREDITS" }),
+ expect.objectContaining({ value: 5, featureId: "MCP_CREDITS" }),
);
expect(refundCredits).toHaveBeenCalledWith(
- expect.objectContaining({ value: 3, featureId: "MCP_CREDITS" }),
+ expect.objectContaining({ value: 10, featureId: "CREDITS" }),
);
});
+});
- it("refunds only the MCP credits part when the rest was not tracked", async () => {
- await queueBillingOperation(
- "team-1",
- "org-1",
- 5,
- 123,
- { endpoint: "scrape", hostedMcp: true, mcpCredits: 3 },
- false,
- false,
- );
- billTeam7.mockRejectedValue(new Error("db failed"));
-
- await processBillingBatch();
-
- expect(refundCredits).toHaveBeenCalledExactlyOnceWith(
- expect.objectContaining({ value: 3, featureId: "MCP_CREDITS" }),
- );
+describe("startBillingBatchProcessing", () => {
+ afterEach(() => {
+ vi.useRealTimers();
});
- it("confirms a split charge's Exchange access once, after both rows commit", async () => {
- await queueBillingOperation(
- "team-1",
- "org-1",
- 5,
- 123,
- { endpoint: "scrape", hostedMcp: true, mcpCredits: 3 },
- false,
- true,
- { accessEventId: "access-1", billingReference: "ref-1" },
+ it("logs a Redis error on a tick and keeps processing on the next one", async () => {
+ vi.useFakeTimers();
+ const loadingError = new Error(
+ "LOADING Redis is loading the dataset in memory",
);
- await processBillingBatch();
+ redis.llen.mockRejectedValueOnce(loadingError);
- expect(reportExchangeBilling).toHaveBeenCalledExactlyOnceWith({
- accessEventId: "access-1",
- status: "confirmed",
- billingReference: "ref-1",
- });
- });
+ startBillingBatchProcessing();
- it("leaves a split charge's Exchange access pending when one row fails", async () => {
- await queueBillingOperation(
- "team-1",
- "org-1",
- 5,
- 123,
- { endpoint: "scrape", hostedMcp: true, mcpCredits: 3 },
- false,
- true,
- { accessEventId: "access-1" },
+ await vi.advanceTimersByTimeAsync(15000);
+ expect(logger.error).toHaveBeenCalledWith(
+ "Error in periodic billing batch processing",
+ { error: loadingError },
);
- billTeam7.mockImplementation(async ({ subscription_id }) => {
- if (subscription_id === "mcp_credits") throw new Error("db failed");
- return [];
- });
-
- await processBillingBatch();
+ expect(redis.set).not.toHaveBeenCalled();
- expect(billTeam7).toHaveBeenCalledTimes(2);
- expect(reportExchangeBilling).not.toHaveBeenCalled();
+ queue = [makeOp()];
+ await vi.advanceTimersByTimeAsync(15000);
+ expect(billTeam7).toHaveBeenCalledTimes(1);
+ expect(queue).toHaveLength(0);
});
});
diff --git a/apps/api/src/services/billing/__tests__/credit_billing.test.ts b/apps/api/src/services/billing/__tests__/credit_billing.test.ts
index ddbd492f0..b6ed0d57e 100644
--- a/apps/api/src/services/billing/__tests__/credit_billing.test.ts
+++ b/apps/api/src/services/billing/__tests__/credit_billing.test.ts
@@ -7,8 +7,6 @@ const {
trackCredits,
refundCredits,
isRoutedThroughFirebill,
- trackHostedMcpCredits,
- refundHostedMcpCredits,
} = vi.hoisted(() => ({
withAuth: vi.fn((fn: any) => fn),
queueBillingOperation: vi.fn<(args: any[]) => Promise<any>>(),
@@ -16,16 +14,6 @@ const {
refundCredits: vi.fn<(args: any) => Promise<void>>(),
isRoutedThroughFirebill:
vi.fn<(teamId: string, orgId: string) => Promise<boolean>>(),
- trackHostedMcpCredits:
- vi.fn<(args: any) => Promise<{ tracked: boolean; mcpCredits: number }>>(),
- refundHostedMcpCredits: vi.fn<(args: any) => Promise<boolean>>(),
-}));
-const { recordRequestCredits } = vi.hoisted(() => ({
- recordRequestCredits: vi.fn<(args: any) => Promise<boolean>>(),
-}));
-
-vi.mock("../../../lib/request-credits-store", () => ({
- recordRequestCredits,
}));
vi.mock("../../../lib/withAuth", () => ({
@@ -41,8 +29,7 @@ vi.mock("../../autumn/autumn.service", () => ({
trackCredits,
refundCredits,
isRoutedThroughFirebill,
- trackHostedMcpCredits,
- refundHostedMcpCredits,
+ hostedMcpFeatureId: async () => "MCP_CREDITS",
},
featureIdForBillingEndpoint: (endpoint?: string) =>
endpoint === "search" ? "SEARCH_CREDITS" : "CREDITS",
@@ -71,8 +58,6 @@ beforeEach(() => {
trackCredits.mockResolvedValue(true);
refundCredits.mockResolvedValue(undefined);
isRoutedThroughFirebill.mockResolvedValue(false);
- trackHostedMcpCredits.mockResolvedValue({ tracked: true, mcpCredits: 0 });
- recordRequestCredits.mockResolvedValue(true);
});
describe("billTeam", () => {
@@ -248,7 +233,7 @@ describe("billTeam", () => {
});
});
-describe("billTeam MCP credits", () => {
+describe("billTeam for a hosted MCP call", () => {
const hostedMcpScrape = {
endpoint: "scrape" as const,
jobId: "job-1",
@@ -256,43 +241,16 @@ describe("billTeam MCP credits", () => {
hostedMcp: true,
};
- it("charges a hosted MCP call through the MCP credits split", async () => {
- trackHostedMcpCredits.mockResolvedValue({ tracked: true, mcpCredits: 2 });
-
- await billTeam("team-1", "org-1", 3, 123, hostedMcpScrape);
-
- expect(trackHostedMcpCredits).toHaveBeenCalledExactlyOnceWith({
- teamId: "team-1",
- orgId: "org-1",
- value: 3,
- properties: {
- source: "billTeam",
- endpoint: "scrape",
- jobId: "job-1",
- apiKeyId: 123,
- },
- featureId: "CREDITS",
- idempotencyKey: "fc:track:scrape:job-1",
- externalRequestId: undefined,
- });
- expect(trackCredits).not.toHaveBeenCalled();
- // The ledger records the MCP-paid part apart from regular credits.
- expect(queueBillingOperation).toHaveBeenCalledWith([
- "team-1",
- "org-1",
- 3,
- 123,
- { ...hostedMcpScrape, mcpCredits: 2 },
- false,
- true,
- ]);
- });
-
- it("queues an unchanged ledger operation when MCP credits paid nothing", async () => {
- trackHostedMcpCredits.mockResolvedValue({ tracked: true, mcpCredits: 0 });
-
+ it("charges the whole call on MCP credits and queues an ordinary ledger operation", async () => {
await billTeam("team-1", "org-1", 3, 123, hostedMcpScrape);
+ expect(trackCredits).toHaveBeenCalledExactlyOnceWith(
+ expect.objectContaining({
+ value: 3,
+ featureId: "MCP_CREDITS",
+ idempotencyKey: "fc:track:scrape:job-1",
+ }),
+ );
expect(queueBillingOperation).toHaveBeenCalledWith([
"team-1",
"org-1",
@@ -304,135 +262,17 @@ describe("billTeam MCP credits", () => {
]);
});
- it("refunds a failed enqueue to the balances the charge was tracked on", async () => {
- trackHostedMcpCredits.mockResolvedValue({ tracked: true, mcpCredits: 2 });
+ it("refunds a failed enqueue whole to MCP credits", async () => {
queueBillingOperation.mockResolvedValueOnce({ success: false });
await billTeam("team-1", "org-1", 3, 123, hostedMcpScrape);
- expect(refundHostedMcpCredits).toHaveBeenCalledExactlyOnceWith({
- teamId: "team-1",
- orgId: "org-1",
- value: 3,
- mcpCredits: 2,
- properties: {
- source: "billTeam",
- endpoint: "scrape",
- jobId: "job-1",
- apiKeyId: 123,
- },
- featureId: "CREDITS",
- idempotencyKey: "fc:refund:scrape:job-1",
- externalRequestId: undefined,
- });
- expect(refundCredits).not.toHaveBeenCalled();
- });
-
- it("refunds the MCP credits part of a failed enqueue even when the rest was not tracked", async () => {
- trackHostedMcpCredits.mockResolvedValue({ tracked: false, mcpCredits: 2 });
- queueBillingOperation.mockResolvedValueOnce({ success: false });
-
- await billTeam("team-1", "org-1", 3, 123, hostedMcpScrape);
-
- expect(queueBillingOperation).toHaveBeenCalledWith(
- expect.arrayContaining([false]),
- );
- expect(refundHostedMcpCredits).toHaveBeenCalledExactlyOnceWith(
- expect.objectContaining({ value: 2, mcpCredits: 2 }),
- );
- expect(refundCredits).not.toHaveBeenCalled();
- });
-
- it("refunds nothing for a failed enqueue when nothing was tracked", async () => {
- trackHostedMcpCredits.mockResolvedValue({ tracked: false, mcpCredits: 0 });
- queueBillingOperation.mockResolvedValueOnce({ success: false });
-
- await billTeam("team-1", "org-1", 3, 123, hostedMcpScrape);
-
- expect(refundHostedMcpCredits).not.toHaveBeenCalled();
- expect(refundCredits).not.toHaveBeenCalled();
- });
-
- it("refunds a failed enqueue MCP credits paid nothing of as today", async () => {
- trackHostedMcpCredits.mockResolvedValue({ tracked: true, mcpCredits: 0 });
- queueBillingOperation.mockResolvedValueOnce({ success: false });
-
- await billTeam("team-1", "org-1", 3, 123, hostedMcpScrape);
-
- expect(refundCredits).toHaveBeenCalledWith(
- expect.objectContaining({ value: 3, featureId: "CREDITS" }),
- );
- expect(refundHostedMcpCredits).not.toHaveBeenCalled();
- });
-
- it("never goes through the MCP credits split for a call the hosted MCP did not make", async () => {
- await billTeam("team-1", "org-1", 3, 123, {
- endpoint: "scrape",
- jobId: "job-1",
- });
-
- expect(trackHostedMcpCredits).not.toHaveBeenCalled();
- expect(trackCredits).toHaveBeenCalledWith(
- expect.objectContaining({ featureId: "CREDITS" }),
+ expect(refundCredits).toHaveBeenCalledExactlyOnceWith(
+ expect.objectContaining({
+ value: 3,
+ featureId: "MCP_CREDITS",
+ idempotencyKey: "fc:refund:scrape:job-1",
+ }),
);
});
-
- it("never charges Autumn for a team without an org", async () => {
- await billTeam("preview_abc", null, 3, null, hostedMcpScrape);
-
- expect(trackHostedMcpCredits).not.toHaveBeenCalled();
- expect(trackCredits).not.toHaveBeenCalled();
- });
-});
-
-describe("billTeam MCP credits on an agent run", () => {
- const agentCall = {
- endpoint: "search" as const,
- jobId: "job-1",
- chargeId: "charge-1",
- hostedMcp: true,
- agentRequestId: "agent-run",
- };
-
- it("adds the MCP credits part to the run's MCP tally under the charge", async () => {
- trackHostedMcpCredits.mockResolvedValue({ tracked: true, mcpCredits: 2 });
-
- await billTeam("team-1", "org-1", 3, 123, agentCall);
-
- expect(recordRequestCredits).toHaveBeenCalledExactlyOnceWith({
- requestId: "agent-run:mcp",
- jobId: "charge-1",
- credits: 2,
- });
- });
-
- it("tallies nothing when MCP credits paid nothing or the charge was refunded", async () => {
- await billTeam("team-1", "org-1", 3, 123, agentCall);
-
- trackHostedMcpCredits.mockResolvedValue({ tracked: true, mcpCredits: 2 });
- queueBillingOperation.mockResolvedValueOnce({ success: false });
- await billTeam("team-1", "org-1", 3, 123, agentCall);
-
- expect(recordRequestCredits).not.toHaveBeenCalled();
- });
-
- it("tallies nothing for a call outside an agent run", async () => {
- trackHostedMcpCredits.mockResolvedValue({ tracked: true, mcpCredits: 2 });
-
- await billTeam("team-1", "org-1", 3, 123, {
- ...agentCall,
- agentRequestId: undefined,
- });
-
- expect(recordRequestCredits).not.toHaveBeenCalled();
- });
-
- it("still bills when the tally cannot be written", async () => {
- trackHostedMcpCredits.mockResolvedValue({ tracked: true, mcpCredits: 2 });
- recordRequestCredits.mockRejectedValueOnce(new Error("store down"));
-
- await expect(
- billTeam("team-1", "org-1", 3, 123, agentCall),
- ).resolves.toMatchObject({ success: true });
- });
});
diff --git a/apps/api/src/services/billing/agent-mcp-credits.ts b/apps/api/src/services/billing/agent-mcp-credits.ts
deleted file mode 100644
index 5fa743ab3..000000000
--- a/apps/api/src/services/billing/agent-mcp-credits.ts
+++ /dev/null
@@ -1,36 +0,0 @@
-import { logger as rootLogger } from "../../lib/logger";
-import { recordRequestCredits } from "../../lib/request-credits-store";
-import type { BillingMetadata } from "./types";
-
-/**
- * The request id a hosted MCP agent run tallies its calls' MCP credits under,
- * next to the run's own call-credits tally (see agentController).
- */
-export function agentMcpCreditsRequestId(agentRequestId: string): string {
- return `${agentRequestId}:mcp`;
-}
-
-/**
- * Adds the MCP credits part of an agent call's charge to its run's tally.
- * `chargeKey` names the charge, so a retried charge counts once. Nothing is
- * recorded for a run that started no tally (any run not from the hosted MCP).
- */
-export async function tallyAgentMcpCredits(
- billing: BillingMetadata,
- chargeKey: string,
- mcpCredits: number,
-): Promise<void> {
- if (!billing.agentRequestId || !(mcpCredits > 0)) return;
- await recordRequestCredits({
- requestId: agentMcpCreditsRequestId(billing.agentRequestId),
- jobId: chargeKey,
- credits: mcpCredits,
- }).catch(error =>
- rootLogger.error("Failed to tally an agent call's MCP credits", {
- agentRequestId: billing.agentRequestId,
- chargeKey,
- mcpCredits,
- error,
- }),
- );
-}
diff --git a/apps/api/src/services/billing/batch_billing.ts b/apps/api/src/services/billing/batch_billing.ts
index 4e8d57c8d..e50a6f35e 100644
--- a/apps/api/src/services/billing/batch_billing.ts
+++ b/apps/api/src/services/billing/batch_billing.ts
@@ -5,7 +5,6 @@ import { withAuth } from "../../lib/withAuth";
import {
autumnService,
featureIdForBillingEndpoint,
- MCP_CREDITS_FEATURE_ID,
} from "../autumn/autumn.service";
import {
resolveBillingMetadata,
@@ -84,9 +83,6 @@ const BATCH_LOCK_KEY = "billing_batch_lock";
const BATCH_SIZE = 5000; // Batch size for processing
const BATCH_TIMEOUT = 15000; // 15 seconds processing interval
const LOCK_TIMEOUT = 30000; // 30 seconds lock timeout
-// The ledger's tag for usage the team's MCP credits paid. bill_team_7 stores it
-// in credit_usage.subscription_id, which it passes through as a tag.
-const MCP_CREDITS_LEDGER_TAG = "mcp_credits";
// Define interfaces for billing operations
interface BillingOperation {
@@ -119,7 +115,6 @@ interface GroupedBillingOperation {
billing: BillingMetadata;
is_extract: boolean;
api_key_id: number | null;
- ledger_tag?: string;
operations: BillingOperation[];
}
@@ -191,11 +186,12 @@ async function refundRequestTrackedCredits(
...toAutumnBillingProperties(group.billing),
apiKeyId: group.api_key_id,
},
- // The part MCP credits paid goes back to them.
- featureId:
- group.ledger_tag === MCP_CREDITS_LEDGER_TAG
- ? MCP_CREDITS_FEATURE_ID
- : featureIdForBillingEndpoint(group.billing.endpoint),
+ featureId: group.billing.hostedMcp
+ ? await autumnService.hostedMcpFeatureId(
+ group.team_id,
+ featureIdForBillingEndpoint(group.billing.endpoint),
+ )
+ : featureIdForBillingEndpoint(group.billing.endpoint),
});
} catch (error) {
logger.warn("Failed to refund Autumn request-tracked credits", {
@@ -220,11 +216,8 @@ export async function processBillingBatch() {
}
// Exchange operations whose debit committed this run; their ledger
- // confirmations are delivered after the lock is released. An operation
- // MCP credits paid part of is split across ledger rows, so each access
- // counts the rows it spans.
+ // confirmations are delivered after the lock is released.
const committedExchangeOps: BillingOperation[] = [];
- const exchangeRowCounts = new Map<string, number>();
try {
// Get all operations from Redis list
@@ -299,51 +292,23 @@ export async function processBillingBatch() {
} else {
orgId = op.org_id;
}
- // The part the team's MCP credits paid is its own ledger row, tagged so
- // MCP-paid usage is counted apart from regular credits used.
- const mcpCredits = Math.min(billing.mcpCredits ?? 0, op.credits);
- const parts = [
- ...(mcpCredits === 0 || mcpCredits < op.credits
- ? [{ credits: op.credits - mcpCredits, ledger_tag: undefined }]
- : []),
- ...(mcpCredits > 0
- ? [{ credits: mcpCredits, ledger_tag: MCP_CREDITS_LEDGER_TAG }]
- : []),
- ];
- if (op.exchange_access_event_id !== undefined) {
- exchangeRowCounts.set(
- op.exchange_access_event_id,
- (exchangeRowCounts.get(op.exchange_access_event_id) ?? 0) +
- parts.length,
- );
- }
- for (const { credits, ledger_tag } of parts) {
- const key = `${op.team_id}:${orgId}:${billing.endpoint}:${ledger_tag ?? ""}:${op.is_extract}:${op.api_key_id}`;
-
- if (!groupedOperations.has(key)) {
- groupedOperations.set(key, {
- team_id: op.team_id,
- org_id: orgId,
- total_credits: 0,
- billing,
- is_extract: op.is_extract,
- api_key_id: op.api_key_id,
- ledger_tag,
- operations: [],
- });
- }
-
- const group = groupedOperations.get(key)!;
- group.total_credits += credits;
- // MCP credits are only recorded once their track succeeded, so their
- // row is refundable even when the rest of the charge was not tracked.
- group.operations.push({
- ...op,
- credits,
- autumnTrackInRequest:
- ledger_tag === MCP_CREDITS_LEDGER_TAG || op.autumnTrackInRequest,
+ const key = `${op.team_id}:${orgId}:${billing.endpoint}:${billing.hostedMcp === true}:${op.is_extract}:${op.api_key_id}`;
+
+ if (!groupedOperations.has(key)) {
+ groupedOperations.set(key, {
+ team_id: op.team_id,
+ org_id: orgId,
+ total_credits: 0,
+ billing,
+ is_extract: op.is_extract,
+ api_key_id: op.api_key_id,
+ operations: [],
});
}
+
+ const group = groupedOperations.get(key)!;
+ group.total_credits += op.credits;
+ group.operations.push(op);
}
if (unresolvedOperations.length > 0) {
@@ -390,7 +355,6 @@ export async function processBillingBatch() {
group.api_key_id,
logger,
group.is_extract,
- group.ledger_tag ?? null,
);
if (!billingResult.success) {
@@ -442,23 +406,7 @@ export async function processBillingBatch() {
await releaseLock();
}
- // An access is confirmed once, and only after every row it spans committed.
- const committedRows = new Map<string, BillingOperation[]>();
- for (const op of committedExchangeOps) {
- const accessEventId = op.exchange_access_event_id!;
- committedRows.set(accessEventId, [
- ...(committedRows.get(accessEventId) ?? []),
- op,
- ]);
- }
- await confirmExchangeOutcomes(
- [...committedRows]
- .filter(
- ([accessEventId, rows]) =>
- rows.length === exchangeRowCounts.get(accessEventId),
- )
- .map(([, rows]) => rows[0]),
- );
+ await confirmExchangeOutcomes(committedExchangeOps);
}
// Start periodic batch processing
@@ -572,7 +520,6 @@ async function supaBillTeam(
api_key_id: number | null,
__logger?: any,
is_extract: boolean = false,
- subscription_id: string | null = null,
) {
const _logger = (__logger ?? logger).child({
module: "credit_billing",
@@ -592,7 +539,7 @@ async function supaBillTeam(
try {
data = await billTeam7({
team_id,
- subscription_id,
+ subscription_id: null,
credits,
api_key_id: api_key_id ?? null,
is_extract,
diff --git a/apps/api/src/services/billing/credit_billing.ts b/apps/api/src/services/billing/credit_billing.ts
index c20be4271..28d54536d 100644
--- a/apps/api/src/services/billing/credit_billing.ts
+++ b/apps/api/src/services/billing/credit_billing.ts
@@ -6,8 +6,6 @@ import {
} from "../autumn/autumn.service";
import { toAutumnBillingProperties, type BillingMetadata } from "./types";
import type { Logger } from "winston";
-import { randomUUID } from "node:crypto";
-import { tallyAgentMcpCredits } from "./agent-mcp-credits";
/**
* `org_id` is the team's Autumn customer. It is nullable here and nowhere
@@ -39,14 +37,18 @@ export async function billTeam(
...toAutumnBillingProperties(billing),
apiKeyId: api_key_id,
};
- const featureId = featureIdForBillingEndpoint(billing.endpoint);
+ const featureId = billing.hostedMcp
+ ? await autumnService.hostedMcpFeatureId(
+ team_id,
+ featureIdForBillingEndpoint(billing.endpoint),
+ )
+ : featureIdForBillingEndpoint(billing.endpoint);
let trackedInRequest = false;
- let mcpCredits = 0;
if (org_id !== null) {
// Stable per-charge key (firebill route only): a caller retry or re-run
// job with the same chargeId dedupes instead of double-billing.
- const charge = {
+ trackedInRequest = await autumnService.trackCredits({
teamId: team_id,
orgId: org_id,
value: credits,
@@ -56,14 +58,7 @@ export async function billTeam(
? `fc:track:${billing.endpoint}:${billing.chargeId}`
: undefined,
externalRequestId: billing.externalRequestId ?? undefined,
- };
- // A hosted MCP charge spends the team's MCP credits first.
- if (billing.hostedMcp) {
- ({ tracked: trackedInRequest, mcpCredits } =
- await autumnService.trackHostedMcpCredits(charge));
- } else {
- trackedInRequest = await autumnService.trackCredits(charge);
- }
+ });
} else if (team_id !== "preview" && !team_id.startsWith("preview_")) {
// Preview teams are never tracked anyway; a real team arriving without
// an org is not expected, and its usage is about to go unmetered.
@@ -78,20 +73,13 @@ export async function billTeam(
org_id,
credits,
api_key_id,
- mcpCredits > 0 ? { ...billing, mcpCredits } : billing,
+ billing,
false,
trackedInRequest,
);
// A track only happens with an org in hand; named again so the type says so.
- // A charge the MCP credits paid part of is refundable for that part even
- // when the rest was not tracked.
- let refunded = false;
- if (
- !result.success &&
- (trackedInRequest || mcpCredits > 0) &&
- org_id !== null
- ) {
+ if (!result.success && trackedInRequest && org_id !== null) {
if (await autumnService.isRoutedThroughFirebill(team_id, org_id)) {
// No compensating refund on the firebill route: the tracked charge
// is durable and correct, and a refund here poisons a retried
@@ -103,12 +91,10 @@ export async function billTeam(
{ team_id, credits, billing },
);
} else {
- const refund = {
+ await autumnService.refundCredits({
teamId: team_id,
orgId: org_id,
- // Only what was charged: the rest when it was tracked, and the
- // MCP credits part (refunded on its own below).
- value: (trackedInRequest ? credits - mcpCredits : 0) + mcpCredits,
+ value: credits,
properties: autumnProperties,
featureId,
// Distinct from the track key: the refund is its own charge event.
@@ -116,23 +102,10 @@ export async function billTeam(
? `fc:refund:${billing.endpoint}:${billing.chargeId}`
: undefined,
externalRequestId: billing.externalRequestId ?? undefined,
- };
- // A split charge refunds each part to the balance it was tracked on.
- await (mcpCredits > 0
- ? autumnService.refundHostedMcpCredits({ ...refund, mcpCredits })
- : autumnService.refundCredits(refund));
- refunded = true;
+ });
}
}
- if (!refunded) {
- await tallyAgentMcpCredits(
- billing,
- billing.chargeId ?? randomUUID(),
- mcpCredits,
- );
- }
-
return result;
},
{ success: true, message: "No DB, bypassed." },
diff --git a/apps/api/src/services/billing/types.ts b/apps/api/src/services/billing/types.ts
index 493da4bae..bad44dc3b 100644
--- a/apps/api/src/services/billing/types.ts
+++ b/apps/api/src/services/billing/types.ts
@@ -41,24 +41,11 @@ export type BillingMetadata = {
externalRequestId?: string | null;
/**
* The hosted MCP made the request, as verified at authentication (see
- * `AuthObject.hostedMcp`). Its charges spend the team's MCP credits first
- * (see AutumnService.trackHostedMcpCredits). Rides the job payload like the
- * rest of this metadata, so a crawl's pages carry it too.
+ * `AuthObject.hostedMcp`). Its charges are made on MCP credits (see
+ * AutumnService.hostedMcpFeatureId). Rides the job payload like the rest of
+ * this metadata, so a crawl's pages carry it too.
*/
hostedMcp?: boolean;
- /**
- * The agent run this charge belongs to, when the agent made the call. The
- * MCP credits part of a hosted MCP run's charge is tallied under it (see
- * tallyAgentMcpCredits), so a failed run can give that part back to MCP
- * credits rather than to regular credits.
- */
- agentRequestId?: string;
- /**
- * Credits of this charge the team's MCP credits paid, set by the charge that
- * tracks them to Autumn (billTeam or billScrapeJob). The ledger records them as their own row, tagged apart from
- * regular credits used (see queueBillingOperation).
- */
- mcpCredits?: number;
};
export function resolveBillingMetadata({
diff --git a/apps/api/src/services/worker/scrape-worker.ts b/apps/api/src/services/worker/scrape-worker.ts
index a0885dafe..ede5442e9 100644
--- a/apps/api/src/services/worker/scrape-worker.ts
+++ b/apps/api/src/services/worker/scrape-worker.ts
@@ -73,7 +73,6 @@ import {
} from "../../lib/scrape-billing";
import { getCrawlScope } from "../../lib/crawl-scope";
import { billTeam } from "../billing/credit_billing";
-import { tallyAgentMcpCredits } from "../billing/agent-mcp-credits";
import { getBillingQueue } from "../queue-service";
import type { Logger } from "winston";
import {
@@ -170,9 +169,13 @@ async function billScrapeJob(
// Scrapes initiated by a search (billing.endpoint === "search", e.g. search +
// scrapeOptions) are metered against SEARCH_CREDITS, matching the search
// request's own credits. Standalone scrapes stay on CREDITS.
- const featureId = featureIdForBillingEndpoint(billing.endpoint);
+ const featureId = billing.hostedMcp
+ ? await autumnService.hostedMcpFeatureId(
+ job.data.team_id,
+ featureIdForBillingEndpoint(billing.endpoint),
+ )
+ : featureIdForBillingEndpoint(billing.endpoint);
let trackedInRequest = false;
- let mcpCredits = 0;
if (job.data.is_scrape !== true && !job.data.internalOptions?.bypassBilling) {
creditsToBeBilled = await calculateCreditsToBeBilled(
@@ -211,31 +214,24 @@ async function billScrapeJob(
routedToFirebill = orgId
? await autumnService.isRoutedThroughFirebill(job.data.team_id, orgId)
: false;
- if (orgId) {
- const charge = {
- teamId: job.data.team_id,
- orgId,
- value: creditsToBeBilled,
- properties: autumnProperties,
- featureId,
- // The worker job id is the one identity that is unique per charge
- // (a crawl id is shared by every page β€” keying on it would collapse
- // a crawl's pages into one billed event) AND survives a stall
- // requeue, which re-runs the job under the same id: with this key,
- // the re-run dedupes instead of double-billing (firebill route).
- idempotencyKey: `fc:track:${billing.endpoint}:${job.id}`,
- // The caller's own operation id, carried on the charge so
- // firebill reports it without a request lookup.
- externalRequestId: billing.externalRequestId ?? undefined,
- };
- // A hosted MCP charge spends the team's MCP credits first.
- if (billing.hostedMcp) {
- ({ tracked: trackedInRequest, mcpCredits } =
- await autumnService.trackHostedMcpCredits(charge));
- } else {
- trackedInRequest = await autumnService.trackCredits(charge);
- }
- }
+ trackedInRequest = orgId
+ ? await autumnService.trackCredits({
+ teamId: job.data.team_id,
+ orgId,
+ value: creditsToBeBilled,
+ properties: autumnProperties,
+ featureId,
+ // The worker job id is the one identity that is unique per charge
+ // (a crawl id is shared by every page β€” keying on it would collapse
+ // a crawl's pages into one billed event) AND survives a stall
+ // requeue, which re-runs the job under the same id: with this key,
+ // the re-run dedupes instead of double-billing (firebill route).
+ idempotencyKey: `fc:track:${billing.endpoint}:${job.id}`,
+ // The caller's own operation id, carried on the charge so
+ // firebill reports it without a request lookup.
+ externalRequestId: billing.externalRequestId ?? undefined,
+ })
+ : false;
// On the firebill route the ledger enqueue must be idempotent by the
// originating job: a stalled job reruns under the same job.id, the
// track dedupes in firebill, and a fresh random billing job id would
@@ -274,7 +270,7 @@ async function billScrapeJob(
team_id: job.data.team_id,
org_id: orgId,
credits: creditsToBeBilled,
- billing: mcpCredits > 0 ? { ...billing, mcpCredits } : billing,
+ billing,
is_extract: false,
timestamp: new Date().toISOString(),
originating_job_id: job.id,
@@ -301,17 +297,13 @@ async function billScrapeJob(
}
}
- await tallyAgentMcpCredits(billing, job.id, mcpCredits);
return creditsToBeBilled;
} catch (error) {
logger.error(
`Failed to add billing job to queue for team ${job.data.team_id} for ${creditsToBeBilled} credits`,
{ error },
);
- if (
- (trackedInRequest || mcpCredits > 0) &&
- creditsToBeBilled !== null
- ) {
+ if (trackedInRequest && creditsToBeBilled !== null) {
if (routedToFirebill) {
// No compensating refund on the firebill route: the tracked charge
// is durable and correct (the scrape ran), and refunding it here
@@ -331,27 +323,18 @@ async function billScrapeJob(
billing,
},
);
- await tallyAgentMcpCredits(billing, job.id, mcpCredits);
} else if (orgId) {
- const refund = {
+ await autumnService.refundCredits({
teamId: job.data.team_id,
orgId,
- // Only what was charged: the rest when it was tracked, and the
- // MCP credits part (refunded on its own below).
- value:
- (trackedInRequest ? creditsToBeBilled - mcpCredits : 0) +
- mcpCredits,
+ value: creditsToBeBilled,
properties: autumnProperties,
featureId,
// Distinct from the track key: a refund is its own charge event
// (same key would 409 as a duplicate of the track and be dropped).
idempotencyKey: `fc:refund:${billing.endpoint}:${job.id}`,
externalRequestId: billing.externalRequestId ?? undefined,
- };
- // A split charge refunds each part to the balance it was tracked on.
- await (mcpCredits > 0
- ? autumnService.refundHostedMcpCredits({ ...refund, mcpCredits })
- : autumnService.refundCredits(refund));
+ });
}
}
// The billing operation never reached the queue, so no debit will