Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gpt-tokens

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gpt-tokens

Calculate the token consumption and amount of openai gpt message

  • 1.0.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.3K
decreased by-34.89%
Maintainers
1
Weekly downloads
 
Created
Source

gpt-tokens

TypeScript

GPT tokens / price Calculate

Install

# npm or yarn

npm install gpt-tokens
yarn add gpt-tokens

Usage

import { GPTTokens } from 'gpt-tokens'

const usageInfo = new GPTTokens({
    model: 'gpt-3.5-turbo',
    messages: [
        {
            'role'   : 'system',
            'content': 'You are a helpful assistant',
        },
        {
            'role'   : 'user',
            'content': '',
        },
    ]
})

// Tokens: 18
console.log('Tokens:', usageInfo.usedTokens)
// Price USD: 0.000036
console.log('Price USD: ', usageInfo.usedUSD)

You can test in Tiktokenizer

img.png

Support Models

  • gpt-3.5-turbo
  • gpt-3.5-turbo-0301
  • gpt-4
  • gpt-4-0314
  • gpt-4-32k
  • gpt-4-32k-0314

For web

If use vite, you need do this in vite.config.js

import wasm             from 'vite-plugin-wasm'
import topLevelAwait    from 'vite-plugin-top-level-await'
import { defineConfig } from 'vite'

export default defineConfig({
    plugins: [wasm(), topLevelAwait()],
})

Compatibility

As the dependency @dqbd/tiktoken is a WASM library, you can find the compatibility methods in the NPM Readme of @dqbd/titoken.

Dependencies

Keywords

FAQs

Package last updated on 26 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc