Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. π₯
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
fix(monitor): bill search checks at actual cost β search call, scrapes, and LLM/judge calls
Developers Digest89da210fa46c
diff --git a/apps/api/src/scraper/scrapeURL/transformers/llmExtract.ts b/apps/api/src/scraper/scrapeURL/transformers/llmExtract.tsindex e581b2ccd..e9ef087b8 100644--- a/apps/api/src/scraper/scrapeURL/transformers/llmExtract.ts+++ b/apps/api/src/scraper/scrapeURL/transformers/llmExtract.ts@@ -259,6 +259,11 @@ export function calculateCost(output_cost: 2.19,},"google/gemini-2.5-flash-lite": { input_cost: 0.1, output_cost: 0.4 },+ // Search-monitor models. flash-lite mirrors gemini-2.5-flash-lite pricing;+ // gemini-3-flash-preview priced at published Gemini Flash input/output rates.+ "gemini-flash-lite-latest": { input_cost: 0.1, output_cost: 0.4 },+ "gemini-2.5-flash-lite": { input_cost: 0.1, output_cost: 0.4 },+ "gemini-3-flash-preview": { input_cost: 0.3, output_cost: 2.5 },};let modelCost = modelCosts[model] || { input_cost: 0, output_cost: 0 };//gemini-2.5-pro-exp-03-25 pricingdiff --git a/apps/api/src/services/monitoring/runner.ts b/apps/api/src/services/monitoring/runner.tsindex cc46f424d..f68fa9d82 100644--- a/apps/api/src/services/monitoring/runner.ts+++ b/apps/api/src/services/monitoring/runner.ts@@ -946,18 +946,6 @@ async function enqueueMonitorCrawlTarget(params: {return params.targetRun;}-// Credit attribution via the canonical estimator: scraped results ran a json extraction,-// already-seen results skip the scrape, failures bill the base scrape credit.-const SEARCH_JSON_DOC = { json: {} } as const;-const SEARCH_JSON_OPTS = { formats: [{ type: "json" }] } as const;-function searchPageCredits(status: string, scraped: boolean): number {- if (status === "already_seen") return 0;- if (scraped) {- return estimateActualCredits(SEARCH_JSON_DOC, SEARCH_JSON_OPTS);- }- return 0;-}-// Runs inline (owns its search + scrape + judge), then persists onto the same// monitor_pages / monitor_check_pages tables the reconciler tallies.async function runMonitorSearchTarget(params: {@@ -1015,25 +1003,22 @@ async function runMonitorSearchTarget(params: {}),});+ // Run-level credits that aren't tied to a single page: the search() calls+ // (~2 credits / 10 results) and every LLM/judge call (router, snippet judge,+ // criteria compile, skeptic, resolver, material-dev, summarizer), all billed+ // at cost by runSearchTarget. Fold this overhead onto the first persisted page+ // so the page-sum reconciler (calculateMonitorCheckActualCredits) deducts it.+ const overheadCredits = result.searchCredits + result.llmCredits;+const pages: PageResult[] = [];- for (const upsert of result.pageUpserts) {+ result.pageUpserts.forEach((upsert, index) => {const status = searchStatusToPageStatus(upsert.status);- const creditsUsed = searchPageCredits(- upsert.status,- upsert.scraped ?? false,- );+ // Real per-page scrape cost from calculateCreditsToBeBilled (0 when the page+ // was not scraped β e.g. already_seen or snippet-only depth).+ const pageScrapeCredits = upsert.creditsUsed ?? 0;+ const creditsUsed =+ index === 0 ? pageScrapeCredits + overheadCredits : pageScrapeCredits;const metadata = { ...upsert.metadata, creditsUsed };- await upsertMonitorPage({- monitorId: monitor.id,- teamId: monitor.team_id,- targetId: target.id,- url: upsert.url,- source: "discovered",- checkId: check.id,- scrapeId: null,- status,- metadata,- });pages.push({check_id: check.id,monitor_id: monitor.id,@@ -1046,7 +1031,42 @@ async function runMonitorSearchTarget(params: {judgment: upsert.judgment ?? null,emailStatus: status,});+ });++ for (let i = 0; i < pages.length; i++) {+ const page = pages[i];+ await upsertMonitorPage({+ monitorId: monitor.id,+ teamId: monitor.team_id,+ targetId: target.id,+ url: page.url,+ source: "discovered",+ checkId: check.id,+ scrapeId: null,+ status: page.status,+ metadata: page.metadata as Record<string, unknown>,+ });}++ // No pages persisted (e.g. zero search results) but the search() call + any+ // criteria-compile LLM still cost credits. Persist a synthetic overhead page+ // so the reconciler bills it.+ if (pages.length === 0 && overheadCredits > 0) {+ const overheadUrl = `monitor-search-overhead:${target.id}`;+ const overheadPage: PageResult = {+ check_id: check.id,+ monitor_id: monitor.id,+ team_id: monitor.team_id,+ target_id: target.id,+ url: overheadUrl,+ url_hash: hashMonitorUrl(overheadUrl),+ status: "same",+ metadata: { creditsUsed: overheadCredits, searchOverhead: true },+ emailStatus: "same",+ };+ pages.push(overheadPage);+ }+await insertMonitorCheckPages(pages);for (const page of pages) {diff --git a/apps/api/src/services/monitoring/search/cost.test.ts b/apps/api/src/services/monitoring/search/cost.test.tsnew file mode 100644index 000000000..e624df95e--- /dev/null+++ b/apps/api/src/services/monitoring/search/cost.test.ts@@ -0,0 +1,55 @@+import { CostTracking } from "../../../lib/cost-tracking";+import { recordLlmCall, llmCostToCredits } from "./cost";++describe("search-monitor LLM cost billing", () => {+ it("records a call's token cost into the shared CostTracking", () => {+ const ct = new CostTracking();+ recordLlmCall({+ costTracking: ct,+ model: "gemini-flash-lite-latest",+ usage: { inputTokens: 1_000_000, outputTokens: 1_000_000 },+ stage: "routeSearchResults",+ });+ // gemini-flash-lite-latest: $0.10 in + $0.40 out per 1M tokens.+ expect(ct.toJSON().totalCost).toBeCloseTo(0.5, 6);+ });++ it("normalizes promptTokens/completionTokens (older SDK usage shape)", () => {+ const ct = new CostTracking();+ recordLlmCall({+ costTracking: ct,+ model: "gemini-flash-lite-latest",+ usage: { promptTokens: 1_000_000, completionTokens: 0 },+ stage: "summarizeRun",+ });+ expect(ct.toJSON().totalCost).toBeCloseTo(0.1, 6);+ });++ it("converts accumulated dollar cost to credits via the platform's at-cost rule", () => {+ const ct = new CostTracking();+ recordLlmCall({+ costTracking: ct,+ model: "gemini-flash-lite-latest",+ usage: { inputTokens: 1_000_000, outputTokens: 1_000_000 },+ stage: "routeSearchResults",+ });+ // tokensBilled = ceil(0.5 * 20000) = 10000; credits = ceil(10000 / 15) = 667.+ expect(llmCostToCredits(ct)).toBe(667);+ });++ it("returns 0 credits when no LLM calls were made", () => {+ expect(llmCostToCredits(new CostTracking())).toBe(0);+ });++ it("returns 0 cost for an unknown/unpriced model rather than throwing", () => {+ const ct = new CostTracking();+ recordLlmCall({+ costTracking: ct,+ model: "some-model-with-no-price",+ usage: { inputTokens: 1000, outputTokens: 1000 },+ stage: "test",+ });+ expect(ct.toJSON().totalCost).toBe(0);+ expect(llmCostToCredits(ct)).toBe(0);+ });+});diff --git a/apps/api/src/services/monitoring/search/cost.ts b/apps/api/src/services/monitoring/search/cost.tsnew file mode 100644index 000000000..b88bf75b3--- /dev/null+++ b/apps/api/src/services/monitoring/search/cost.ts@@ -0,0 +1,59 @@+import { CostTracking } from "../../../lib/cost-tracking";+import { calculateCost } from "../../../scraper/scrapeURL/transformers/llmExtract";+import { calculateThinkingCost } from "../../../lib/extract/usage/llm-cost";++// AI SDK `generateObject` returns token usage under `usage.inputTokens` /+// `usage.outputTokens`. Older SDK builds expose `promptTokens` /+// `completionTokens`; normalize both so cost recording never silently drops to 0.+type RawUsage =+ | {+ inputTokens?: number;+ outputTokens?: number;+ promptTokens?: number;+ completionTokens?: number;+ }+ | undefined+ | null;++function inputTokensOf(usage: RawUsage): number {+ return usage?.inputTokens ?? usage?.promptTokens ?? 0;+}++function outputTokensOf(usage: RawUsage): number {+ return usage?.outputTokens ?? usage?.completionTokens ?? 0;+}++/**+ * Record a single monitor LLM call against a shared CostTracking, mirroring how+ * the scrape/extract path bills LLM work "at cost" (llmExtract.ts β calculateCost+ * β CostTracking.addCall). The dollar cost is later converted to credits via+ * {@link llmCostToCredits}, the same conversion extract uses.+ */+export function recordLlmCall(params: {+ costTracking: CostTracking;+ model: string;+ usage: RawUsage;+ stage: string;+}): void {+ const input = inputTokensOf(params.usage);+ const output = outputTokensOf(params.usage);+ params.costTracking.addCall({+ type: "other",+ metadata: { source: "search-monitor", stage: params.stage },+ model: params.model,+ cost: calculateCost(params.model, input, output),+ tokens: { input, output },+ });+}++/**+ * Convert the accumulated LLM dollar cost into credits. Uses the platform's+ * canonical at-cost conversion: calculateThinkingCost(ct) = ceil(totalCost *+ * 20000) tokens, then ceil(tokens / 15) credits β identical to how the extract+ * endpoint bills its fire-1 / thinking LLM work (extraction-service.ts).+ */+export function llmCostToCredits(costTracking: CostTracking): number {+ const tokensBilled = calculateThinkingCost(costTracking);+ if (tokensBilled <= 0) return 0;+ return Math.ceil(tokensBilled / 15);+}diff --git a/apps/api/src/services/monitoring/search/criteria.ts b/apps/api/src/services/monitoring/search/criteria.tsindex a9be0f233..7817d955a 100644--- a/apps/api/src/services/monitoring/search/criteria.ts+++ b/apps/api/src/services/monitoring/search/criteria.ts@@ -1,6 +1,8 @@import { generateObject } from "ai";import { z } from "zod";import { googleModel, googleProviderOptions } from "./tuning";+import { recordLlmCall } from "./cost";+import type { CostTracking } from "../../../lib/cost-tracking";const CRITERIA_MODEL =process.env.SEARCH_MONITOR_CRITERIA_MODEL ?? "gemini-flash-lite-latest";@@ -135,9 +137,10 @@ export async function compileGoalCriteriaWithLlm(params: {subject: string;queries: string[];goalVersion: string;+ costTracking?: CostTracking;}): Promise<GoalCriteria> {const deterministic = compileGoalCriteria(params);- const { object } = await generateObject({+ const { object, usage } = await generateObject({model: googleModel(CRITERIA_MODEL),schema: criteriaSchema,system:@@ -150,5 +153,13 @@ export async function compileGoalCriteriaWithLlm(params: {temperature: 0,...googleProviderOptions(),});+ if (params.costTracking) {+ recordLlmCall({+ costTracking: params.costTracking,+ model: CRITERIA_MODEL,+ usage,+ stage: "compileGoalCriteria",+ });+ }return mergeCompiledCriteria(deterministic, object);}diff --git a/apps/api/src/services/monitoring/search/llm.ts b/apps/api/src/services/monitoring/search/llm.tsindex b56b7bb9a..87a789176 100644--- a/apps/api/src/services/monitoring/search/llm.ts+++ b/apps/api/src/services/monitoring/search/llm.ts@@ -2,6 +2,8 @@ import { generateObject } from "ai";import { z } from "zod";import { googleModel, googleProviderOptions } from "./tuning";import type { GoalCriteria } from "./criteria";+import { recordLlmCall } from "./cost";+import type { CostTracking } from "../../../lib/cost-tracking";const EVENT_MODEL =process.env.SEARCH_MONITOR_EVENT_MODEL ?? "gemini-flash-lite-latest";@@ -38,8 +40,9 @@ export async function resolveEvent(params: {subject: string;result: { title: string; url: string; evidence: string };candidates: KnownEvent[];+ costTracking?: CostTracking;}): Promise<EventResolution> {- const { object } = await generateObject({+ const { object, usage } = await generateObject({model: googleModel(EVENT_MODEL),schema: eventResolverSchema,system:@@ -65,6 +68,15 @@ export async function resolveEvent(params: {...googleProviderOptions(),});+ if (params.costTracking) {+ recordLlmCall({+ costTracking: params.costTracking,+ model: EVENT_MODEL,+ usage,+ stage: "resolveEvent",+ });+ }+return {matchedKey: object.matchedKey ?? null,isNew: object.matchedKey ? false : object.isNew !== false,@@ -83,8 +95,9 @@ export async function judgeMaterialDevelopment(params: {subject: string;eventLabel: string;result: { title: string; evidence: string };+ costTracking?: CostTracking;}): Promise<{ material: boolean; reason: string }> {- const { object } = await generateObject({+ const { object, usage } = await generateObject({model: googleModel(EVENT_MODEL),schema: materialDevSchema,system:@@ -104,6 +117,14 @@ export async function judgeMaterialDevelopment(params: {temperature: 0,...googleProviderOptions(),});+ if (params.costTracking) {+ recordLlmCall({+ costTracking: params.costTracking,+ model: EVENT_MODEL,+ usage,+ stage: "judgeMaterialDevelopment",+ });+ }return { material: object.material === true, reason: object.reason ?? "" };}@@ -135,8 +156,9 @@ export async function reviewAlert(params: {concept: string;judgeAnswer: string;};+ costTracking?: CostTracking;}): Promise<SkepticVerdict> {- const { object } = await generateObject({+ const { object, usage } = await generateObject({model: googleModel(SKEPTIC_MODEL),schema: alertSkepticSchema,system:@@ -164,6 +186,14 @@ export async function reviewAlert(params: {temperature: 0,...googleProviderOptions(),});+ if (params.costTracking) {+ recordLlmCall({+ costTracking: params.costTracking,+ model: SKEPTIC_MODEL,+ usage,+ stage: "reviewAlert",+ });+ }return object;}@@ -197,9 +227,10 @@ export async function routeSearchResults(params: {url: string;snippet: string;}>;+ costTracking?: CostTracking;}): Promise<RouteDecision[]> {const candidates = params.candidates.slice(0, 50);- const { object } = await generateObject({+ const { object, usage } = await generateObject({model: googleModel(ROUTER_MODEL),schema: routerSchema,system:@@ -219,6 +250,14 @@ export async function routeSearchResults(params: {temperature: 0,...googleProviderOptions(),});+ if (params.costTracking) {+ recordLlmCall({+ costTracking: params.costTracking,+ model: ROUTER_MODEL,+ usage,+ stage: "routeSearchResults",+ });+ }const validIds = new Set(candidates.map(c => c.id));return object.decisions.filter(d => validIds.has(d.id))@@ -257,9 +296,10 @@ export async function judgeSnippets(params: {url: string;snippet: string;}>;+ costTracking?: CostTracking;}): Promise<SnippetVerdict[]> {if (params.candidates.length === 0) return [];- const { object } = await generateObject({+ const { object, usage } = await generateObject({model: googleModel(ROUTER_MODEL),schema: snippetVerdictSchema,system:@@ -272,6 +312,14 @@ export async function judgeSnippets(params: {temperature: 0,...googleProviderOptions(),});+ if (params.costTracking) {+ recordLlmCall({+ costTracking: params.costTracking,+ model: ROUTER_MODEL,+ usage,+ stage: "judgeSnippets",+ });+ }const validIds = new Set(params.candidates.map(c => c.id));return object.verdicts.filter(v => validIds.has(v.id));}@@ -285,8 +333,9 @@ export async function summarizeRun(params: {goal: string;subject: string;evidence: Array<{ title: string; url: string; rationale: string }>;+ costTracking?: CostTracking;}): Promise<{ label: string; summary: string }> {- const { object } = await generateObject({+ const { object, usage } = await generateObject({model: googleModel(SUMMARY_MODEL),schema: runSummarySchema,system:@@ -303,5 +352,14 @@ export async function summarizeRun(params: {...googleProviderOptions(),});+ if (params.costTracking) {+ recordLlmCall({+ costTracking: params.costTracking,+ model: SUMMARY_MODEL,+ usage,+ stage: "summarizeRun",+ });+ }+return { label: object.label, summary: object.summary };}diff --git a/apps/api/src/services/monitoring/search/run.ts b/apps/api/src/services/monitoring/search/run.tsindex 56851fb10..fc7de7df0 100644--- a/apps/api/src/services/monitoring/search/run.ts+++ b/apps/api/src/services/monitoring/search/run.ts@@ -5,6 +5,8 @@ import { buildSearchQuery } from "../../../lib/search-query-builder";import { scrapeURL } from "../../../scraper/scrapeURL";import { scrapeOptions } from "../../../controllers/v2/types";import { CostTracking } from "../../../lib/cost-tracking";+import { calculateCreditsToBeBilled } from "../../../lib/scrape-billing";+import { llmCostToCredits } from "./cost";import { hashMonitorUrl } from "../store";import { canonicalizeUrl, stableSerpFingerprint } from "./dedupe";import {@@ -110,11 +112,20 @@ type SearchTargetRunResult = {matches: number;summary: string;sources: SearchSource[];+ // Credits attributable to the search() calls themselves (β2 per 10 results),+ // matching executeSearch's search-credit math.+ searchCredits: number;+ // Credits for all monitor LLM/judge calls (router, snippet judge, criteria+ // compile, skeptic, resolver, material-dev, summarizer), billed at cost.+ llmCredits: number;pageUpserts: Array<{url: string;urlHash: Buffer;status: string;scraped?: boolean;+ // Real per-page scrape credits from calculateCreditsToBeBilled() β includes+ // the scrape's own JSON/LLM extraction cost. 0 when the page was not scraped.+ creditsUsed?: number;metadata: Record<string, unknown>;judgment?: {meaningful: boolean;@@ -152,6 +163,12 @@ export async function runSearchTarget(params: {let pagesChecked = 0;let skipped = 0;let matches = 0;+ // Single CostTracking shared by every LLM/judge call (and the per-page scrape+ // extraction) in this run, so the run's LLM dollar cost converts to credits+ // once via llmCostToCredits β the platform's canonical at-cost LLM billing.+ const costTracking = new CostTracking();+ // Search calls bill β2 credits / 10 results, mirroring executeSearch().+ let searchResultsBilled = 0;const seenThisRun = new Map<string, number>();const candidates: Array<{@@ -174,6 +191,9 @@ export async function runSearchTarget(params: {});// v2 search returns { web, news, images }; the monitor consumes web hits.const results = response.web ?? [];+ // Bill the search call at the platform rate (executeSearch: ~2 credits / 10+ // results) on the results this query actually returned, before dedupe/trim.+ searchResultsBilled += results.length;for (const r of results) {if (!r.url) continue;if (isExcludedDomain(r.url, target.excludeDomains)) continue;@@ -220,6 +240,7 @@ export async function runSearchTarget(params: {subject,queries: target.queries,goalVersion,+ costTracking,});} catch (error) {logger.warn(@@ -244,6 +265,7 @@ export async function runSearchTarget(params: {subject,searchWindow: target.searchWindow,maxResults: target.maxResults,+ costTracking,candidates: candidates.map((c, i) => ({id: `result_${i + 1}`,query: c.query,@@ -302,6 +324,7 @@ export async function runSearchTarget(params: {goal,subject,searchWindow: target.searchWindow,+ costTracking,candidates: snippetCandidates.map((c, i) => ({id: `result_${i + 1}`,query: c.query,@@ -458,6 +481,7 @@ export async function runSearchTarget(params: {let verdict: SearchVerdict | null = null;let pageText = "";let realDate: string | null = null;+ let scrapeCredits = 0;if (depth === "standard") {verdict = snippetVerdicts.get(canonical) ?? null;if (!verdict) {@@ -468,23 +492,25 @@ export async function runSearchTarget(params: {pagesChecked += 1;} else {let res;+ const scrapeParsedOptions = scrapeOptions.parse({+ formats: [+ { type: "markdown" },+ {+ type: "json",+ schema: verdictJsonSchema,+ prompt: buildJudgePrompt(goal, subject, target.searchWindow),+ },+ ],+ timeout: 20000,+ });+ const scrapeCostTracking = new CostTracking();try {res = await scrapeURL("search-monitor;" + uuidv7(),c.url,- scrapeOptions.parse({- formats: [- { type: "markdown" },- {- type: "json",- schema: verdictJsonSchema,- prompt: buildJudgePrompt(goal, subject, target.searchWindow),- },- ],- timeout: 20000,- }),+ scrapeParsedOptions,{ teamId, zeroDataRetention: params.zeroDataRetention },- new CostTracking(),+ scrapeCostTracking,);} catch (error) {logger.warn("search monitor scrape threw", { url: c.url, error });@@ -501,6 +527,26 @@ export async function runSearchTarget(params: {}pagesChecked += 1;+ // Bill the scrape at actual cost via the same primitive the worker uses+ // (calculateCreditsToBeBilled): base + JSON-extraction + any premium+ // surcharges + the scrape's own LLM extraction cost. This replaces the+ // dummy searchPageCredits() estimate.+ try {+ scrapeCredits = await calculateCreditsToBeBilled(+ scrapeParsedOptions,+ { teamId, zeroDataRetention: params.zeroDataRetention },+ res.document,+ scrapeCostTracking,+ {},+ );+ } catch (error) {+ logger.warn("search monitor scrape credit calc failed; using base", {+ url: c.url,+ error,+ });+ scrapeCredits = res.document.metadata?.creditsUsed ?? 1;+ }+verdict = parseVerdict(res.document.json);if (!verdict) {skipped += 1;@@ -545,6 +591,7 @@ export async function runSearchTarget(params: {goal,subject,criteria,+ costTracking,result: {title: c.title,url: c.url,@@ -588,6 +635,7 @@ export async function runSearchTarget(params: {urlHash: hashMonitorUrl(canonical),status: "ignored",scraped: depth === "deep",+ creditsUsed: scrapeCredits,metadata: { ...baseMeta, searchStatus: "ignored" },});continue;@@ -604,6 +652,7 @@ export async function runSearchTarget(params: {urlHash: hashMonitorUrl(canonical),status: "watching",scraped: depth === "deep",+ creditsUsed: scrapeCredits,metadata: { ...baseMeta, searchStatus: "watching" },});continue;@@ -614,6 +663,7 @@ export async function runSearchTarget(params: {resolution = await resolveEvent({goal,subject,+ costTracking,result: {title: c.title,url: c.url,@@ -650,6 +700,7 @@ export async function runSearchTarget(params: {goal,subject,eventLabel,+ costTracking,result: { title: c.title, evidence: verdict.rationale },});alreadySatisfied = !dev.material;@@ -674,6 +725,7 @@ export async function runSearchTarget(params: {urlHash: hashMonitorUrl(canonical),status: "already_seen",scraped: depth === "deep",+ creditsUsed: scrapeCredits,metadata: { ...eventMeta, searchStatus: "already_seen" },});continue;@@ -708,6 +760,7 @@ export async function runSearchTarget(params: {urlHash: hashMonitorUrl(canonical),status: "alert",scraped: depth === "deep",+ creditsUsed: scrapeCredits,metadata: {...eventMeta,searchStatus: "alert",@@ -736,6 +789,7 @@ export async function runSearchTarget(params: {const out = await summarizeRun({goal,subject,+ costTracking,evidence: sources.filter(s => s.status === "alert").map(s => ({@@ -753,6 +807,11 @@ export async function runSearchTarget(params: {}}+ const isZDR = params.zeroDataRetention;+ const searchCredits =+ Math.ceil(searchResultsBilled / 10) * (isZDR ? 10 : 2);+ const llmCredits = llmCostToCredits(costTracking);+return {targetId: target.id,type: "search",@@ -763,6 +822,8 @@ export async function runSearchTarget(params: {matches,summary,sources,+ searchCredits,+ llmCredits,pageUpserts,};}diff --git a/apps/api/src/services/monitoring/store.ts b/apps/api/src/services/monitoring/store.tsindex 67a76b2e3..384fdb059 100644--- a/apps/api/src/services/monitoring/store.ts+++ b/apps/api/src/services/monitoring/store.ts@@ -41,6 +41,13 @@ const REMOVED_PAGE_CREDITS = 0;const X_TWITTER_POSTPROCESSOR_CREDIT_BONUS = 29;const DEFAULT_CRAWL_LIMIT_FOR_ESTIMATE = 10000;const MONITOR_CHECK_PAGE_BATCH_SIZE = 1000;+// Search monitors also bill the search() call (~2 credits / 10 results) and+// every LLM/judge call at cost. The estimate adds a per-result LLM allowance so+// the reserved credits roughly cover the new at-cost actuals (router + snippet+// judge + skeptic + resolver + summarizer). Tuned to typical gemini-flash-lite+// usage; the actual deduction is always the at-cost figure, never this estimate.+const SEARCH_CREDITS_PER_TEN_RESULTS = 2;+const SEARCH_LLM_CREDITS_PER_RESULT = 2;type MonitorCreditMetadata = {creditsUsed?: unknown;@@ -144,14 +151,31 @@ function estimateTargetBaseCredits(target: MonitorTarget): number {return target.urls.length * creditsPerPage;}if (target.type === "search") {+ // The search() call bills ~2 credits / 10 results regardless of depth.+ const searchCallCredits =+ Math.ceil(Math.max(1, target.maxResults) / 10) *+ SEARCH_CREDITS_PER_TEN_RESULTS *+ Math.max(1, target.queries.length);+ if (target.depth === "raw") {+ // No scrape, no LLM β only the search call.+ return searchCallCredits;+ }+ // Every result may drive LLM/judge work billed at cost.+ const llmCredits = target.maxResults * SEARCH_LLM_CREDITS_PER_RESULT;if (target.depth === "standard") {- return Math.max(1, target.queries.length);+ // Snippet-only: no per-result scrape, but LLM snippet judging still runs.+ return (+ searchCallCredits + Math.max(1, target.queries.length) + llmCredits+ );}+ // Deep: each selected result is scraped with JSON extraction.return (+ searchCallCredits ++ llmCredits +target.maxResults *- estimateBaseCreditsPerPage({- formats: [{ type: "json" }],- } as MonitorTarget["scrapeOptions"])+ estimateBaseCreditsPerPage({+ formats: [{ type: "json" }],+ } as MonitorTarget["scrapeOptions"]));}@@ -268,6 +292,14 @@ export function calculateMonitorCheckActualCreditsFromPages(return 0;}+ // Search-target pages bill all of their LLM/judge work at cost (folded into+ // metadata.creditsUsed by the search runner), so the flat per-page judge+ // credit would double-count them. Only scrape/crawl judge invocations get it.+ const target = targetsById.get(page.target_id ?? "");+ if (target?.type === "search") {+ return 0;+ }+// A persisted judgment means the judge ran for this page. Charge for that// invocation whether the verdict was meaningful or not.return JUDGE_CREDITS_PER_PAGE;