Problem/Motivation

When ai_translate is configured to use the chat_translation provider,
every translation is recorded twice in ai_metering: once under the non-billable
chat_translation provider name and once under the real provider
(e.g. anthropic). This doubles the call count and the displayed
cost on the dashboard, and surfaces a provider name that has no pricing of its own.

The root cause is that chat_translation is a wrapper: it dispatches
its own PostGenerateResponseEvent and then makes a nested call to
the real provider, which dispatches a second event. ai_metering records both.

Steps to reproduce

  1. Install ai_metering and ai_translate. Configure ai_translate to use the chat_translation provider backed by Anthropic.
  2. Translate a node (e.g. from English to French).
  3. Open /admin/reports/ai-metering. Two rows appear with the same timestamp: one labelled chat_translation and one labelled anthropic. Token counts and cost are doubled.

Environment

  • Drupal version: 11.3.11
  • ai_metering version: 1.0.0-alpha1
  • AI provider module + version: ai_provider_anthropic 1.x
  • AI feature module tested: ai_translate 1.x

Proposed resolution

Skip wrapper-provider events in AiPostGenerateSubscriber, keyed off
a small list of known wrapper providers (starting with chat_translation).
The inner call to the real provider already records the operation with the correct
tokens, model, and billable provider, so only that row should remain.

Remaining tasks

  • Skip wrapper providers in AiPostGenerateSubscriber.
  • Add a kernel test asserting no row is written for a wrapper provider event.
  • Verify a single row per translation appears under the real provider.
  • Review.

User interface changes

Yes, the dashboard shows one row per translation with the correct provider and cost.

API changes

N/A

Data model changes

N/A

AI assistance

N/A

Comments

codeitwisely created an issue. See original summary.

codeitwisely’s picture

Assigned: Unassigned » codeitwisely

  • 0ec43525 committed on 1.0.x-3604229-skip-wrapper-provider-double-count
    Issue #3604229 by codeitwisely: Skip wrapper providers to prevent double...

  • 9d8695f5 committed on 1.0.x-3604229-skip-wrapper-provider-double-count
    Issue #3604229 by codeitwisely: Skip wrapper providers to prevent double...

  • codeitwisely committed 9ebc6305 on 1.0.x
    Issue #3604229: Skip wrapper providers to prevent double-counting AI...

  • codeitwisely committed 9ebc6305 on 1.0.x-3604228-uid-filter-in-usage-log
    Issue #3604229: Skip wrapper providers to prevent double-counting AI...

  • codeitwisely committed 9ebc6305 on 1.0.x-3604255-token-details-column
    Issue #3604229: Skip wrapper providers to prevent double-counting AI...

  • codeitwisely committed 9ebc6305 on 1.0.x-3604240-toolbar-icon
    Issue #3604229: Skip wrapper providers to prevent double-counting AI...

  • codeitwisely committed 9ebc6305 on 1.0.x-3604346-by-role-dashboard
    Issue #3604229: Skip wrapper providers to prevent double-counting AI...
codeitwisely’s picture

Status: Active » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.