Skip to content
Merged
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
16 changes: 16 additions & 0 deletions packages/scrawn/src/core/scrawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,14 @@ export class Scrawn<
expr: payload.inputCacheDebit.expr?._expr,
}
: undefined,
outputCacheTokens: payload.outputCacheTokens,
outputCacheDebit: payload.outputCacheDebit
? {
amount: payload.outputCacheDebit.amount,
tag: payload.outputCacheDebit.tag,
expr: payload.outputCacheDebit.expr?._expr,
}
: undefined,
};

// Validate each payload
Expand Down Expand Up @@ -1147,6 +1155,14 @@ export class Scrawn<
resolveTokens(validated.inputCacheDebit.expr, tokenContext)
)
: undefined,
outputCacheTokens: validated.outputCacheTokens ?? 0,
outputCacheAmount: validated.outputCacheDebit?.amount ?? undefined,
outputCacheTag: validated.outputCacheDebit?.tag ?? undefined,
outputCacheExpr: validated.outputCacheDebit?.expr
? serializeExpr(
resolveTokens(validated.outputCacheDebit.expr, tokenContext)
)
: undefined,
} as AITokenUsage;

const eventId = randomUUID();
Expand Down
14 changes: 14 additions & 0 deletions packages/scrawn/src/core/types/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ const DebitFieldSchema = z
* - provider: optional non-empty string
* - inputCacheTokens: optional non-negative integer
* - inputCacheDebit: optional one of amount, tag, or expr
* - outputCacheTokens: optional non-negative integer
* - outputCacheDebit: optional one of amount, tag, or expr
*/
export const AITokenUsagePayloadSchema = z.object({
userId: z.string().min(1, "userId must be a non-empty string"),
Expand All @@ -383,6 +385,12 @@ export const AITokenUsagePayloadSchema = z.object({
.nonnegative("inputCacheTokens must be non-negative")
.optional(),
inputCacheDebit: DebitFieldSchema.optional(),
outputCacheTokens: z
.number()
.int("outputCacheTokens must be an integer")
.nonnegative("outputCacheTokens must be non-negative")
.optional(),
outputCacheDebit: DebitFieldSchema.optional(),
});

/**
Expand All @@ -403,6 +411,8 @@ export const AITokenUsagePayloadSchema = z.object({
* @property provider - (Optional) LLM provider identifier (e.g. 'openai', 'anthropic')
* @property inputCacheTokens - (Optional) Cached input tokens count (typically cheaper)
* @property inputCacheDebit - (Optional) Debit pricing for cached input tokens
* @property outputCacheTokens - (Optional) Cached output tokens count (typically cheaper)
* @property outputCacheDebit - (Optional) Debit pricing for cached output tokens
*
* @example
* ```typescript
Expand Down Expand Up @@ -454,4 +464,8 @@ export type AITokenUsagePayload<TTag extends string = string> = {
inputCacheTokens?: number;
/** Debit pricing for cached input tokens (oneof amount, tag, or expr). */
inputCacheDebit?: DebitField<TTag>;
/** Number of cached output tokens used (typically cheaper). */
outputCacheTokens?: number;
/** Debit pricing for cached output tokens (oneof amount, tag, or expr). */
outputCacheDebit?: DebitField<TTag>;
};
81 changes: 81 additions & 0 deletions packages/scrawn/src/gen/event/v1/event.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading