Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cost information before openai requests #17

Open
agamm opened this issue Oct 4, 2023 · 1 comment
Open

Add cost information before openai requests #17

agamm opened this issue Oct 4, 2023 · 1 comment

Comments

@agamm
Copy link
Contributor

agamm commented Oct 4, 2023

https://www.npmjs.com/package/openai-cost-calculator

This should be added after/with: #11

@elie222
Copy link
Contributor

elie222 commented Oct 4, 2023

If we want to log the actual cost after its been completed then this is some code to help:

// returns cost in cents
function calcuateCost(
  model: AIModel,
  usage: {
    prompt_tokens: number;
    completion_tokens: number;
  }
): number {
  let costPerCompletionToken: number;
  let costPerPromptToken: number;

  if (model === "gpt-4") {
    costPerCompletionToken = 0.003;
    costPerPromptToken = 0.006;
  } else {
    costPerCompletionToken = 0.00015;
    costPerPromptToken = 0.0002;
  }

  return (
    usage.completion_tokens * costPerCompletionToken +
    usage.prompt_tokens * costPerPromptToken
  );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants