openai token usage_nehaguptadev

How to Track OpenAI Token Usage Per User (Complete Guide)

When building applications with OpenAI (like chatbots, document Q&A, or APIs), tracking token usage per user is critical for:

  • 💰 Cost control
  • 📊 Usage analytics
  • 🔐 Fair usage / rate limiting
  • 🧾 Billing users accurately

In this blog, you’ll learn how to track, store, and monitor token usage per user step by step.


🧠 What Are Tokens?

Tokens are the basic units of text used by OpenAI models.

  • 1 token ≈ 4 characters (roughly)
  • Both input (prompt) and output (response) tokens are counted

👉 Total cost = Input tokens + Output tokens


📦 1. Get Token Usage from OpenAI Response

Every OpenAI API response includes a usage field.

Example Response:

{
  "usage": {
    "prompt_tokens": 120,
    "completion_tokens": 80,
    "total_tokens": 200
  }
}

Extract in Python:

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello"}]
)

usage = response.usage

prompt_tokens = usage.prompt_tokens
completion_tokens = usage.completion_tokens
total_tokens = usage.total_tokens

👤 2. Associate Usage with a User

To track per user, you must have a user identifier:

  • user_id (from your database)
  • email
  • session_id

Example:

user_id = "user_123"

🗄️ 3. Store Token Usage in Database

Design a simple table:

Example Table: token_usage

iduser_idprompt_tokenscompletion_tokenstotal_tokenstimestamp

Insert Data (Python Example):

import datetime

db.insert({
    "user_id": user_id,
    "prompt_tokens": prompt_tokens,
    "completion_tokens": completion_tokens,
    "total_tokens": total_tokens,
    "timestamp": datetime.datetime.utcnow()
})

📊 4. Calculate Total Usage Per User

SQL Example:

SELECT 
    user_id,
    SUM(total_tokens) as total_tokens_used
FROM token_usage
GROUP BY user_id;

Monthly Usage:

SELECT 
    user_id,
    SUM(total_tokens) as monthly_usage
FROM token_usage
WHERE timestamp >= DATE_TRUNC('month', CURRENT_DATE)
GROUP BY user_id;

💰 5. Convert Tokens to Cost

Each OpenAI model has different pricing.

Example (illustrative):

  • Input: $0.001 / 1K tokens
  • Output: $0.002 / 1K tokens

Cost Calculation:

input_cost = prompt_tokens / 1000 * 0.001
output_cost = completion_tokens / 1000 * 0.002

total_cost = input_cost + output_cost

⚡ 6. Middleware Approach (Best Practice)

Instead of tracking everywhere, centralize logic.

Example Wrapper Function:

def call_openai(user_id, messages):
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=messages
    )

    usage = response.usage

    save_usage(
        user_id=user_id,
        prompt=usage.prompt_tokens,
        completion=usage.completion_tokens,
        total=usage.total_tokens
    )

    return response

👉 This ensures every API call is tracked automatically


📈 7. Build a Dashboard (Optional but Powerful)

Track:

  • Tokens per user
  • Cost per user
  • Daily / monthly usage
  • Top users

Tools you can use:

  • Power BI
  • Grafana
  • Custom admin panel

🚨 8. Add Usage Limits Per User

Prevent abuse or overspending.

Example:

MAX_TOKENS = 100000

if get_user_total_tokens(user_id) > MAX_TOKENS:
    raise Exception("Usage limit exceeded")

🔐 9. Best Practices

  • Track every request
  • Store timestamps for analytics
  • Separate input/output tokens
  • Cache frequent queries to reduce tokens
  • Monitor unusual spikes

🎯 Final Thoughts

Tracking token usage per user is not just about cost—it’s about control, scalability, and insights.

With a simple setup:

  1. Capture usage from API response
  2. Store it with user_id
  3. Aggregate and monitor

You can build a production-grade system that scales efficiently.


    Leave a Comment

    Your email address will not be published. Required fields are marked *