Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/core/orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import {
} from "../credentials/connector-status.ts";
import { renderComputerBlock, renderResidentLoginsBlock, renderConnectedAppsBlock } from "./environment-facts.ts";
import { PROVIDERS } from "../connectors/oauth.ts";
import { estimateCostUsd } from "../ratelimit/budget.ts";
import { costFromUsage, estimateCostUsd } from "../ratelimit/budget.ts";
import {
mintCapabilityToken,
CAPABILITY_TTL_MS,
Expand Down Expand Up @@ -2528,14 +2528,14 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
},
recordModelCall: (rec) => {
deps.modelGateway.recordCall({ at: Date.now(), scopeLabel: scopeId, ...rec });
void deps.budget?.record(actor.id, estimateCostUsd(rec.inputTokens));
},
recordLlmRequest: async (rec) => {
try {
await deps.sessions.recordLlmRequest(session.id, { ...rec, scopeLabel: scopeId });
} catch (err) {
console.error("[orchestrator] failed to persist LLM request snapshot:", errMessage(err));
}
void deps.budget?.record(actor.id, costFromUsage(rec.usage));
},
});
};
Expand Down
19 changes: 19 additions & 0 deletions src/ratelimit/budget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ export function estimateCostUsd(inputTokens: number, usdPerMTok = DEFAULT_AGENT_
return (inputTokens / 1_000_000) * usdPerMTok;
}

export function costFromUsage(
usage:
| {
input: number;
output: number;
cacheRead: number;
cacheWrite: number;
costUsd: number;
}
| null
| undefined,
): number {
if (!usage) return 0;
if (Number.isFinite(usage.costUsd) && usage.costUsd > 0) return usage.costUsd;
const tokens = usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
if (!Number.isFinite(tokens) || tokens <= 0) return 0;
return estimateCostUsd(tokens);
}

export function createBudgetTracker(
opts: { limitUsd?: number; orgLimitUsd?: number; windowMs?: number } = {},
): BudgetTracker {
Expand Down
27 changes: 26 additions & 1 deletion test/budget.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import assert from "node:assert/strict";
import { mkdtempSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { createBudgetTracker, estimateCostUsd } from "../src/ratelimit/budget.ts";
import { costFromUsage, createBudgetTracker, estimateCostUsd } from "../src/ratelimit/budget.ts";
import { DEFAULT_AGENT_INPUT_USD_PER_MTOK } from "../src/model/pi-models.ts";
import { buildApp } from "../src/wiring.ts";
import type { TurnRequest } from "../src/types.ts";
Expand Down Expand Up @@ -59,3 +59,28 @@ test("a principal over budget is refused by the app", async () => {
assert.equal(second.status, "refused");
assert.match(second.reason ?? "", /budget exceeded/);
});

test("costFromUsage prefers the provider-reported cost when it is usable", () => {
assert.equal(costFromUsage({ input: 1_000_000, output: 0, cacheRead: 0, cacheWrite: 0, costUsd: 0.03 }), 0.03);
});

test("costFromUsage falls back to a token-total estimate without a usable cost", () => {
const fallback = { input: 500_000, output: 300_000, cacheRead: 100_000, cacheWrite: 100_000, costUsd: 0 };
assert.equal(costFromUsage(fallback), DEFAULT_AGENT_INPUT_USD_PER_MTOK);
assert.equal(
costFromUsage({ input: 0, output: 0, cacheRead: 0, cacheWrite: 0, costUsd: Number.NaN }),
0,
"no tokens and no cost means nothing to bill",
);
});

test("costFromUsage treats missing metering as zero cost", () => {
assert.equal(costFromUsage(null), 0);
assert.equal(costFromUsage(undefined), 0);
});

test("metered output tokens count against the budget even with no input growth", async () => {
const b = createBudgetTracker({ limitUsd: DEFAULT_AGENT_INPUT_USD_PER_MTOK, windowMs: 60_000 });
await b.record("U1", costFromUsage({ input: 0, output: 2_000_000, cacheRead: 0, cacheWrite: 0, costUsd: 0 }), 1000);
assert.equal((await b.check("U1", 1000)).allowed, false);
});