# ai-cost-tracker-pro 🚀💸

Stop burning money on AI APIs without knowing it.

ai-cost-tracker-pro automatically tracks token usage and cost across OpenAI, Anthropic, and Gemini — with zero setup.

[![Powered by Gemini 2.0](https://img.shields.io/badge/Powered%20by-Gemini%202.0-blue?style=for-the-badge&logo=google-gemini)](https://ai-cost-tracker-pro.com)

⚡ Know exactly where your AI budget is going in real time.

---

## ⚡ What You See Instantly

After running:

```bash
npx ai-cost-tracker dev
```

You get:

✔ **Real-time cost updates**  
✔ **Token usage per request**  
✔ **Most expensive API calls**  
✔ **Suggestions to reduce cost**  

### Example Insight:

> **Total Cost Today**: `$3.42`  
> **Most Expensive Model**: `GPT-4`  
> **Suggestion**: Switch 60% traffic → `GPT-4o-mini` → **Save ~$1.10/day**

👉 This is the simplest way to take control of your AI infrastructure.

---

```javascript
import { monitorAI } from "ai-cost-tracker-pro"

// Zero-Config: Just add this at any start of your app
await monitorAI() 
```

### 📦 Key Features
- **Auto-Tracking**: Instantly patches OpenAI, Anthropic, and Gemini (2.0+).
- **Zero Configuration**: Detects API keys from your environment automatically.
- **Local Dashboard**: Visual insights into your spend without leaving your machine.
- **Pro Ready**: Optional cloud sync for team-wide analytics and alerts.

---

## 🎯 Use Cases

- **SaaS apps** using OpenAI / Claude
- **AI agents** & automation tools
- **Chatbots** & copilots
- **Internal tools** with AI APIs
- **Startups** tracking burn rate

🔥 *If you're paying for AI APIs — you need this.*

---

## 🤔 Without vs With ai-cost-tracker

| Without | With |
| :--- | :--- |
| ❌ No idea where money is going | ✅ **Real-time** cost tracking |
| ❌ Surprise bills at end of month | ✅ **Smart suggestions** to save $ |
| ❌ No optimization strategy | ✅ **Full visibility** across models |

---

## Manual Integration (Optional)

If you prefer manual control, use our lightweight wrappers:

### OpenAI
```javascript
import { trackOpenAI } from "ai-cost-tracker-pro"
import OpenAI from "openai"

const client = trackOpenAI(new OpenAI())
```

### Anthropic
```javascript
import { trackAnthropic } from "ai-cost-tracker-pro"
import { Anthropic } from "@anthropic-ai/sdk"

const client = trackAnthropic(new Anthropic())
```

### Google Gemini (2.0+)
Works with both the standard and the newest Gemini SDKs:

**New SDK (`@google/genai`):**
```javascript
import { trackGenAI } from "ai-cost-tracker-pro"
import { GoogleGenAI } from "@google/genai"

const client = trackGenAI(new GoogleGenAI({ apiKey: "YOUR_KEY" }))
```

**Standard SDK (`@google/generative-ai`):**
```javascript
import { trackGemini } from "ai-cost-tracker-pro"
import { GoogleGenerativeAI } from "@google/generative-ai"

const genAI = new GoogleGenerativeAI("YOUR_KEY")
const model = trackGemini(genAI.getGenerativeModel({ model: "gemini-1.5-flash" }))
```

---

## 📊 Powerful Local Dashboard

Visualize your AI spending instantly. The dashboard reads from the `.ai-usage.json` file generated in your project root.

### How to use:
1. **Generate Data**: Run your AI project at least once (using `monitorAI()` or manual wrappers).
2. **Launch**:
   ```bash
   npx ai-cost-tracker dev
   ```
3. **Analyze**: Open the provided local URL (usually `http://localhost:3000`) to see:
   - **Real-time Cost Charts**: Watch your budget in a live timeline.
   - **Model Breakdown**: See which models (`gpt-4`, `gemini-pro`, etc.) are most expensive.
   - **Cost Optimization**: Get automatic advice on switching models to save money.

---

## 💻 CLI Reports
If you prefer the terminal, you can get quick summaries without leaving your shell:

| Command | Description |
| --- | --- |
| `npx ai-cost-tracker report` | Print a detailed usage table in your terminal. |
| `npx ai-cost-tracker dev` | **(Recommended)** Launch the interactive local web dashboard. |
| `npx ai-cost-tracker upgrade` | View Pro features and visit the [Live Cloud Dashboard](https://ai-monitor-dash.vercel.app). |

---

## 🚀 Show Off Your Efficiency

Show your users (and investors) you care about efficiency! Run `npx ai-cost-tracker-pro init-readme` to add this badge to your project:

[![AI Cost Tracked](https://img.shields.io/badge/AI_Cost-Tracked-10b981?style=for-the-badge&logo=openai)](https://ai-cost-tracker-pro.com)

👉 **Projects with this badge:**
- Signal cost awareness
- Build trust with users
- Stand out to investors

---

## Free vs Pro Features

| Feature | Free | Pro |
| --- | --- | --- |
| Local Tracking | ✅ | ✅ |
| CLI Reports | ✅ | ✅ |
| Context-Aware (User/Team) | ✅ | ✅ |
| Cloud Sync | ❌ | ✅ |
| Cost Alerts | ❌ | ✅ |
| Real-Time Hooks | ❌ | ✅ |
| Team Analytics | ❌ | ✅ |

## 🚀 Why Upgrade?

If you're building anything serious with AI:

- **You NEED cost alerts** before bills explode.
- **You NEED team-level visibility** to track usage across developers.
- **You NEED historical tracking** to spot trends and optimize.

👉 **Free** is for individuals  
👉 **Pro** is for real products

---

## Configuration (Pro)

To enable Pro features, configure the tracker with your API key:

```javascript
import { configureAITracker } from "ai-cost-tracker-pro"

configureAITracker({
  apiKey: "your_pro_api_key",
  endpoint: "https://your-saas-backend.com/api/v1/usage"
})
```

### Environment Variables (Optional)

- `OPENAI_API_KEY`: Automatically used if present.
- `AI_TRACKER_API_KEY` (PRO): Your SaaS API Key for cloud sync.
- `AI_TRACKER_ENDPOINT` (PRO): Custom SaaS endpoint (defaults to cloud).

---

## Real-Time Hooks (Pro)

```javascript
import { onUsage } from "ai-cost-tracker-pro"

onUsage((data) => {
  console.log(`Model ${data.model} used ${data.totalTokens} tokens. Cost: $${data.cost}`)
})
```

---

### 🚀 Roadmap
- [x] Zero-Config Mode
- [x] Model Recommendations
- [x] Local Dashboard
- [x] SaaS Cloud Sync
- [x] Stripe Billing
- [x] Viral growth features
- [ ] Mobile App (Coming Soon)

---

## 🚀 Quick Start (Testing in a new project)
Want to see it in action in under 60 seconds?

1. **Install**: `npm install ai-cost-tracker-pro @google/genai tsx`
2. **Setup**: Add your `GEMINI_API_KEY` to your `.env`.
3. **Run AI Call**:
```typescript
import { monitorAI, tracker } from 'ai-cost-tracker-pro';
import { GoogleGenAI } from '@google/genai';

await monitorAI();

const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
await ai.models.generateContent({ 
  model: 'gemini-1.5-flash', 
  contents: [{role: 'user', parts: [{text: 'Hi!'}]}] 
});

console.log('Usage Recorded!');
```
4. **Launch Dashboard**: `npx ai-cost-tracker dev` and visit `http://localhost:3001`.

👉 *Stop guessing. Start measuring.*
