#18046·langfuse

bug: OpenAI-compatible usage with provider fields loses normalized token counts

Author: dakjdakdCreated Sep 29, 2026Updated Sep 29, 2026
Labelsbugfeat-llm-cost-trackingfeat-ingestion

Prerequisites

  • I have searched the existing issues and pull requests for the ingestion and usage-accounting paths, and this does not duplicate the reports about missing streaming usage.
  • This is a bug report, not a usage question.

Describe the bug

OpenAI-compatible providers can return the standard token counters together with provider-specific usage fields. Langfuse accepts the payload, but an extra top-level field can make the usage parser choose its fallback branch instead of the OpenAI normalization branch.

For example, an OpenAI-compatible provider can return a Chat Completions usage object shaped like this:

json
{
  "prompt_tokens": 194,
  "completion_tokens": 2,
  "total_tokens": 196,
  "prompt_tokens_details": {
    "cached_tokens": 4
  },
  "cost": 0.0001,
  "cost_details": {
    "upstream_inference_cost": 0.0001
  }
}

The OpenAI-compatible schemas in packages/shared/src/server/ingestion/types.ts are strict objects. Because cost and cost_details are not part of those schemas, the object is rejected by both OpenAI-shaped branches. The union then falls through to RawUsageDetails, which only keeps top-level non-negative integer values.

That fallback changes the meaning of the payload:

  • prompt_tokens_details and completion_tokens_details are discarded;
  • prompt_tokens and completion_tokens are kept under their provider field names instead of being normalized to input and output;
  • total_tokens is not recognized as the normalized total field;
  • the ingestion service later synthesizes total by summing the remaining numeric fields, so 194 + 2 + 196 becomes 392.

The generation is therefore stored with incorrect usage information and cannot match the normal input/output pricing keys.

Steps to reproduce

  1. Send a generation-update event through the public ingestion API.
  2. Put the JSON object above in body.usageDetails.
  3. Read the stored generation usage details. In the parser-level reproduction below, inspect the parsed object directly.

A minimal parser-level reproduction is:

typescript
UsageDetails.parse({
  prompt_tokens: 194,
  completion_tokens: 2,
  total_tokens: 196,
  prompt_tokens_details: { cached_tokens: 4 },
  cost: 0.0001,
  cost_details: { upstream_inference_cost: 0.0001 },
});

Expected behavior

Unknown provider-specific top-level fields should not change the interpretation of the standard OpenAI usage fields.

The payload should continue through the OpenAI normalization branch and produce input: 190, input_cached_tokens: 4, output: 2, and total: 196. Provider extensions that are outside Langfuse's usage contract may be ignored, but they must not change the branch selected by the parser.

Impact

This affects OpenAI-compatible integrations that attach accounting or provider metadata to an otherwise valid usage object. It can produce incorrect token totals, omit cache/reasoning token details, and prevent the normal model-pricing lookup from using the reported input and output counters.

The same shape applies to both the Chat Completions usage form and the newer Responses API usage form.

Proposed fix

Make the two OpenAI usage schemas tolerant of unknown top-level properties while keeping their known fields and transforms unchanged. Zod's default object behavior is sufficient here: the provider extensions can be stripped after the OpenAI-shaped branch has been selected.

Add regression coverage for:

  • a Chat Completions usage object with cost and cost_details;
  • a Responses API usage object with an additional provider field;
  • nested token detail fields remaining available after normalization.

Relevant code

  • packages/shared/src/server/ingestion/types.ts — OpenAICompletionUsageSchema, OpenAIResponseUsageSchema, and UsageDetails.
  • worker/src/services/IngestionService/index.ts — normalization and total-usage synthesis.

Actual values from the example

With the payload above, the generic fallback retains the provider field names and produces a synthesized total from all top-level integer counters:

prompt_tokens: 194
completion_tokens: 2
total_tokens: 196
total: 392

The normalized OpenAI path should instead expose input: 190, input_cached_tokens: 4, output: 2, and total: 196. The four cached input tokens are kept in their own bucket.

Environment

  • Langfuse main at 81e065bb0
  • Affected surface: public ingestion usage parsing and worker-side usage/pricing normalization
  • Provider example: OpenRouter's documented OpenAI-compatible usage payload

Related reports

The existing streaming-usage reports concern requests where no usage chunk is requested or returned. This report concerns a usage object that is present and valid, but is routed through the wrong parser because it contains provider extensions.