---
title: "LLM usage logging - cost per user and feature, no PII"
description: "Log every LLM API call with tokens, cost, latency and request ID, attribute spend to users and features, and redact PII before it is written. Python code."
url: https://broadrouter.com/blog/llm-api-usage-logging/
lang: en
updated: 2026-11-14
publisher: BroadRouter (a BroadNet site); API by BroadNet Inference, https://inference.broadnet.ai
---

# LLM API usage logging: cost per user and feature, PII redacted

By the BroadNet Inference team · 4 min read · Published 2026-11-14

Wrap every LLM call in one function that records model, feature, a hashed user ID, prompt, cached, reasoning and output tokens, cost, latency, status and the provider request ID. Compute cost from a price table, not the invoice. Log metadata by default; if you must keep prompt text, redact it before writing and set a short retention.

## Key takeaways

-   One wrapper, one log line per call: model, feature, hashed user, token counts, cost, latency, status, request ID.
-   Compute cost per call from the usage block and a price table: new input, cached input and output have different prices.
-   Hash user IDs with a keyed HMAC so you can group by user without storing who they are.
-   Log metadata by default. Prompt text is the main PII risk; redact before writing, never after.
-   Regex and NER redaction both miss things. Treat them as a layer, not a guarantee.

**On this page**

1.  What should you log for each LLM call?
2.  How do you compute cost per call?
3.  What does the logging wrapper look like?
4.  How do you attribute cost to users and features?
5.  How do you keep PII out of LLM logs?
6.  Where BroadNet Inference fits

Provider dashboards tell you what you spent. They rarely tell you **which feature** or **which customer** spent it, and they cannot tell you why a bill jumped on Tuesday. Your own usage log can. This post shows the log schema, a wrapper that fills it, how to compute cost per call, and how to keep personal data out. What to do with the numbers is covered elsewhere: [cost per user and SaaS pricing](https://broadrouter.com/blog/llm-cost-per-user-saas/) and [budgets and loop guards](https://broadrouter.com/blog/llm-api-spend-control/).

## What should you log for each LLM call?

One row per attempt, retries included:

| Field | Example | Why |
| --- | --- | --- |
| `ts` | 2026-11-14T09:12:03Z | Time series, incident timelines |
| `feature` | `support_reply` | Cost per feature |
| `user_hash` | `hmac:4f1c…` | Cost per user without storing identity |
| `tenant` | `acme` | Cost per customer in B2B apps |
| `model` | broadnet/deepseek-v4.1-flash | Price lookup, model comparisons |
| `prompt_tokens` | 18,240 | Input cost |
| `cached_tokens` | 16,384 | Cached input is cheaper; track the [cache rate](https://broadrouter.com/blog/cached-input-tokens-explained/) |
| `reasoning_tokens` | 612 | Hidden output cost on reasoning models |
| `completion_tokens` | 1,104 | Output cost (includes reasoning) |
| `cost_usd` | computed | The number finance asks for |
| `latency_ms`, `ttft_ms` | 4,210 / 690 | Performance regressions |
| `status` | 200, 429, 503 | Error and retry rates |
| `request_id` | `X-Request-ID` value | Support tickets and invoice disputes |
| `prompt_version` | `support-v14` | Ties cost changes to prompt changes |

No prompt text, no response text. Those come later, and only with redaction.

## How do you compute cost per call?

Read the usage block and price each [token](https://broadrouter.com/glossary/input-and-output-tokens/) type separately. BroadNet publishes machine-readable prices in [pricing.json](https://inference.broadnet.ai/pricing.json?utm_source=broadrouter&utm_medium=referral&utm_campaign=blog-llm-api-usage-logging&utm_content=body), with per-1M prices for input, cached input and output per model:

```python
import json, urllib.request

PRICES = {m["id"]: m for m in json.load(urllib.request.urlopen("https://inference.broadnet.ai/pricing.json"))["models"]}

def call_cost(model: str, usage) -> float:
    p = PRICES[model]
    details = getattr(usage, "prompt_tokens_details", None)
    cached = (getattr(details, "cached_tokens", 0) or 0) if details else 0
    new_input = usage.prompt_tokens - cached
    return (
        new_input * (p.get("input_per_m") or 0)
        + cached * (p.get("cached_input_per_m") or p.get("input_per_m") or 0)
        + usage.completion_tokens * (p.get("output_per_m") or 0)
    ) / 1_000_000
```

Cache the price file and refresh it daily rather than fetching it on every call. Two easy mistakes: pricing cached tokens as new input (overstates cost), and forgetting that reasoning tokens are already inside `completion_tokens` (do not add them twice).

## What does the logging wrapper look like?

```python
import hashlib, hmac, os, time, logging
from openai import OpenAI

log = logging.getLogger("llm.usage")
client = OpenAI(base_url="https://inference.broadnet.ai/v1", api_key=os.environ["BROADNET_API_KEY"])
HASH_KEY = os.environ["USAGE_HASH_KEY"].encode()

def user_hash(user_id: str) -> str:
    return "hmac:" + hmac.new(HASH_KEY, user_id.encode(), hashlib.sha256).hexdigest()[:16]

def chat(*, feature: str, user_id: str, prompt_version: str, **kwargs):
    t0 = time.perf_counter()
    row = {"feature": feature, "user_hash": user_hash(user_id),
           "model": kwargs["model"], "prompt_version": prompt_version}
    try:
        raw = client.chat.completions.with_raw_response.create(**kwargs)
        resp = raw.parse()
        u = resp.usage
        d = getattr(u, "completion_tokens_details", None)
        row.update(
            status=200,
            request_id=raw.headers.get("x-request-id"),
            prompt_tokens=u.prompt_tokens,
            cached_tokens=getattr(getattr(u, "prompt_tokens_details", None), "cached_tokens", 0) or 0,
            reasoning_tokens=(getattr(d, "reasoning_tokens", 0) or 0) if d else 0,
            completion_tokens=u.completion_tokens,
            cost_usd=round(call_cost(kwargs["model"], u), 6),
        )
        return resp
    except Exception as e:
        row.update(status=getattr(e, "status_code", "error"),
                   request_id=getattr(getattr(e, "response", None), "headers", {}).get("x-request-id"))
        raise
    finally:
        row["latency_ms"] = int((time.perf_counter() - t0) * 1000)
        log.info("llm_call", extra={"llm": row})
```

`with_raw_response` gives you the headers, so you can keep the `X-Request-ID`. For streamed calls, add `stream_options={"include_usage": True}` and fill the row from the final chunk, as shown in [the SSE streaming post](https://broadrouter.com/blog/llm-streaming-sse-explained/). Keep `HASH_KEY` secret and stable: the same user always maps to the same hash, but the hash cannot be reversed without the key.

## How do you attribute cost to users and features?

With the rows above in any SQL store, attribution is one query:

```sql
SELECT feature, model,
       count(*)                          AS calls,
       sum(cost_usd)                     AS cost,
       sum(cached_tokens)::float / nullif(sum(prompt_tokens), 0) AS cache_rate,
       avg(completion_tokens)            AS avg_out
FROM llm_calls
WHERE ts >= now() - interval '7 days'
GROUP BY feature, model
ORDER BY cost DESC;
```

Swap `feature` for `user_hash` or `tenant` for per-customer cost. Three follow-ups pay off fast:

1.  **Top 1% of users by cost.** Often a single loop or a bot. A falling `cache_rate` is the other early warning; the [prompt caching calculator](https://broadrouter.com/tools/prompt-caching-calculator/) shows what each point of cache rate is worth.
2.  **Cost per successful outcome**, not per call: join to your own success events.
3.  **Retry share.** Rows with status 429 or 5xx show how much you spend on failures; see [handling 429 errors](https://broadrouter.com/blog/handle-llm-api-429-rate-limits/).

## How do you keep PII out of LLM logs?

The safest log has no prompt text at all. When you need text, for debugging or evals, follow these steps in order:

1.  **Sample.** Keep text for a small share of calls and for failures, not for everything.
2.  **Redact before writing.** Run redaction in the process that writes the log. Redacting later means the raw text already sits on disk and in backups.
3.  **Layer the detectors.** Regex for structured data, an NER tool for names and addresses.
4.  **Store separately.** Put text in a different table or bucket with stricter access and a short retention, linked by `request_id`.
5.  **Never log secrets.** The OWASP logging cheat sheet lists access tokens, passwords and encryption keys among data to exclude; the [API key](https://broadrouter.com/glossary/api-key/) belongs on that list.

A minimal regex layer:

```python
import re
PATTERNS = [
    (re.compile(r"[\w.+-]+@[\w-]+\.[\w.-]+"), "[EMAIL]"),
    (re.compile(r"\+?\d[\d\s().-]{7,}\d"), "[PHONE]"),
    (re.compile(r"\b(?:\d[ -]?){13,19}\b"), "[CARD]"),
    (re.compile(r"\b(sk|key|bearer)[-_ ]?[A-Za-z0-9]{16,}\b", re.I), "[SECRET]"),
]
def redact(text: str) -> str:
    for pat, label in PATTERNS:
        text = pat.sub(label, text)
    return text
```

This catches the obvious cases and misses names, addresses and anything unusual. [Microsoft Presidio](https://github.com/microsoft/presidio) adds entity recognition for those, and its README says plainly that there is no guarantee it will find all sensitive information. Plan for misses: short retention and tight access limit the damage when one gets through.

## Where BroadNet Inference fits

BroadNet Inference returns the usage block with cached and reasoning tokens when the model reports them, an `X-Request-ID` on every response, and a public price file you can load for cost computation. On its side, prompts and answers are processed for the request and not retained; it keeps only content-free records such as timings, token counts and cost ([how zero data retention works](https://broadrouter.com/zero-data-retention/)). That makes your own logs the only copy of any prompt text, so the redaction rules above decide your real retention. BroadNet’s chat requests have no per-user metadata field, so attribution lives in your wrapper. For separate budgets per team, use separate keys; the [keys guide](https://broadrouter.com/guides/api-keys-ip-allow-lists/) covers labels and allow-lists.

Free tools for this

-   [OpenAI to BroadNet migration helper](https://broadrouter.com/tools/openai-to-broadnet-migration/) Paste OpenAI SDK code; get base URL, key and model edits, a diff and the checks that remain.
-   [max\_tokens calculator for reasoning models](https://broadrouter.com/tools/reasoning-token-budget/) Answer length + reasoning budget + margin = a max\_tokens that does not cut the answer off.
-   [JSON schema generator for LLM structured output](https://broadrouter.com/tools/json-schema-generator/) Sample JSON to strict schema, Pydantic, Zod and a ready request.
-   [LLM JSON output validator](https://broadrouter.com/tools/llm-json-output-validator/) Parse errors by line and column, safe repairs, cut-off detection and JSON Schema checks for model output.

[All free tools](https://broadrouter.com/tools/)

## Terms in this article

[Token](https://broadrouter.com/glossary/token/)

A token is the unit of text a language model reads and writes: a word, part of a word, a punctuation mark or a byte sequence produced by the model's tokenizer. LLM APIs count, limit and usually bill usage in tokens.

[Cached input tokens](https://broadrouter.com/glossary/cached-input-tokens/)

Cached input tokens are prompt tokens that an LLM provider has recently processed and can reuse from a cache instead of computing again, usually a repeated prefix such as a system prompt or document. Providers bill them at a lower rate than new input tokens.

[Reasoning tokens](https://broadrouter.com/glossary/reasoning-tokens/)

Reasoning tokens are tokens a reasoning model generates to work through a problem before it writes its visible answer. They are usually billed as output tokens, count toward the output limit and the context window, and may be hidden or summarized by the API.

[API key](https://broadrouter.com/glossary/api-key/)

An API key is a secret string that identifies and authorizes a client calling an API. LLM APIs usually expect it in an Authorization: Bearer header, and anyone who holds the key can make requests that are billed to the account that owns it.

[Eval (LLM evaluation)](https://broadrouter.com/glossary/eval/)

An eval (short for evaluation) is a repeatable test of a language model or prompt: a fixed set of inputs, a description of what a good output looks like, and a grader (code, a human or another model) that scores each output so that versions can be compared.

[Named entity recognition (NER)](https://broadrouter.com/glossary/named-entity-recognition/)

Named entity recognition (NER) is the task of finding names in text and labelling each with a type, such as person, organization or location. Classic systems tag every token; LLMs are often asked instead to return a list of entities as JSON, which is then checked against the source.

[PII (personally identifiable information)](https://broadrouter.com/glossary/pii/)

PII (personally identifiable information) is information that can be used to distinguish or trace a person's identity, such as a name, ID number or biometric record, plus other information linked or linkable to that person. The GDPR's broader term is personal data. In LLM work, PII redaction removes it from prompts before they leave your systems.

[Server-sent events (SSE)](https://broadrouter.com/glossary/sse/)

Server-sent events (SSE) is a web standard for a server to push a stream of text events to a client over one long-lived HTTP response with the content type text/event-stream. Each event is one or more field lines, such as data:, ended by a blank line.

## Questions

### How do I track LLM API cost per user?

Tag each call with a user identifier, read the usage block from the response, multiply token counts by your price table, and write one log row per call. Group by the user tag. Use a hashed ID rather than an email.

### Should I log full prompts and responses?

Not by default. They are where personal data lives. Log token counts and metadata for every call, and keep full text only for sampled or failed calls, after redaction, with a short retention period.

### Why does my computed cost differ from the provider invoice?

Usually because cached tokens were priced as new input, reasoning tokens were left out of output, or retries were not logged. Log every attempt, including failed ones that returned usage.

### How do I get token usage from a streaming response?

Set stream\_options include\_usage to true. The final chunk then carries the usage block with an empty choices list.

### What PII redaction tool should I use for LLM logs?

Start with regex for structured data such as emails, phone numbers and card numbers, and add an NER-based tool such as Microsoft Presidio for names and addresses. Presidio's own README says it cannot guarantee it finds everything.

## Keep reading

-   [Zero data retention LLM API: a 10-question checklist](https://broadrouter.com/blog/zero-data-retention-llm-api-checklist/)
-   [Cached input tokens: how to cut an LLM API bill](https://broadrouter.com/blog/cached-input-tokens-explained/)
-   [How to handle LLM API 429 errors: backoff, jitter and limiters](https://broadrouter.com/blog/handle-llm-api-429-rate-limits/)
-   [LLM streaming explained: SSE chunks, deltas, usage and cancel](https://broadrouter.com/blog/llm-streaming-sse-explained/)
-   [Cache LLM API responses to cut cost: an exact-match cache in Python](https://broadrouter.com/blog/cache-llm-responses-cut-cost/)
-   [A private endpoint starts with a clear workload](https://broadrouter.com/blog/private-endpoint-planning/)
-   [Before fine-tuning: define the problem to solve](https://broadrouter.com/blog/fine-tuning-readiness/)
-   [Guide: Use the OpenAI Python SDK with BroadNet Inference](https://broadrouter.com/guides/openai-python-sdk/)
-   [Guide: Stream chat completions and handle dropped connections](https://broadrouter.com/guides/streaming-chat-completions/)
-   [Guide: Lock API keys to your servers with IP allow-lists](https://broadrouter.com/guides/api-keys-ip-allow-lists/)

Try it on your real workload. New accounts start with $5 of credit for Gemma and transcription. DeepSeek, GLM and Qwen3.8 need a top-up.

[Get started](https://inference.broadnet.ai/account/?utm_source=broadrouter&utm_medium=referral&utm_campaign=blog-llm-api-usage-logging&utm_content=page-cta) ·

**Sources**

-   [BroadNet Inference API reference, usage fields, request IDs and retention](https://inference.broadnet.ai/docs?utm_source=broadrouter&utm_medium=referral&utm_campaign=blog-llm-api-usage-logging&utm_content=sources)
-   [BroadNet Inference machine-readable prices (pricing.json)](https://inference.broadnet.ai/pricing.json?utm_source=broadrouter&utm_medium=referral&utm_campaign=blog-llm-api-usage-logging&utm_content=sources)
-   [OWASP Logging Cheat Sheet, data to exclude from logs](https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html)
-   [Microsoft Presidio README, PII detection and its limits (fetched 2026-11-14)](https://github.com/microsoft/presidio)
-   [openai-python README, reading response headers with with\_raw\_response](https://github.com/openai/openai-python)
