UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

78 lines (77 loc) • 5.03 kB
/** * The run's cache-hit account: the ONE fold of a booked call's cache members onto the aggregate faces, and the ONE derivation of * the leg's cache-hit rate from it. Both usage accounts (`main-call-account.ts`, `side-call-account.ts`) book every call through * {@link bookCacheOperands}; nothing else reads a usage's cache members for the aggregates. * * Two layers, one reading each: * - a single call's `Usage` says "not reported" by leaving `cacheRead` / `cacheWrite` ABSENT (a reported 0 is a measured 0); * - an aggregate keeps its numbers (the counted sum, absent members adding nothing) and says what they are with marks: * `cacheReadUnreported` / `cacheWriteUnreported` — some call did not report that member, so the sum is not a measurement of it; * and, on a family whose input EXCLUDES the cached parts (the total is input + reads + writes), a missing cache member also * leaves the total a lower bound, which is the existing `usageMissing` mark. * * The hit rate is judged per LANE (the main loop, each side-call lane): a lane is complete while every call on it stated what the * ratio needs — a known usage (not `usageMissing`), the cache reads, and on an input-excludes-cached family the cache writes too * (they are part of the denominator). An incomplete lane is left out of the rate whole, so one lane that does not report (a * classifier on a gateway without the cache counts) does not erase another lane's rate, and a lane that reports only some of * its calls (a gateway that omits a zero) is never read off its reporting calls alone. The one identity: * `rate = (cachedTokens − Σ excluded.cachedTokens) / (totalInputTokens − Σ excluded.totalInputTokens)`. */ import type { Usage } from "../../internal/llm.js"; import type { Stats } from "./assemble-result.js"; import type { UsageLane } from "./side-call-account.js"; import type { CacheFamily } from "./usage-accounting.js"; /** One lane's ratio operands as the account counted them, and whether every call on it stated them. */ export interface CacheLaneOperands { cachedTokens: number; totalInputTokens: number; complete: boolean; } /** The lanes a leg booked calls on, keyed by lane, in the order each lane first booked one. */ export type CacheLanes = Partial<Record<UsageLane, CacheLaneOperands>>; /** A lane left out of the cache-hit rate, with what it contributed to the aggregate numbers. */ export interface CacheHitExclusion { readonly lane: UsageLane; readonly cachedTokens: number; readonly totalInputTokens: number; } /** What one booked call's usage says about its cache members. */ export interface CacheMemberMarks { /** The call did not report its cache reads. */ readonly readUnreported: boolean; /** The call did not report its cache writes. */ readonly writeUnreported: boolean; /** A member the call's total is made of was not reported (input-excludes-cached family): the total is a lower bound. */ readonly totalLowerBound: boolean; } /** A usage a call reported, under the cache family of the model that made it. */ export interface ReportedUsage { readonly family: CacheFamily; readonly usage: Usage; } /** The marks of one call's usage (absent ⇒ the call reported nothing: every member is unreported and its total a lower bound). */ export declare function cacheMemberMarksOf(reported: ReportedUsage | undefined): CacheMemberMarks; /** * Book one call on `lane`: its cache marks onto the leg's stats, its ratio operands onto the lane. `reported` absent ⇒ the call — * or a main-loop turn that booked no call — reported nothing; `usageMissing` ⇒ the call's usage is a lower bound; either way the * lane is incomplete. `totalInputTokens` is the call's cache-inclusive input total as the account counted it. Returns the * call's marks, for the turn's frame. */ export declare function bookCacheOperands(stats: Stats, lane: UsageLane, reported: ReportedUsage | undefined, usageMissing: boolean, totalInputTokens: number): CacheMemberMarks; /** A call's cache members as its `brain.call` trace row carries them: each one present only when the call reported it. */ export declare function cacheTraceMembersOf(usage: Usage): { cacheRead?: number; cacheWrite?: number; }; /** The lanes left out of the rate: every incomplete lane, with its counted operands. */ export declare function cacheHitExclusionsOf(lanes: CacheLanes | undefined): CacheHitExclusion[]; /** The rate's operands by the one identity: the aggregate numbers minus what the excluded lanes contributed. */ export declare function cacheHitOperandsOf(totals: { readonly cachedTokens: number; readonly totalInputTokens: number; }, excludes: readonly CacheHitExclusion[]): { cachedTokens: number; totalInputTokens: number; }; /** Is the main loop's lane complete? (No main-loop call booked ⇒ there is nothing on it to judge: not complete.) */ export declare function mainCacheLaneComplete(lanes: CacheLanes | undefined): boolean;