Skip to main content

Claude Code - Granular Cost Tracking

Track Claude Code spend by customer organization and member with Customer Management. Add tags to group costs by project or environment.

Since v1.111.0

Set up customer tracking​

Start with a gateway on v1.111 or later, PostgreSQL with migrations applied, and an application key that can call your configured Claude model. Use your existing LITELLM_BASE_URL and LITELLM_API_KEY environment values. Keep the gateway URL at its root, without /v1.

Set ANTHROPIC_CUSTOM_HEADERS with one header per line:

export ANTHROPIC_BASE_URL="$LITELLM_BASE_URL"
export ANTHROPIC_API_KEY="$LITELLM_API_KEY"
export ANTHROPIC_CUSTOM_HEADERS="x-litellm-customer-org-alias: Acme
x-litellm-customer-member-id: acme-alice"
claude

Replace Acme and acme-alice with your customer's organization alias and stable member ID. Use a member ID that is unique across the gateway. LiteLLM registers new identities under the default team, subject to capacity limits. Create budgets first if you need customer caps from the first request.

Send a prompt in Claude Code. Then open Customer Management → Customers → Acme, select a date range that includes the request, and check the member's spend, requests, and tokens. Allow for background accounting. See request details to inspect the resolved customer and cost in Logs.

Add project or environment tags​

Append x-litellm-tags to the same variable. Keep the two customer headers to attribute the request to a customer too:

export ANTHROPIC_CUSTOM_HEADERS="x-litellm-customer-org-alias: Acme
x-litellm-customer-member-id: acme-alice
x-litellm-tags: project:support,env:prod"

You can also send the tags header on its own. See tag budgets for spending limits on tags.

Legacy end-user tracking​

These examples remain supported for the flat /customer/* APIs and the End User log field. Use the setup above for Customer Management. The screenshots below show the legacy tracking flow.

How It Works​

Claude Code supports custom headers via ANTHROPIC_CUSTOM_HEADERS. LiteLLM automatically tracks requests with specific headers for cost attribution.

Why Set a Customer Header​

Without one, Claude Code still reaches customer tracking on its own. It sends a per-session JSON blob in metadata.user_id, which LiteLLM reads as a customer ID, so every session shows up as a separate customer and picks up its own copy of any default customer budget you have configured. Setting x-litellm-customer-id overrides that, since headers are checked before any request body field. If you would rather have that traffic carry no customer at all, see Restricting Which IDs Become Customers.

Tracking Options​

Choose how you want to attribute costs:

Track ByHeaderUse Case
Customerx-litellm-customer-idBill customers, per-user budgets
Tagsx-litellm-tagsProject tracking, cost centers, environments

Environment Variables​

VariableDescriptionExample
ANTHROPIC_BASE_URLLiteLLM proxy URLhttp://localhost:4000
ANTHROPIC_API_KEYLiteLLM API keysk-<your-api-key>
ANTHROPIC_CUSTOM_HEADERSCustom headers (header-name: value format)See examples below

Option 1: Track by Customer​

Use this to attribute costs to specific customers or end-users.

export ANTHROPIC_BASE_URL=http://localhost:4000
export ANTHROPIC_API_KEY=sk-<your-api-key>
export ANTHROPIC_CUSTOM_HEADERS="x-litellm-customer-id: claude-ishaan-local"

Option 2: Track by Tags​

Use this to attribute costs to projects, cost centers, or environments. Pass comma-separated tags.

export ANTHROPIC_BASE_URL=http://localhost:4000
export ANTHROPIC_API_KEY=sk-<your-api-key>
export ANTHROPIC_CUSTOM_HEADERS="x-litellm-tags: project:acme,env:prod,team:backend"

Quick Start​

1. Set Environment Variables​

export ANTHROPIC_BASE_URL=http://localhost:4000
export ANTHROPIC_API_KEY=sk-<your-api-key>
export ANTHROPIC_CUSTOM_HEADERS="x-litellm-customer-id: claude-ishaan-local"

2. Use Claude Code​

claude

All requests will now be tracked under the customer ID claude-ishaan-local.

3. View Usage in LiteLLM UI​

Navigate to the Logs tab in the LiteLLM UI.

Click on a request to see details.

Filter by customer ID to see all requests for that customer.

Supported Headers​

HeaderDescription
x-litellm-customer-idTrack by customer/end-user ID
x-litellm-end-user-idAlternative customer ID header
x-litellm-tagsComma-separated tags for cost attribution