OpenAI usage tracking: Project ID setting is ignored

Open
#1,247 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@dajiaohuang is already working on this.

Since Oct 20, 2026.

  • #1248 by @dajiaohuang — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
php, wordpress
Domain
api, backend

Research direction

Read includes/Classifai/Providers/ProviderProfiles.php and trace CredentialResolver::resolve() for the api_usage_tracking profile; the issue names the affected fields and request path. Check the existing credential resolver and UsageTrackingProvider tests, then verify that configured project IDs appear as project_ids on costs requests and are omitted when unset. A linked pull request is already open.

Written by the indexing model from the issue text.

Description

Describe the bug

The AI Usage Tracking feature lets you enter an optional Project ID so cost data is restricted to a single OpenAI project (see wp-hooks-docs/docs/02.feature-configuration/19.usage-tracking.md: "Optionally enter a Project ID to restrict cost data to a single OpenAI project."). The value is saved, but it never reaches the OpenAI costs request, so the usage and cost totals are always organization-wide.

Steps to reproduce
  1. Enable ClassifAI → Usage Tracking → AI Usage Tracking and select OpenAI.
  2. Enter an Admin API key and a Project ID (e.g. proj_abc123), then save the settings.
  3. Trigger a usage refresh (wait for the Action Scheduler run or click Refresh Usage Data).
  4. Inspect the request to https://api.openai.com/v1/organization/costs: there is no project_ids query arg, so the totals include every project in the organization.
Expected behavior

The saved Project ID should be sent as the project_ids query arg on the costs request, which is what UsageTrackingProvider::fetch_period() attempts to do.

Root cause

UsageTrackingProvider::fetch_period() reads the setting with get_credential( 'project_id', $settings ). That call goes through Provider::get_credentials() → CredentialResolver::resolve(), which keeps only the fields listed in the provider profile's credential_fields. The api_usage_tracking profile in includes/Classifai/Providers/ProviderProfiles.php lists only api_key and authenticated, so project_id is filtered out and $project_id is always empty. That makes the if ( ! empty( $project_id ) ) { $url = add_query_arg( 'project_ids', $project_id, $url ); } branch unreachable.

This is the same pattern the Azure profiles already use: the non-secret deployment value is listed next to the credentials so it survives resolution.

Proposed fix

Add project_id to the api_usage_tracking profile's credential_fields (it stays out of sensitive_fields, so it is not obfuscated) and add regression tests that cover:

  • CredentialResolver::resolve( 'openai_usage_tracking', ... ) keeps project_id;
  • fetch_period() requests include project_ids=<value> when a Project ID is configured, and omit it when not.

A PR covering this will follow.

Dominant language
PHP
Stars
714
Forks
68
Avg merge
4h 32m
Merged PRs (30d)
23

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from 10up/classifai

All issues in 10up/classifai

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.