MCPcopy Index your code
hub / github.com/OpenPipe/OpenPipe / recordUsage

Function recordUsage

app/src/utils/recordRequest.ts:22–96  ·  view source on GitHub ↗
({
  projectId,
  requestedAt,
  receivedAt,
  cacheHit,
  inputPayload,
  completion,
  logRequest,
  fineTune,
  tags,
  ongoingRequestId,
}: {
  projectId: string;
  requestedAt: number;
  receivedAt: number;
  cacheHit: boolean;
  inputPayload: z.infer<typeof chatCompletionInput>;
  completion: unknown;
  logRequest?: boolean;
  fineTune?: FineTune;
  tags?: Record<string, string>;
  ongoingRequestId?: string;
})

Source from the content-addressed store, hash-verified

20import { recordOngoingRequestEnd } from "./rateLimit/concurrencyRateLimits";
21
22export const recordUsage = async ({
23 projectId,
24 requestedAt,
25 receivedAt,
26 cacheHit,
27 inputPayload,
28 completion,
29 logRequest,
30 fineTune,
31 tags,
32 ongoingRequestId,
33}: {
34 projectId: string;
35 requestedAt: number;
36 receivedAt: number;
37 cacheHit: boolean;
38 inputPayload: z.infer<typeof chatCompletionInput>;
39 completion: unknown;
40 logRequest?: boolean;
41 fineTune?: FineTune;
42 tags?: Record<string, string>;
43 ongoingRequestId?: string;
44}) => {
45 if (completion instanceof Stream) {
46 let merged: ChatCompletion | null = null;
47 for await (const chunk of completion) {
48 merged = mergeChunks(merged, chunk as ChatCompletionChunk);
49 }
50 completion = merged;
51 }
52
53 void recordOngoingRequestEnd(ongoingRequestId);
54
55 const parsedCompletion = chatCompletionOutput.safeParse(completion);
56 const usage = parsedCompletion.success
57 ? calculateUsage({
58 inputPayload,
59 completion: parsedCompletion.data,
60 fineTune,
61 })
62 : undefined;
63
64 if (fineTune) {
65 await prisma.usageLog
66 .create({
67 data: {
68 fineTuneId: fineTune.id,
69 projectId: fineTune.projectId,
70 baseModel: fineTune.baseModel,
71 type: cacheHit ? UsageType.CACHE_HIT : UsageType.EXTERNAL,
72 inputTokens: usage?.inputTokens ?? 0,
73 outputTokens: usage?.outputTokens ?? 0,
74 cost: cacheHit ? 0 : usage?.cost ?? 0,
75 inputCost: cacheHit ? 0 : usage?.inputCost ?? 0,
76 outputCost: cacheHit ? 0 : usage?.outputCost ?? 0,
77 billable: fineTune.provider === "openpipe",
78 },
79 })

Calls 6

recordOngoingRequestEndFunction · 0.90
mergeChunksFunction · 0.85
calculateUsageFunction · 0.85
recordLoggedCallFunction · 0.85
catchMethod · 0.45
createMethod · 0.45

Tested by

no test coverage detected