How-to Guides

How do I tag my first AI call for cost attribution?

Add 2 headers to your existing OpenAI or Anthropic call and see spend broken down by feature and team in under 5 minutes.

Goal: Go from a single unattributed AI spend total to a per-feature, per-team cost breakdown — by adding 2 lines of code.

Time: 5 minutes.


Step 1 — Find where you create your AI client

Search your codebase for where you initialise your OpenAI or Anthropic client. It's usually in one place — a shared utility file, a service class, or a config module.

# What to search for:
OpenAI(           ← Python
new OpenAI(       ← TypeScript/Node.js
anthropic.Anthropic(

You only need to change one place. If you have the client in a shared file, tagging there covers every call in your app.

Step 2 — Add the 2 headers

Add default_headers when you create the client. Pick names that describe your feature and team — use lowercase with hyphens.

Multiple features or services?

Create one client per service, each with its own X-Cost-Feature tag. A SupportService gets feature: support-chat. A SearchService gets feature: search. This gives you automatic per-service cost breakdown with zero per-call overhead.

Step 3 — Make a call and check the dashboard

Make any API call through the updated client — your existing code works without any other changes. Then open your Cognocient dashboard.

Within a few seconds you'll see:

  • Spend by Feature chart — your feature name appears as its own slice
  • Spend by Department chart — your team appears with its cost
  • Live Calls tab — your call listed with the feature and department tags

That's it. Every subsequent call from this client is automatically attributed.


What to do next

Once the first tag is working, layer in more context:

  • Add "X-Cost-User": f"user_{user_id}" to track per-customer cost
  • Add "X-Cost-Session": session_id to group multi-turn conversations
  • Add "X-Cost-GL-Account": "5100" for accounting integration

See Attribution Headers for the full guide.

On this page