Skip to content

Commit

Permalink
fix: cached llm cost (#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
hughcrt authored Feb 4, 2025
1 parent 7d4f61e commit 98eca6d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/backend/src/api/v1/runs/ingest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ async function registerRunEvent(
promptTokens: tokensUsage?.prompt,
completionTokens: tokensUsage?.completion,
name: runData?.name,
duration: +timestamp - +runData?.createdAt,
duration: new Date(timestamp) - new Date(runData?.createdAt),
projectId,
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/utils/calcCost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export function calcRunCostLegacy(run: any) {
}

export async function calcRunCost(run: any) {
if (run.duration && run.duration < 0.01 * 1000) return null; // cached llm calls
if (!run.duration || run.duration < 0.01 * 1000) return null; // cached llm calls
if (run.type !== "llm" || !run.name) return null;

// Look at table model_mapping, in this logic:
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/components/blocks/DurationBadge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function DurationBadge({
? new Date(endedAt).getTime() - new Date(createdAt).getTime()
: NaN;

if (cached) {
if (cached || duration < 0.01 * 1000) {
return (
<Badge
variant="light"
Expand Down
11 changes: 11 additions & 0 deletions packages/frontend/utils/datatable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ export function durationColumn(unit = "s"): ColumnDef<any> {
enableSorting: true,
cell: (props) => {
const value = props?.getValue() || 0;
const duration =
new Date(props.row.original.endedAt) -
new Date(props.row.original.createdAt);
const metadata = props.row.original.metadata?.cache;

console.log(duration);
const isCached = metadata?.cached || duration < 0.01 * 1000;

if (isCached) {
return "Cached";
}

if (value === 0) {
return "0.00s";
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/schemas/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export type Feedback = z.infer<typeof Feedback>;
export const Score = z.object({
label: z.string(),
value: z.union([z.number(), z.string(), z.boolean()]),
comment: z.string().nullable(),
comment: z.string().nullable().optional(),
});
export type Score = z.infer<typeof Scor>;
export type Score = z.infer<typeof Score>;

export interface Run {
id: string;
Expand Down

0 comments on commit 98eca6d

Please sign in to comment.