Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

feat(ai-usage): track embedding usage separately with usage_type column#981

Open
Abh1shxkk wants to merge 11 commits intoInsForge:mainInsForge/InsForge:mainfrom
Abh1shxkk:feat/embeddings-usage-type-979Abh1shxkk/InsForge:feat/embeddings-usage-type-979Copy head branch name to clipboard
Open

feat(ai-usage): track embedding usage separately with usage_type column#981
Abh1shxkk wants to merge 11 commits intoInsForge:mainInsForge/InsForge:mainfrom
Abh1shxkk:feat/embeddings-usage-type-979Abh1shxkk/InsForge:feat/embeddings-usage-type-979Copy head branch name to clipboard

Conversation

@Abh1shxkk
Copy link
Copy Markdown
Contributor

@Abh1shxkk Abh1shxkk commented Mar 20, 2026

Summary

Closes #979

  • Migration 025 — adds usage_type VARCHAR(20) NOT NULL DEFAULT 'chat' to ai.usage, backfills existing image generation rows to 'image_generation'
  • shared-schemas — adds usageTypeSchema ('chat' | 'embedding' | 'image_generation'), usageType field to aiUsageDataSchema and aiUsageRecordSchema, and embeddingRequests/embeddingTokens breakdown to aiUsageSummarySchema
  • ai-usage.service — new trackEmbeddingUsage() method; trackChatUsage and trackImageGenerationUsage now write explicit usage_type; getUsageSummary returns per-type breakdown; getAllUsage includes usage_type in SELECT
  • embedding.service — calls trackEmbeddingUsage() instead of trackChatUsage(), threads source from sendRequest to skip tracking for BYOK

How did you test this change?

  1. Migration verified

docker exec -it insforge-postgres psql -U postgres -d insforge -c "SELECT column_name, data_type, column_default FROM information_schema.columns WHERE table_schema='ai' AND table_name='usage' AND column_name='usage_type';"
→ Column added with DEFAULT 'chat' ✅

  1. Chat request tracked with correct type

curl -X POST http://localhost:7130/api/ai/chat/completion
-H "Authorization: Bearer ik_local_dev_access_key_..."
-H "Content-Type: application/json"
-d '{"model":"meta-llama/llama-3.2-1b-instruct","messages":[{"role":"user","content":"say hi"}]}'
DB result:

usage_type | input_tokens | output_tokens
-----------+--------------+--------------
chat | 13 | 3

  1. Usage records include usageType field

GET /api/ai/usage?limit=5
Response includes "usageType":"chat" on each record ✅

  1. Usage summary includes embedding breakdown

GET /api/ai/usage/summary
Response:

{
"totalInputTokens": 13,
"totalOutputTokens": 3,
"totalTokens": 16,
"totalImageCount": 0,
"totalRequests": 1,
"embeddingRequests": 0,
"embeddingTokens": 0
}
embeddingRequests and embeddingTokens fields present ✅

  1. embedding.service now calls trackEmbeddingUsage() instead of trackChatUsage() — embedding rows will be stored with usage_type = 'embedding' going forward ✅

Summary by CodeRabbit

  • New Features

    • Added an AI Usage dashboard with date-range filters, paginated records, summary cards, badges, retry/empty states, and embedding-specific metrics.
    • Added routing and a menu entry for the AI Usage page.
    • Tracking now distinguishes usage types: chat, image generation, and embedding.
  • Bug Fixes

    • Improved menu item active-state matching to respect exact-path configuration.
  • Chores

    • Database migration added to introduce the usage type field while preserving existing data.

Note

Track embedding AI usage separately with a usage_type column

  • Adds a usage_type VARCHAR(20) column (default 'chat') to ai.usage via migration, backfilling rows with image_count > 0 as 'image_generation'.
  • Adds trackEmbeddingUsage to AIUsageService and updates EmbeddingService to call it instead of trackChatUsage, so embedding requests are now recorded with usage_type='embedding'.
  • Extends getUsageSummary and getAllUsage to return embeddingRequests, embeddingTokens, and usageType per record.
  • Adds an AI Usage page at /dashboard/ai/usage with date filters, pagination, and per-record usageType badges, plus a sidebar for AI section navigation.
  • Also introduces a storage object rename feature (backend service, S3/local providers, API endpoint, and dashboard UI), which appears bundled in this PR.

Macroscope summarized f4d61e9.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 20, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

Adds a non-null usage_type column to ai.usage (default 'chat' and backfilled for image rows), extends schemas to include usageType and embedding aggregates, tracks embedding usage via a new backend method, updates embedding service to call the new tracker, and adds a frontend AI Usage page, route, and menu entry.

Changes

Cohort / File(s) Summary
Database Migration
backend/src/infra/database/migrations/025_add-usage-type-to-ai-usage.sql
Add usage_type VARCHAR NOT NULL DEFAULT 'chat' and backfill existing image rows to 'image_generation'.
Backend — AI Usage Service
backend/src/services/ai/ai-usage.service.ts
Insert statements include usage_type; trackUsage uses `data.usageType
Backend — Embedding Service
backend/src/services/ai/embedding.service.ts
Cast provider response to OpenAI embedding types; call aiUsageService.trackEmbeddingUsage(...) instead of chat tracker; remove derived outputTokens for embeddings.
Shared Schemas
shared-schemas/src/ai.schema.ts
Add usageType enum (`'chat'
Frontend — AI Usage UI & Routing
frontend/src/features/ai/pages/AIUsagePage.tsx, frontend/src/lib/routing/AppRoutes.tsx, frontend/src/lib/utils/menuItems.ts, frontend/src/components/layout/SecondaryMenu.tsx
Add AIUsagePage (date-range, summary stats, paginated records, type badges); register route /dashboard/ai/usage; add Usage menu item and exact flag handling in secondary menu matching.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested reviewers

  • tonychang04
  • Fermionic-Lyu

Poem

🐰 I hop through rows and mark each kind,

chat, embed, or images aligned.
Tokens counted, types in place,
each record finds its proper space.
A little hop — clear traces shine.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning Frontend changes include SecondaryMenu.tsx route matching adjustment, AIUsagePage component, and menuItems.ts updates that are related to the AI usage dashboard but extend beyond the core backend tracking requirements from issue #979. The PR comments indicate frontend work should be in separate PR #982. Move SecondaryMenu.tsx, AIUsagePage.tsx, AppRoutes.tsx route addition, and menuItems.ts updates to PR #982 to keep backend tracking (this PR) and frontend display separated as discussed in the thread.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and specifically describes the main change: introducing a usage_type column to track embedding usage separately, which is the core objective of the PR.
Linked Issues check ✅ Passed The PR successfully implements all primary coding objectives from issue #979: adds usage_type column with migration, updates shared schemas with usageType field, tracks chat/image/embedding with correct types, updates getUsageSummary with embedding metrics, and updates getAllUsage to include usage_type.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
backend/src/services/ai/embedding.service.ts (1)

32-39: ⚠️ Potential issue | 🔴 Critical

Build-breaking type error: sendRequest() does not return { result, source }.

The pipeline failure confirms this issue. Per openrouter.provider.ts:340-388, sendRequest<T>() returns Promise<T> directly (the raw CreateEmbeddingResponse), not an object with result and source properties.

Either:

  1. Modify OpenRouterProvider.sendRequest() to return { result: T, source: string }, or
  2. Revert to the previous pattern and find another way to detect BYOK usage
🔧 Option 2: Revert to direct response handling
-      const { result: response, source } = await this.openRouterProvider.sendRequest((client) =>
+      const response = await this.openRouterProvider.sendRequest((client) =>
         client.embeddings.create({
           model: options.model,
           input: options.input,
           encoding_format: options.encoding_format || 'float',
           dimensions: options.dimensions,
         })
       );

This would also require updating the BYOK check at line 57 to use a different mechanism.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/src/services/ai/embedding.service.ts` around lines 32 - 39, The call
to openRouterProvider.sendRequest currently returns the raw
CreateEmbeddingResponse (Promise<T>), not an object with { result, source }, so
update the embedding call in embedding.service.ts to accept the direct response
from sendRequest (e.g., assign the returned value to a variable like response or
embeddingResult) and stop destructuring { result, source }; then adjust the BYOK
detection logic (the check referencing source/BYOK) to use a different signal
available on the raw CreateEmbeddingResponse or another provider method (e.g.,
inspect fields on the returned response or add a helper on OpenRouterProvider)
so BYOK detection no longer relies on a non-existent source property.
backend/src/services/ai/ai-usage.service.ts (1)

167-171: ⚠️ Potential issue | 🟡 Minor

Include usage_type in SELECT statement to return accurate data.

The getUsageByConfig() method omits the usage_type column from its SELECT query. While Zod validation won't fail (the schema has usageType: usageTypeSchema.default('chat')), this causes all returned records to have usageType: 'chat' regardless of their actual database value, resulting in data inaccuracy. Other methods in the same file correctly select this column (e.g., line 268). The database column exists and stores distinct values: 'chat', 'embedding', and 'image_generation'.

🛠️ Proposed fix to include usageType
       let query = `
         SELECT id, config_id as "configId", input_tokens as "inputTokens",
                output_tokens as "outputTokens", image_count as "imageCount",
-               image_resolution as "imageResolution", created_at as "createdAt"
+               image_resolution as "imageResolution", created_at as "createdAt",
+               usage_type as "usageType"
         FROM ai.usage
         WHERE config_id = $1
       `;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/src/services/ai/ai-usage.service.ts` around lines 167 - 171, The
SELECT in getUsageByConfig is missing the usage_type column so returned rows get
the Zod default ('chat') instead of the DB value; update the SQL in
getUsageByConfig to include usage_type as "usageType" (matching other methods'
mapping) so the query returns the actual usage_type values for downstream
validation and mapping.
🧹 Nitpick comments (1)
backend/src/infra/database/migrations/025_add-usage-type-to-ai-usage.sql (1)

2-2: Consider adding an index on usage_type for query performance.

The getUsageSummary() method uses FILTER (WHERE usage_type = 'embedding') clauses. As the table grows, an index on usage_type could improve aggregation performance.

📊 Optional index suggestion
CREATE INDEX IF NOT EXISTS idx_ai_usage_usage_type ON ai.usage (usage_type);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/src/infra/database/migrations/025_add-usage-type-to-ai-usage.sql` at
line 2, Add a non-blocking index creation to the migration to improve query
performance on the new usage_type column used by getUsageSummary();
specifically, in the migration that adds the usage_type column on table ai.usage
add a CREATE INDEX IF NOT EXISTS (e.g., idx_ai_usage_usage_type on ai.usage
(usage_type)) so aggregates and FILTER (WHERE usage_type = 'embedding') clauses
run faster as the table grows. Ensure the index creation is included in the same
migration flow (or a follow-up migration) and uses IF NOT EXISTS to be
idempotent.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@backend/src/services/ai/embedding.service.ts`:
- Around line 56-63: The BYOK check using `source !== 'byok'` is ineffective
because `source` is not returned by `sendRequest()`; update the embedding usage
tracking in the method that calls `this.aiUsageService.trackEmbeddingUsage` to
use an explicit BYOK flag instead: add and read a boolean like `options.isByok`
(or `options.source`) passed into the provider call, or detect BYOK from
`aiConfig` (e.g., `aiConfig.isByok`) before invoking tracking; then change the
condition to `if (aiConfig?.id && tokenUsage && !options.isByok)` (or
`!aiConfig.isByok`) so BYOK requests are correctly excluded when calling
`trackEmbeddingUsage`.

---

Outside diff comments:
In `@backend/src/services/ai/ai-usage.service.ts`:
- Around line 167-171: The SELECT in getUsageByConfig is missing the usage_type
column so returned rows get the Zod default ('chat') instead of the DB value;
update the SQL in getUsageByConfig to include usage_type as "usageType"
(matching other methods' mapping) so the query returns the actual usage_type
values for downstream validation and mapping.

In `@backend/src/services/ai/embedding.service.ts`:
- Around line 32-39: The call to openRouterProvider.sendRequest currently
returns the raw CreateEmbeddingResponse (Promise<T>), not an object with {
result, source }, so update the embedding call in embedding.service.ts to accept
the direct response from sendRequest (e.g., assign the returned value to a
variable like response or embeddingResult) and stop destructuring { result,
source }; then adjust the BYOK detection logic (the check referencing
source/BYOK) to use a different signal available on the raw
CreateEmbeddingResponse or another provider method (e.g., inspect fields on the
returned response or add a helper on OpenRouterProvider) so BYOK detection no
longer relies on a non-existent source property.

---

Nitpick comments:
In `@backend/src/infra/database/migrations/025_add-usage-type-to-ai-usage.sql`:
- Line 2: Add a non-blocking index creation to the migration to improve query
performance on the new usage_type column used by getUsageSummary();
specifically, in the migration that adds the usage_type column on table ai.usage
add a CREATE INDEX IF NOT EXISTS (e.g., idx_ai_usage_usage_type on ai.usage
(usage_type)) so aggregates and FILTER (WHERE usage_type = 'embedding') clauses
run faster as the table grows. Ensure the index creation is included in the same
migration flow (or a follow-up migration) and uses IF NOT EXISTS to be
idempotent.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 915f2379-8ef7-452f-b889-e5bec0d4e549

📥 Commits

Reviewing files that changed from the base of the PR and between 79a07f7 and cfc7180.

📒 Files selected for processing (4)
  • backend/src/infra/database/migrations/025_add-usage-type-to-ai-usage.sql
  • backend/src/services/ai/ai-usage.service.ts
  • backend/src/services/ai/embedding.service.ts
  • shared-schemas/src/ai.schema.ts

Comment thread backend/src/services/ai/embedding.service.ts Outdated
@tonychang04
Copy link
Copy Markdown
Contributor

on top of this we also want the correspoding ui to display embedding usages

@Abh1shxkk
Copy link
Copy Markdown
Contributor Author

on top of this we also want the correspoding ui to display embedding usages

Hey @tonychang04, quick clarification — when we discussed surfacing embedding usage in the UI, I ended up opening a separate issue #978 (AI Usage Dashboard) and built it in a separate PR #982. The backend fixes (usage_type column, trackEmbeddingUsage) are in this PR, and the frontend (type badges, embedding token counts in summary) is in #982 which depends on this merging first.

Two options going forward:

Keep them separate — merge this first, then #982 brings the UI
Squash both into one branch — I can bring the frontend changes into this PR
What would you prefer?

@Abh1shxkk
Copy link
Copy Markdown
Contributor Author

on top of this we also want the correspoding ui to display embedding usages

Hey @tonychang04, quick clarification — when we discussed surfacing embedding usage in the UI, I ended up opening a separate issue #978 (AI Usage Dashboard) and built it in a separate PR #982. The backend fixes (usage_type column, trackEmbeddingUsage) are in this PR, and the frontend (type badges, embedding token counts in summary) is in #982 which depends on this merging first.

Two options going forward:

Keep them separate — merge this first, then #982 brings the UI Squash both into one branch — I can bring the frontend changes into this PR What would you prefer?

Hey @tonychang04, @Fermionic-Lyu just to clarify — I had already opened issue #978 (AI Usage Dashboard) before you raised #979, so I split the work into two PRs. The backend tracking is here in #981 and the frontend UI is in #982. Both are linked and #982 depends on this merging first. Let me know if you'd prefer I consolidate them into one PR!

@tonychang04
Copy link
Copy Markdown
Contributor

@Abh1shxkk I prefer to keep it seperate, for the focus in this repo, can we follow somthing like the old ui?

for the other branch. you can give us the view for the new ui you propsed(that doesn't need mebedding)

@Abh1shxkk
Copy link
Copy Markdown
Contributor Author

@Abh1shxkk I prefer to keep it seperate, for the focus in this repo, can we follow somthing like the old ui?

for the other branch. you can give us the view for the new ui you propsed(that doesn't need mebedding)

Hey @tonychang04, got it — keeping them separate. For #982, should I update the UI to follow the existing AIPage.tsx style (inline stats/table within the same page layout) rather than a separate dashboard page? And should I hold off on showing embedding-specific fields until #981 merges, or include them with a fallback?

@Abh1shxkk
Copy link
Copy Markdown
Contributor Author

@Abh1shxkk I prefer to keep it seperate, for the focus in this repo, can we follow somthing like the old ui?

for the other branch. you can give us the view for the new ui you propsed(that doesn't need mebedding)

Hey @tonychang04, the UI in PR #982 already follows the same pattern as AIPage.tsx — same layout, same card/table structure, same styling conventions. The usageType field (chat/embedding/image) is there but gracefully handled until #981 merges. Would love your feedback on it!

Abh1shxkk added a commit to Abh1shxkk/InsForge that referenced this pull request Mar 21, 2026
…#981

- Add AIUsagePage with stats cards: Total Requests, Input/Output Tokens,
  Images Generated, Embedding Requests, Embedding Tokens
- Add date range filter (This week / This month / All time)
- Add usage records table with TypeBadge (chat/embedding/image_generation)
- Add /dashboard/ai/usage route in AppRoutes
- Add Models/Usage secondary menu under Model Gateway
- Support exact route matching in SecondaryMenu
- Update shared-schemas: usageTypeSchema, embeddingRequests, embeddingTokens

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
frontend/src/features/ai/pages/AIUsagePage.tsx (1)

280-287: Use theme tokens for the usage badges.

These bg-blue-100 / dark:bg-blue-900/30 style pairs hard-code palette values inside a themed page, so they can drift from the rest of the app when theme tokens change. Please switch the badge variants to CSS-variable-backed styles instead.

As per coding guidelines, "frontend/src/**/*.{tsx,jsx,css}: Maintain consistent color scheme using CSS variables".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@frontend/src/features/ai/pages/AIUsagePage.tsx` around lines 280 - 287, The
badge color classes in the styles map (styles, keys: chat, embedding,
image_generation) use hard-coded Tailwind palette classes; replace them with
CSS-variable–backed tokens: add theme CSS variables for each badge (e.g.,
--badge-chat-bg, --badge-chat-text, and dark-mode counterparts) in your
global/theme CSS, update the styles map to reference token-backed utility
classes or neutral classes combined with inline style variables (so the
className for the span uses those variables instead of
bg-blue-100/dark:bg-blue-900/30), and ensure dark-mode variants are provided by
the theme CSS (using .dark or prefers-color-scheme rules). Update
AIUsagePage.tsx to use the new token-backed styles for the three keys (chat,
embedding, image_generation) so badges follow the app theme tokens.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@frontend/src/features/ai/pages/AIUsagePage.tsx`:
- Around line 28-40: The filter labels say "This week"/"This month" but
getDateRange(range) computes rolling windows; change to calendar-anchored
ranges: for 'week' set startDate to the beginning of the current calendar week
(e.g., set time to 00:00:00 of the week's first day, choose Sunday or Monday
consistent with UI) and for 'month' set startDate to the first day of the
current month at 00:00:00 (use start.setDate(1) instead of subtracting a month
to avoid end-of-month rollover issues), keep endDate as now (or end of today if
preferred); update the same logic used in the other occurrence noted (lines
94-105) so both places use the anchored calendar-week and calendar-month
calculations in getDateRange and its duplicate.

---

Nitpick comments:
In `@frontend/src/features/ai/pages/AIUsagePage.tsx`:
- Around line 280-287: The badge color classes in the styles map (styles, keys:
chat, embedding, image_generation) use hard-coded Tailwind palette classes;
replace them with CSS-variable–backed tokens: add theme CSS variables for each
badge (e.g., --badge-chat-bg, --badge-chat-text, and dark-mode counterparts) in
your global/theme CSS, update the styles map to reference token-backed utility
classes or neutral classes combined with inline style variables (so the
className for the span uses those variables instead of
bg-blue-100/dark:bg-blue-900/30), and ensure dark-mode variants are provided by
the theme CSS (using .dark or prefers-color-scheme rules). Update
AIUsagePage.tsx to use the new token-backed styles for the three keys (chat,
embedding, image_generation) so badges follow the app theme tokens.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 76bd1543-6be2-4815-a80d-da228b581361

📥 Commits

Reviewing files that changed from the base of the PR and between 9dce89c and cf1bd3d.

📒 Files selected for processing (5)
  • frontend/src/components/layout/SecondaryMenu.tsx
  • frontend/src/features/ai/pages/AIUsagePage.tsx
  • frontend/src/lib/routing/AppRoutes.tsx
  • frontend/src/lib/utils/menuItems.ts
  • shared-schemas/src/ai.schema.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • shared-schemas/src/ai.schema.ts

Comment thread frontend/src/features/ai/pages/AIUsagePage.tsx Outdated
@Abh1shxkk
Copy link
Copy Markdown
Contributor Author

Hey @tonychang04, here's the frontend embedding UI running locally on the feat/embeddings-usage-type-979 branch:

AI Usage page at /dashboard/ai/usage with 6 stats cards: Total Requests, Input Tokens, Output Tokens, Images Generated, Embedding Requests, and Embedding Tokens
Usage records table with Time, Model, Type badge (chat/embedding/image_generation), Input tokens, Output tokens
Date range filter: This week / This month / All time
Secondary menu under Model Gateway with Models and Usage tabs
Everything is in this PR — migration, backend tracking, shared schemas, and frontend UI all together.
image

Contributor

@tonychang04
Copy link
Copy Markdown
Contributor

can you rebase the changes @Abh1shxkk and i can take a look at the embeddings ui once it is exposed?

@Abh1shxkk Abh1shxkk force-pushed the feat/embeddings-usage-type-979 branch from 429c362 to 7abad0b Compare March 28, 2026 04:25
Abh1shxkk added a commit to Abh1shxkk/InsForge that referenced this pull request Mar 28, 2026
…#981

- Add AIUsagePage with stats cards: Total Requests, Input/Output Tokens,
  Images Generated, Embedding Requests, Embedding Tokens
- Add date range filter (This week / This month / All time)
- Add usage records table with TypeBadge (chat/embedding/image_generation)
- Add /dashboard/ai/usage route in AppRoutes
- Add Models/Usage secondary menu under Model Gateway
- Support exact route matching in SecondaryMenu
- Update shared-schemas: usageTypeSchema, embeddingRequests, embeddingTokens

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Abh1shxkk Abh1shxkk force-pushed the feat/embeddings-usage-type-979 branch 3 times, most recently from 42245ad to 4f20715 Compare March 28, 2026 05:31
@Abh1shxkk
Copy link
Copy Markdown
Contributor Author

Hey @tonychang04 Rebased the branch on latest main and resolved all the merge conflicts. Here's what changed:

The usage tracking is now integrated directly into the Model Gateway page instead of being a separate page. There's a Usage button next to Gateway credentials that opens a clean modal showing all the stats. The modal has date filters (Today, 7 days, 30 days, All time) so you can drill into specific time periods. It shows total requests, input/output tokens, images generated, embedding requests and embedding tokens at a glance. Below the stats there's a scrollable activity table showing each request with the date, time, model name, type (Chat, Embed, Image) and token counts.

Also fixed the migration numbering conflict that came up during the rebase. Our usage_type migration is now 029 and the admin email fix is 030 so they run after all the upstream migrations.

The sidebar we had before (Models/Usage) is removed since everything lives on one page now. All files pass ESLint and Prettier checks.

image image

@Abh1shxkk
Copy link
Copy Markdown
Contributor Author

please let me know if there is any issue i am free today @tonychang04

@tonychang04
Copy link
Copy Markdown
Contributor

howevery for this pr embeddings can only be tracked when click usage... in the dashboard there is no place to enable and disable embeddings right?

@Abh1shxkk
Copy link
Copy Markdown
Contributor Author

howevery for this pr embeddings can only be tracked when click usage... in the dashboard there is no place to enable and disable embeddings right?

@tonychang04 just to clarify, clicking the Usage button does not control whether embeddings are tracked or not, it only opens the usage view.

In this PR, embedding requests are tracked automatically on the backend whenever the embeddings endpoint is used, and they show up separately in the usage modal with their own type and counts.

You’re right though that there isn’t a separate place on the main Model Gateway dashboard to explicitly enable/disable embeddings as a distinct UI flow in this branch. This PR is mainly about fixing the tracking so embeddings are no longer counted as chat. If you want, I can handle the dashboard-side UX for embeddings separately in a follow-up

@tonychang04
Copy link
Copy Markdown
Contributor

@Abh1shxkk yah if you can have 1 pr. that

  1. makes the embedding tracked seperately
  2. let the dashboard shows the embedding

That is the goal of the issue

Abh1shxkk and others added 6 commits April 12, 2026 01:44
…age_generation requests

- Migration 025: adds usage_type column (default 'chat'), backfills image_generation rows
- shared-schemas: adds usageTypeSchema, usageType field to aiUsageDataSchema/aiUsageRecordSchema, embeddingRequests/embeddingTokens to aiUsageSummarySchema
- ai-usage.service: new trackEmbeddingUsage(), trackChatUsage/trackImageGenerationUsage write explicit usage_type, getUsageSummary returns embedding breakdown, getAllUsage includes usage_type in SELECT
- embedding.service: use trackEmbeddingUsage() instead of trackChatUsage(), also thread source from sendRequest to skip tracking for BYOK

Closes InsForge#979

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Cast sendRequest return type explicitly to fix TS2339 errors on result/source
destructuring. Add OpenAI.Embedding type annotation to fix implicit any on map callback.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…rom sendRequest

sendRequest() on this branch returns T not { result, source }, so source was
always undefined and the BYOK guard was ineffective. Switch to isByokActive()
which correctly detects BYOK from the provider.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…#981

- Add AIUsagePage with stats cards: Total Requests, Input/Output Tokens,
  Images Generated, Embedding Requests, Embedding Tokens
- Add date range filter (This week / This month / All time)
- Add usage records table with TypeBadge (chat/embedding/image_generation)
- Add /dashboard/ai/usage route in AppRoutes
- Add Models/Usage secondary menu under Model Gateway
- Support exact route matching in SecondaryMenu
- Update shared-schemas: usageTypeSchema, embeddingRequests, embeddingTokens

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Abh1shxkk and others added 4 commits April 12, 2026 01:46
…te ranges

- Add usageTypeSchema enum and usageType field to aiUsageDataSchema so
  ai-usage.service.ts trackUsage() typechecks correctly
- Fix getDateRange: 'week' now anchors to start of current calendar week
  (Sunday 00:00:00), 'month' anchors to first day of current month 00:00:00

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Show last 10 usage records in AIPage with type badges (chat/embedding/image_generation)
- Remove separate AIUsagePage, route, and secondary menu — fits existing UI

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ture

Update @/components alias to relative import path to match the
packages/dashboard package convention.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Abh1shxkk Abh1shxkk force-pushed the feat/embeddings-usage-type-979 branch from 4f20715 to e9cd568 Compare April 11, 2026 20:21
- AILayout now renders AISidebar + Outlet (Models / Usage tabs)
- Register /dashboard/ai/usage route pointing to AIUsagePage
- Remove UsageDialog modal and Usage button from AIPage — sidebar nav
  replaces it
- Fix packages/dashboard tsconfig: add rootDir to silence TS warning

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

Embeddings usage not tracked or displayed separately from chat completions

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.