UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

66 lines (65 loc) • 3.73 kB
import { servedAtFastSpeed } from "../../brain/usage-reading.js"; import { emitTrace } from "../trace.js"; import { bookCacheOperands, cacheTraceMembersOf } from "./cache-hit-account.js"; import { noteUnpricedSpend, unpricedCallCause } from "./side-call-account.js"; import { callPriced, usageCostMicroUsd } from "./usage-accounting.js"; export function bookMainCall(rs, stats, model, usage, usageMissing, apiMs) { const price = rs.degrade.priceOf(model); const { totalInputTokens: turnTotal, uncachedInputTokens: turnUncached, costMicroUsd: turnCostMicroUsd, tierPriced } = usageCostMicroUsd(price.cacheFamily, usage, price.pricing); const priced = callPriced(price.pricingConfigured, tierPriced, usage, usageMissing); const requested = rs.telemetry.requestedModel; const via = requested !== undefined && model !== requested ? { fallbackOf: requested.id } : rs.degrade.budgetSwitched ? "degrade" : "main"; if (!priced) noteUnpricedSpend(rs.telemetry, unpricedCallCause(model.id, price.pricingConfigured, via)); stats.tokens += usage.totalTokens || 0; stats.promptTokens += turnUncached; stats.totalInputTokens += turnTotal; stats.cachedTokens += usage.cacheRead ?? 0; stats.cacheWriteTokens += usage.cacheWrite ?? 0; stats.cacheWriteTokensLong += usage.cacheWriteLong ?? 0; stats.outputTokens += usage.output || 0; stats.costMicroUsd += turnCostMicroUsd; stats.apiDurationMs = (stats.apiDurationMs ?? 0) + apiMs; const marks = bookCacheOperands(stats, "main", { family: price.cacheFamily, usage }, usageMissing, turnTotal); const fast = servedAtFastSpeed(usage); if (fast) stats.fastSpeedCalls = (stats.fastSpeedCalls ?? 0) + 1; const firstOfTurn = rs.turn.turnUsage === undefined; rs.turn.turnUsage ??= { inputTokens: 0, totalInputTokens: 0, outputTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0, costMicroUsd: 0, apiDurationMs: 0 }; if (fast && firstOfTurn) rs.turn.turnUsage.speed = "fast"; else if (!fast) delete rs.turn.turnUsage.speed; rs.turn.turnUsage.inputTokens += turnUncached; rs.turn.turnUsage.totalInputTokens += turnTotal; rs.turn.turnUsage.outputTokens += usage.output || 0; rs.turn.turnUsage.cacheReadTokens += usage.cacheRead ?? 0; rs.turn.turnUsage.cacheWriteTokens += usage.cacheWrite ?? 0; rs.turn.turnUsage.costMicroUsd += turnCostMicroUsd; rs.turn.turnUsage.apiDurationMs += apiMs; noteTurnCacheMarks(rs, marks); return { turnTotal, turnUncached, turnCostMicroUsd, priced }; } export function noteTurnCacheMarks(rs, marks) { if (marks.readUnreported) rs.turn.turnCacheMarks.cacheReadUnreported = true; if (marks.writeUnreported) rs.turn.turnCacheMarks.cacheWriteUnreported = true; if (marks.totalLowerBound) rs.turn.turnUsageMissing = true; } export function bookLeftHops(rs, stats, reading) { for (const attempt of reading?.attempts ?? []) { if (attempt.usageMissing === true) { rs.turn.turnUsageMissing = true; stats.usageMissing = true; } const billed = bookMainCall(rs, stats, attempt.model, attempt.usage, attempt.usageMissing === true, 0); emitTrace(rs.telemetry.tracer, () => ({ kind: "brain.call", version: 1, taskId: rs.telemetry.taskId, model: attempt.model.id, provider: attempt.model.provider, turn: stats.turns + 1, promptTokens: billed.turnUncached, totalInputTokens: billed.turnTotal, completionTokens: attempt.usage.output || 0, ...cacheTraceMembersOf(attempt.usage), latencyMs: 0, ...(billed.priced ? { costMicroUsd: billed.turnCostMicroUsd } : {}), ts: Date.now(), })); } }