Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: add optional OpenAI usage/cost tracking for presentation generation#421

Open
mlimarenko wants to merge 3 commits intopresenton:mainfrom
mlimarenko:main
Open

feat: add optional OpenAI usage/cost tracking for presentation generation#421
mlimarenko wants to merge 3 commits intopresenton:mainfrom
mlimarenko:main

Conversation

@mlimarenko
Copy link

Summary

This PR adds optional end-to-end OpenAI usage and cost tracking for presentation generation, including LLM and image
generation calls.

What’s included

  • Added optional usage tracking flag in generate request:
    • track_openai_usage in GeneratePresentationRequest
  • Extended generate response with usage summary:
    • openai_usage in PresentationPathAndEditPath
  • Implemented context-based tracker:
    • servers/fastapi/services/openai_usage_tracker.py
  • Added usage/cost response models:
    • servers/fastapi/models/openai_usage_cost.py
  • Wired tracking into generation pipeline:
    • wraps generate_presentation_handler execution in tracker context
  • Captures OpenAI usage from:
    • chat.completions (regular + structured + streaming with include_usage)
    • responses API (web search path)
    • images.generate
  • Added pricing support:
    • built-in defaults for gpt-5.2 and gpt-image-1.5
    • env override via OPENAI_PRICING_JSON
    • unpriced model warnings and exclusion from total
  • Added env helpers and docs:
    • TRACK_OPENAI_USAGE
    • OPENAI_PRICING_JSON
  • Added tests:
    • servers/fastapi/tests/test_openai_usage_tracker.py
    • validates cached tokens, pricing override, unpriced models, image modal pricing

Additional fix

  • Installed zstd in both Docker images:
    • Dockerfile
    • Dockerfile.dev
  • Purpose: avoid runtime/container issues where zstd support is required by dependencies/tooling.

Validation

  • uv run pytest -q tests/test_openai_usage_tracker.py → passed
  • compile checks for modified FastAPI files → passed
  • manual API generation call with track_openai_usage=true returned populated openai_usage with total USD cost and
    per-model breakdown.

Backward compatibility

  • Tracking is optional and disabled by default unless enabled by:
    • request field track_openai_usage=true, or
    • env TRACK_OPENAI_USAGE=true.
  • Existing clients remain compatible; new response field is optional.

root added 3 commits February 22, 2026 14:55
Track OpenAI token usage and estimated USD cost for presentation generation when enabled.\n\n- add context-aware OpenAI usage tracker with per-model aggregation\n- capture usage from chat completions, responses API, streaming chunks, and image generation\n- support pricing defaults for gpt-5.2 and gpt-image-1.5 with OPENAI_PRICING_JSON overrides\n- extend generate request/response models with track_openai_usage and openai_usage summary\n- wire tracking through presentation generation pipeline and document env flags\n- add unit tests for pricing, cached tokens, overrides, and unpriced models
Add zstd package to both production and development Docker images so dependencies and runtime paths that rely on zstd support do not fail in containerized environments.
Update built-in OpenAI pricing defaults used by usage tracker to match official rates for gpt-5.2 and gpt-image-1.5, and adjust unit test expectations accordingly.
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