<div align="center">

# 🏰 AI meter

[![npm version](https://img.shields.io/npm/v/ai-meter.svg)](https://www.npmjs.com/package/ai-meter)
[![License: ISC](https://img.shields.io/badge/License-ISC-blue.svg)](https://opensource.org/licenses/ISC)
[![TypeScript](https://img.shields.io/badge/TypeScript-Ready-blue.svg)](https://www.typescriptlang.org/)

</div>

AI Meter is a JavaScript library designed to estimate AI models tokens and API costs. It provides a simple and efficient way to calculate token usage and associated costs for various AI models.

You can test it at <a href="https://www.agentstower.com/calculator" target="_blank" rel="noopener noreferrer">AgentsTower Calculator</a>


## ✨ Features

<table>
<tr>
<td width="50%">

- 🎯 **Calculate Tokens**
  - Accurate token estimation for various AI models
  - Support for different tokenization methods
  - Handles both input and output tokens

- 🔄 **Cost Calculation**
  - Real-time cost estimation
  - Support for multiple pricing models
  - Currency conversion support

</td>
<td width="50%">

- 🛠 **Developer Friendly**
  - Free and easy-to-use API
  - Use directly with vanilla JavaScript
  - Comprehensive documentation
  - Built-in error handling

- 📊 **Extensible**
  - Easy integration with existing projects
  - Add more tokenizer algorithms
  - Fork to add more functionality 

</td>
</tr>
</table>


## 🚀 Installation

```bash
npm install ai-meter
# or
yarn add ai-meter
# or
pnpm add ai-meter
```


## 📖 Quick Start

```javascript
import AiMeter from 'ai-meter';

// Get tokens, costs and cost details with one request (example)
const metrics = await AiMeter.metrics('gpt-4o', 'Hello, world!', 'input');

// View response (example)
console.log(metrics);
// {
//   "tokens": 4,
//   "cost": 0.00001,
//   "cost_details": {
//     "pricing_mode": "standard",
//     "cost_per_input_unit": "0.000002500000000",
//     "cost_per_cached_input_unit": "0.000001250000000",
//     "cost_per_output_unit": "0.000010000000000",
//     "last_verified_at": "2025-06-07T05:49:08.000000Z"
//   }
// }
```

## 🛠 Development

### Prerequisites

- Declare your script as a module (docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#applying_the_module_to_your_html)


## 🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request


## 📝 License

This project is licensed under the ISC License - see the [LICENSE](LICENSE) file for details.

---


<div align="center">

Built with ❤️ by [Vista Platforms](https://github.com/vistaplatforms)

</div>
