Skip to content

fix(ai): capture pre-calculated cost from OpenAI Agents responses#733

Draft
fuchengwarrenzhu wants to merge 4 commits into
PostHog:mainfrom
fuchengwarrenzhu:warren/openai-agents-usage-cost
Draft

fix(ai): capture pre-calculated cost from OpenAI Agents responses#733
fuchengwarrenzhu wants to merge 4 commits into
PostHog:mainfrom
fuchengwarrenzhu:warren/openai-agents-usage-cost

Conversation

@fuchengwarrenzhu

@fuchengwarrenzhu fuchengwarrenzhu commented Jul 10, 2026

Copy link
Copy Markdown

💡 Motivation and Context

Captures an SDK-provided response.usage.cost for OpenAI Agents response spans as $ai_total_cost_usd. This preserves gateway-calculated pricing when an OpenAI-compatible gateway routes an alias to a model whose provider differs from the apparent model.

💚 How did you test it?

  • uv run pytest posthog/test/ai/openai_agents/test_processor.py -q
  • uv run ruff format --check posthog/ai/openai_agents/processor.py posthog/test/ai/openai_agents/test_processor.py
  • uv run ruff check posthog/ai/openai_agents/processor.py posthog/test/ai/openai_agents/test_processor.py
  • uv run python -W error -c "import posthog"

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file

🤖 Agent context

Autonomy: Fully autonomous

Codex verified the targeted implementation and focused test coverage. The change only forwards a non-None pre-calculated cost, preserving explicit zero and leaving events without a cost unchanged.


Created with PostHog Code from an inbox report.

@fuchengwarrenzhu fuchengwarrenzhu requested a review from a team as a code owner July 10, 2026 01:58
@fuchengwarrenzhu

Copy link
Copy Markdown
Author

This is ready for review.

@fuchengwarrenzhu fuchengwarrenzhu changed the title feat(ai): capture usage cost for OpenAI Agents responses fix(ai): capture pre-calculated cost from OpenAI Agents responses Jul 10, 2026
@posthog posthog Bot marked this pull request as draft July 10, 2026 15:21
@dustinbyrne dustinbyrne requested a review from a team July 10, 2026 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant