DEV Community

serkan
serkan

Posted on

How to track OpenAI API costs with a simple proxy

The problem

You're building with the OpenAI API and suddenly get a $200 bill. Which feature caused it? Which user? Which prompt? You have no idea.

This happens to almost every developer building with LLMs.

The solution

I built LLMWatch – a lightweight proxy that sits between your app and OpenAI. It logs every request with exact cost, latency, and token usage.

How it works

Change one line in your code:

// Before
const openai = new OpenAI({ 
  baseURL: "https://api.openai.com" 
})

// After
const openai = new OpenAI({ 
  baseURL: "https://llmwatch-rho.vercel.app/api/proxy",
  defaultHeaders: {
    "x-llmwatch-key": "your_llmwatch_key"
  }
})
Enter fullscreen mode Exit fullscreen mode

That's it. Every request is now logged.

What you get

  • Exact cost per request – See which prompt costs $0.001 vs $0.05
  • Token breakdown – Prompt tokens vs completion tokens
  • Latency tracking – Which requests are slow?
  • Cost alerts – Get an email when you hit 80% of your monthly budget

Getting started

  1. Sign up at llmwatch-rho.vercel.app
  2. Create a project and copy your API key
  3. Change your baseURL
  4. Done

Free tier: 1,000 requests/month. Pro plan: $20/month unlimited.

Would love feedback from anyone building with LLMs!

Top comments (0)