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
4 changes: 4 additions & 0 deletions graphile/graphile-llm/src/metering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export interface MeteringContext {
billing: BillingConfig;
/** Entity ID to meter against (from JWT claims) */
entityId: string;
/** Per-request correlation ID (from request.id pgSetting) */
requestId: string | null;
}

export interface MeteringOptions {
Expand Down Expand Up @@ -185,6 +187,7 @@ export async function meteredEmbed(
// Record actual usage (input_chars as the metered amount)
ctx.withPgClient(ctx.pgSettings, async (pgClient) => {
await recordUsage(pgClient, ctx.billing, ctx.entityId, meterSlug, text.length, {
request_id: ctx.requestId,
input_chars: text.length,
dims: result.length,
latency_ms: latencyMs,
Expand Down Expand Up @@ -271,6 +274,7 @@ export async function meteredChat(
const inputChars = messages.reduce((sum, m) => sum + m.content.length, 0);
ctx.withPgClient(ctx.pgSettings, async (pgClient) => {
await recordUsage(pgClient, ctx.billing, ctx.entityId, meterSlug, inputChars + result.length, {
request_id: ctx.requestId,
input_chars: inputChars,
output_chars: result.length,
messages_count: messages.length,
Expand Down
2 changes: 2 additions & 0 deletions graphile/graphile-llm/src/plugins/metering-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ async function buildMeteringContext(
const pgSettings: Record<string, string> = graphqlContext?.pgSettings ?? {};
const entityId = resolveEntityId(pgSettings);
const databaseId = pgSettings['jwt.claims.database_id'] ?? null;
const requestId = pgSettings['request.id'] ?? null;
if (!entityId || !databaseId) return null;

const withPgClient: WithPgClient | undefined = graphqlContext?.withPgClient;
Expand All @@ -88,6 +89,7 @@ async function buildMeteringContext(
pgSettings,
billing: billingConfig,
entityId,
requestId,
};
}

Expand Down
17 changes: 13 additions & 4 deletions graphql/server/src/middleware/graphile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,24 @@ const buildPreset = (
pgSettings['default_transaction_read_only'] = 'on';
}

if (req.requestId) {
pgSettings['request.id'] = req.requestId;
}

return { pgSettings };
}
}

const anonSettings: Record<string, string> = {
role: anonRole,
...context,
};
if (req?.requestId) {
anonSettings['request.id'] = req.requestId;
}

return {
pgSettings: {
role: anonRole,
...context,
},
pgSettings: anonSettings,
};
},
},
Expand Down
3 changes: 3 additions & 0 deletions graphql/server/src/middleware/llm-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ function getPgSettings(req: Request): Record<string, string> {
if (req.databaseId) {
settings['jwt.claims.database_id'] = req.databaseId;
}
if (req.requestId) {
settings['request.id'] = req.requestId;
}

return settings;
}
Expand Down
Loading