Conversation
Consolidate duplicated image usage recording logic into shared helpers Support polymorphic request/response types (chat completion, image generation) via tagged union
Summary of ChangesHello @henrypark133, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the platform's usage tracking capabilities by introducing a dedicated API endpoint for recording usage events. It streamlines internal usage recording processes through refactoring and ensures that both internal and external usage logging is consistent, robust, and provides rich data through updated service interfaces and detailed response structures. The changes facilitate better integration with external systems for usage reporting and billing. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new API endpoint for recording usage and refactors existing usage recording logic for image generation. The changes are well-structured, and the addition of end-to-end tests for the new endpoint is excellent. The core logic for the new /v1/usage endpoint, including request validation and delegation to the service layer, is solid. The refactoring in completions.rs significantly improves code clarity and reduces duplication.
I have a couple of suggestions for improvement:
- Refactoring duplicated code in the
record_usage_with_sync_fallbackhelper function to improve maintainability. - Making the handling of
image_countin the newrecord_usageAPI handler more robust by logging an error if it's unexpectedly missing.
Overall, this is a great contribution that enhances the API's capabilities and improves the codebase.
Code Review - PR #421I've reviewed this PR focusing on critical issues. The overall architecture and implementation are solid, but I found one critical issue that needs to be addressed before merging:
|
Made external id required so every new usage record has some idempotency protection |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
Note
Medium Risk
Touches billing/usage accounting and adds a new write endpoint plus a DB uniqueness constraint; mistakes could cause under/over-charging or rejected inserts, though changes are scoped and covered by new E2E tests.
Overview
Adds a new authenticated
POST /v1/usageendpoint (and OpenAPI schema) to let clients record chat-completion and image-generation usage directly, with server-side model pricing lookup, request validation, and a tagged-union response.Makes usage recording idempotent per organization by hashing the caller-supplied
idintoinference_id, enforcing it with a new partial unique index, and updating the usage repository/service flow to return the existing record (and skip balance updates + cost metrics) on duplicates; also refactors image usage recording in completion routes to reuse shared helper/timeout+async-retry logic and updates mocks/tests accordingly.Written by Cursor Bugbot for commit c971212. This will update automatically on new commits. Configure here.