Welcome to Portkey Forum

Updated 2 months ago

Cost calculations on the logger endpoint

Hey that worked. But I missed the cost calculation

Plain Text
const apiResponse = await fetch(apiUrl, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "x-portkey-api-key": process.env.PORTKEY_API_KEY,
        "x-portkey-provider": "cerebras",
        "x-portkey-trace-id": metadata.traceID,
        "x-portkey-metadata": JSON.stringify(metadata),
      },
      body: JSON.stringify(logData),
    });

This the code snippet I'm using. I'm passing the provider slug in the header, but it is not registering in the log and not calculating the cost even though the tokens and model info is available.
Attachment
image.png
V
W
d
9 comments
cc @Witherwings who can take a look here and suggest what to do!
hey @darkprince currently we do not have price calculation support for cerebras. We will be adding the support in some time
also you can send logs in bulk at one go. I would suggest to pass metadata for custom logs inside the body object and not via headers. we treat custom log metadata a bit separately and have more options specifically in body
Okay Cool. Can do that. Is there a documentation i can follow for this. The doc for the custom logs is not complete i guess. Most of the things i got by guess work and trial and error.
Plain Text
async function logPortkeyRequest(request, response, metadata) {
  const apiUrl = "https://api.portkey.ai/v1/logs";

  const logData = {
    request,
    response,
  };

  try {
    const apiResponse = await fetch(apiUrl, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "x-portkey-api-key": process.env.PORTKEY_API_KEY,
        "x-portkey-provider": "cerebras",
        "x-portkey-trace-id": metadata.traceID,
        "x-portkey-metadata": JSON.stringify(metadata),
      },
      body: JSON.stringify(logData),
    });

    if (!apiResponse.ok) {
      console.error("Failed to log request:", apiResponse.statusText);
    }
  } catch (error) {
    console.error("Error logging request:", error);
    Sentry.captureException(error);
  }
}


This is the full code snippet that logs to portkey. Let me know what are the other improvements i can do.
Plain Text
async function logPortkeyRequest(request, response, metadata) {
  const apiUrl = "https://api.portkey.ai/v1/logs";

  const logData = {
    request,
    response,
    metadata
  };

  try {
    const apiResponse = await fetch(apiUrl, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "x-portkey-api-key": process.env.PORTKEY_API_KEY,
        "x-portkey-provider": "cerebras",
        "x-portkey-trace-id": metadata.traceID,
      },
      body: JSON.stringify(logData),
    });

    if (!apiResponse.ok) {
      console.error("Failed to log request:", apiResponse.statusText);
    }
  } catch (error) {
    console.error("Error logging request:", error);
    Sentry.captureException(error);
  }
}
Thanks @Witherwings
Add a reply
Sign up and join the conversation on Discord