UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

38 lines (37 loc) • 3.14 kB
/** * The run's usage account for the MAIN loop's model calls: one function books one call at the model that made it, and the * hops a fallback walk left behind are booked through it before the answer. The model calls made beside the main loop book * through `side-call-account.ts` instead; both price a bill the same way (`RunState.degrade.priceOf`), and both fold a call's * cache members through the one cache account (`cache-hit-account.ts`). */ import type { Model, Usage } from "../../internal/llm.js"; import type { ServedReading } from "../../brain/model-lane.js"; import type { Stats } from "./assemble-result.js"; import { type CacheMemberMarks } from "./cache-hit-account.js"; import type { RunState } from "./contracts.js"; /** One main-loop call as the account booked it. `turnTotal` is the CACHE-INCLUSIVE prompt total (billing + window/budget * denominator); `turnUncached` is the protocol-shaped miss count the `promptTokens` / `inputTokens` faces carry; `priced` = the * call has a cost figure (`callPriced`: a table that declares the serving tier, or a call that billed nothing; `false` ⇒ the cost * is a fabricated 0, or the standard rates' lower bound for a fast-served call, and the disclosure faces omit it). */ export interface BookedCall { readonly turnTotal: number; readonly turnUncached: number; readonly turnCostMicroUsd: number; readonly priced: boolean; } /** * Book one main-loop call of `usage` at `model`: re-priced in core from the injected pricing (the brain's own cost total charges * cached tokens at full price for OpenAI / DeepSeek), into the leg's stats and the upcoming `turn_end` usage (a turn may make * more than one call). Integer micro-USD, summed per call, so a mid-task model or price change is accounted correctly. * `usageMissing` = the call's usage is a lower bound (the caller marks the turn and the leg; the cache account leaves the main * lane out of the hit rate). A cache member the call did not report marks the turn's frame as it marks the leg's stats. * `apiMs` is the call's API wall time: from the engine's issue stamp (the row's `callStartedAt`) to the row's own `ts`, 0 where the call has * no stamp (a hop a walk left behind, a call beside the loop); it is summed into the turn's and the leg's `apiDurationMs` here, beside the tokens. */ export declare function bookMainCall(rs: RunState, stats: Stats, model: Model, usage: Usage, usageMissing: boolean, apiMs: number): BookedCall; /** Carry one booking's cache marks onto the upcoming `turn_end` (the harness-handlers lane also calls it for a turn that booked no * call at all). */ export declare function noteTurnCacheMarks(rs: RunState, marks: CacheMemberMarks): void; /** Book the requests an answer left behind (the lane's hops, the failover decorator's gateways; none for an answer that lists none): each * its own bill at its own model and its own `brain.call` row (latency not modeled, as on a side call); a hop whose usage is a lower bound marks the turn and the leg. */ export declare function bookLeftHops(rs: RunState, stats: Stats, reading: ServedReading | undefined): void;