Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 39 additions & 30 deletions convex/_ai/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const GENERATION_MODELS = {
export const CHAT_MODELS: Record<
string,
{
name: string;
provider: "anthropic" | "openai" | "google";
inputPer1M: number;
outputPer1M: number;
Expand All @@ -60,6 +61,7 @@ export const CHAT_MODELS: Record<
}
> = {
"claude-opus-5-5": {
name: "Claude Opus 5.5",
provider: "anthropic",
inputPer1M: 4,
outputPer1M: 20,
Expand All @@ -68,25 +70,29 @@ export const CHAT_MODELS: Record<
},
// Refusal fallback targets for Opus 5.5, priced only for recorded cost.
"claude-opus-5": {
name: "Claude Opus 5",
provider: "anthropic",
inputPer1M: 5,
outputPer1M: 25,
quotaWeight: 2.5,
},
"claude-opus-4-8": {
name: "Claude Opus 4.8",
provider: "anthropic",
inputPer1M: 5,
outputPer1M: 25,
quotaWeight: 2.5,
},
"claude-sonnet-5-5": {
name: "Claude Sonnet 5.5",
provider: "anthropic",
inputPer1M: 2,
outputPer1M: 10,
quotaWeight: 1,
},
// Available on an owner's own key only; priced for recorded cost.
"claude-haiku-5-5": {
name: "Claude Haiku 5.5",
provider: "anthropic",
inputPer1M: 0.1,
outputPer1M: 0.5,
Expand All @@ -98,6 +104,7 @@ export const CHAT_MODELS: Record<
},
},
"gpt-6.2-sol": {
name: "GPT-6.2 Sol",
provider: "openai",
inputPer1M: 2,
outputPer1M: 10,
Expand All @@ -110,6 +117,7 @@ export const CHAT_MODELS: Record<
},
},
"gpt-6.1-sol": {
name: "GPT-6.1 Sol",
provider: "openai",
inputPer1M: 2,
outputPer1M: 10,
Expand All @@ -121,6 +129,7 @@ export const CHAT_MODELS: Record<
},
},
[LEGACY_GOOGLE_MODELS.chatModelId]: {
name: "Gemini 2.5 Flash",
provider: LEGACY_GOOGLE_MODELS.provider,
inputPer1M: LEGACY_GOOGLE_MODELS.chatInputPer1M,
outputPer1M: LEGACY_GOOGLE_MODELS.chatOutputPer1M,
Expand Down Expand Up @@ -173,10 +182,28 @@ export type UserKeyModel = {
name: string;
efforts: readonly ModelEffort[];
defaultEffort: ModelEffort;
// USD per 1M tokens, so owners can compare models before choosing one.
inputPer1M: number;
outputPer1M: number;
};

const EFFORTS: readonly ModelEffort[] = ["low", "medium", "high"];

function userKeyOption(
modelId: string,
defaultEffort: ModelEffort,
): UserKeyModel {
const { name, inputPer1M, outputPer1M } = CHAT_MODELS[modelId];
return {
modelId,
name,
efforts: EFFORTS,
defaultEffort,
inputPer1M,
outputPer1M,
};
}

/**
* Models an owner can choose for their own provider key, the default first.
* The provider bills that usage, so it never counts against Gitdog tokens.
Expand All @@ -186,43 +213,25 @@ const EFFORTS: readonly ModelEffort[] = ["low", "medium", "high"];
*/
export const USER_KEY_MODELS: Record<"anthropic" | "openai", UserKeyModel[]> = {
anthropic: [
{
modelId: "claude-opus-5-5",
name: "Claude Opus 5.5",
efforts: EFFORTS,
defaultEffort: "medium",
},
{
modelId: "claude-sonnet-5-5",
name: "Claude Sonnet 5.5",
efforts: EFFORTS,
defaultEffort: "high",
},
{
modelId: "claude-haiku-5-5",
name: "Claude Haiku 5.5",
efforts: EFFORTS,
defaultEffort: "medium",
},
userKeyOption("claude-opus-5-5", "medium"),
userKeyOption("claude-sonnet-5-5", "high"),
userKeyOption("claude-haiku-5-5", "medium"),
],
// Keys saved before GPT-6.2 Sol were checked against 6.1 and store no
// model, so 6.1 stays the default they resolve to.
openai: [
{
modelId: "gpt-6.1-sol",
name: "GPT-6.1 Sol",
efforts: EFFORTS,
defaultEffort: "high",
},
{
modelId: "gpt-6.2-sol",
name: "GPT-6.2 Sol",
efforts: EFFORTS,
defaultEffort: "high",
},
userKeyOption("gpt-6.1-sol", "high"),
userKeyOption("gpt-6.2-sol", "high"),
],
};

/** A chat model's display name, or its id when Gitdog never priced it. */
export function chatModelName(modelId: string): string {
return Object.hasOwn(CHAT_MODELS, modelId)
? CHAT_MODELS[modelId].name
: modelId;
}

/**
* The model and effort an owner's key runs with. A choice that is no longer
* offered falls back to the default, so a key never stops working.
Expand Down
2 changes: 2 additions & 0 deletions convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ import type * as tokenUsage_periodTotals from "../tokenUsage/periodTotals.js";
import type * as tokenUsage_query from "../tokenUsage/query.js";
import type * as tokenUsage_quota from "../tokenUsage/quota.js";
import type * as tokenUsage_totals from "../tokenUsage/totals.js";
import type * as tokenUsage_usageSummary from "../tokenUsage/usageSummary.js";
import type * as users_action from "../users/action.js";
import type * as users_internalQuery from "../users/internalQuery.js";
import type * as users_legal from "../users/legal.js";
Expand Down Expand Up @@ -339,6 +340,7 @@ declare const fullApi: ApiFromModules<{
"tokenUsage/query": typeof tokenUsage_query;
"tokenUsage/quota": typeof tokenUsage_quota;
"tokenUsage/totals": typeof tokenUsage_totals;
"tokenUsage/usageSummary": typeof tokenUsage_usageSummary;
"users/action": typeof users_action;
"users/internalQuery": typeof users_internalQuery;
"users/legal": typeof users_legal;
Expand Down
139 changes: 95 additions & 44 deletions convex/tokenUsage/query.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { v } from "convex/values";
import { query, type QueryCtx } from "../_generated/server";
import type { Doc, Id } from "../_generated/dataModel";
import { getAuthUserId } from "@convex-dev/auth/server";
import { MAX_COLLECT_LIMIT } from "../_utils/pagination";
import { getSubscriptionForEntitlement } from "../subscriptions/entitlement";
import { readEffectiveSubscriptionQuota } from "../subscriptions/quotaEntitlement";
import { readCompletePeriodTotals } from "./periodTotals";
import { summarizeActionUsage, summarizeUserKeyUsage } from "./usageSummary";

export const MAX_TOKEN_USAGE_AGGREGATION_RECORDS = MAX_COLLECT_LIMIT;

Expand Down Expand Up @@ -55,7 +57,8 @@ export function assertCompleteUsageAggregation(recordCount: number): void {

/**
* Usage on the user's own provider keys. It never counts toward quota, so a
* busy period is reported as a floor instead of failing the usage page.
* busy period is reported as a floor instead of failing the usage page. Rows
* come newest first, so a partial read still holds the latest actions.
*/
async function readUserKeyUsage(
ctx: QueryCtx,
Expand All @@ -72,6 +75,7 @@ async function readUserKeyUsage(
.gte("createdAt", periodStart)
.lt("createdAt", periodEnd),
)
.order("desc")
.take(MAX_TOKEN_USAGE_AGGREGATION_RECORDS + 1);
return {
rows: rows.slice(0, MAX_TOKEN_USAGE_AGGREGATION_RECORDS),
Expand Down Expand Up @@ -118,14 +122,6 @@ export const getCurrentMonthUsage = query({
totalCostCents: period.gitdog.totalCostCents,
totalActions: period.gitdog.chatActions,
totalEmbeddingTokens: period.gitdog.embeddingTokens,
userKey: {
inputTokens: period.userKey.inputTokens,
outputTokens: period.userKey.outputTokens,
totalTokens: period.userKey.totalTokens,
totalActions: period.userKey.rowCount,
estimatedProviderCostCents: period.userKey.baseCostCents,
isPartial: false,
},
paymentStatus: paymentStatus as "waived" | "pending" | "paid",
};
}
Expand All @@ -142,12 +138,6 @@ export const getCurrentMonthUsage = query({
)
.take(MAX_TOKEN_USAGE_AGGREGATION_RECORDS + 1);
assertCompleteUsageAggregation(usageRecords.length);
const userKeyRecords = await readUserKeyUsage(
ctx,
userId,
periodStart,
periodEnd,
);

// `totalTokens` keeps cost-equivalent quota usage, while
// `embeddingTokens` preserves the provider's actual embedding input count.
Expand Down Expand Up @@ -198,38 +188,9 @@ export const getCurrentMonthUsage = query({
);
}

const userKey = {
inputTokens: 0,
outputTokens: 0,
totalTokens: 0,
totalActions: 0,
estimatedProviderCostCents: 0,
isPartial: userKeyRecords.isPartial,
};
for (const record of userKeyRecords.rows) {
userKey.inputTokens = checkedTokenSum(
userKey.inputTokens,
record.inputTokens,
);
userKey.outputTokens = checkedTokenSum(
userKey.outputTokens,
record.outputTokens,
);
userKey.totalTokens = checkedTokenSum(
userKey.totalTokens,
record.totalTokens,
);
userKey.totalActions = checkedTokenSum(userKey.totalActions, 1);
userKey.estimatedProviderCostCents = checkedCostSum(
userKey.estimatedProviderCostCents,
record.baseCostCents,
);
}

return {
yearMonth,
...totals,
userKey,
paymentStatus: paymentStatus as "waived" | "pending" | "paid",
};
},
Expand Down Expand Up @@ -375,3 +336,93 @@ export const getUsageByRepository = query({
return Array.from(usageByRepo.values());
},
});

const RECENT_USER_KEY_ACTIONS = 10;

/**
* This period's usage on the owner's own keys, priced as the provider bills
* it: totals, each model, each repository and the most recent actions.
*/
export const getUserKeyUsage = query({
args: {},
handler: async (ctx) => {
const userId = await getAuthUserId(ctx);
if (!userId) return null;

const subscription = await getSubscriptionForEntitlement(ctx, userId);
const quota = await readEffectiveSubscriptionQuota(ctx, subscription);
// Own-key usage never touches quota, so a quota problem only hides it;
// the keys page that shows it must keep working.
if (!quota.verified) return null;
const { periodStart, periodEnd } = quota.entitlement;

const { rows, isPartial } = await readUserKeyUsage(
ctx,
userId,
periodStart,
periodEnd,
);
const { actions, ...summary } = summarizeUserKeyUsage(rows);

const recentActions = await Promise.all(
actions.slice(0, RECENT_USER_KEY_ACTIONS).map(async (usage) => {
const action = await ctx.db.get(usage.ticketActionId);
const ticket = action && (await ctx.db.get(action.ticketId));
if (!action || !ticket || ticket.userId !== userId) return null;
return {
...usage,
actionType: action.actionType,
// Deleted tickets keep their cost but no longer link anywhere.
ticketId: ticket.deletedAt ? null : ticket._id,
ticketType: ticket.ticketType,
githubRepo: ticket.githubRepo,
githubNumber: ticket.githubNumber,
githubTitle: ticket.githubTitle,
};
}),
);

return {
isPartial,
...summary,
recentActions: recentActions.filter((entry) => entry !== null),
};
},
});

// Bounded well under a query's read limit: the newest actions, and more rows
// per action than the largest batched review writes.
const MAX_TICKET_USAGE_ACTIONS = 40;
const MAX_ACTION_USAGE_ROWS = 300;

/** What each action on a ticket used and whose key paid for it. */
export const getTicketUsage = query({
args: { ticketId: v.id("tickets") },
handler: async (ctx, args) => {
const userId = await getAuthUserId(ctx);
if (!userId) return [];
const ticket = await ctx.db.get(args.ticketId);
if (!ticket || ticket.userId !== userId || ticket.deletedAt) return [];

const actions = await ctx.db
.query("ticketActions")
.withIndex("by_ticket", (q) => q.eq("ticketId", ticket._id))
.order("desc")
.take(MAX_TICKET_USAGE_ACTIONS);
return await Promise.all(
actions.map(async (action) => {
const rows = await ctx.db
.query("tokenUsage")
.withIndex("by_ticket_action", (q) =>
q.eq("ticketActionId", action._id),
)
.take(MAX_ACTION_USAGE_ROWS + 1);
return {
ticketActionId: action._id,
isPartial: rows.length > MAX_ACTION_USAGE_ROWS,
...summarizeActionUsage(rows.slice(0, MAX_ACTION_USAGE_ROWS)),
};
}),
);
},
});
Loading