feat: Add OpenAI/Codex usage tracking support #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Adds OpenAI API usage tracking following the existing provider pattern (Anthropic, Cursor). This integrates with OpenAI's Usage API to track token consumption across GPT-4o, o1, o3-mini, and Codex models.
New files:
src/lib/sync/openai.ts- Main sync module withsyncOpenAIUsage(),syncOpenAICron(),backfillOpenAIUsage()src/lib/sync/openai-mappings.ts- User ID to email mapping via/v1/organization/userssrc/app/api/cron/sync-openai/route.ts- Daily cron endpoint (7 AM UTC)Modified:
openai:status,sync openai,backfill openaicommandsEnvironment variable required:
From: https://platform.openai.com/settings/organization/admin-keys
This implementation has not been tested as Sentry does not currently have an OpenAI Enterprise plan with admin API access. The code follows the same patterns as the working Anthropic integration, but real-world testing is needed.
ChatGPT Team Plan Limitations
During research, we discovered significant limitations for organizations on ChatGPT Team plans (vs Enterprise):
/v1/organization/usage/*)This means: If your team uses Codex CLI authenticated via ChatGPT accounts (not API keys), usage tracking is not available on Team plans. Options:
~/.codex/sessions/) - but this requires manual collection from each userSources:
Test plan
OPENAI_ADMIN_KEYenvironment variablenpm run cli openai:statusto verify connectionnpm run cli sync openai --days 7to sync recent datanpm run cli backfill openai --from 2024-01-01 --to 2025-01-01🤖 Generated with Claude Code