Skip to content

Store and visualize use-case, model, token usage #1118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
139 changes: 138 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions packages/cdk/lambda/getTokenUsage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { aggregateTokenUsage } from './repository';
import { GetTokenUsageEvent } from 'generative-ai-use-cases';

export const handler = async (event: GetTokenUsageEvent) => {
try {
console.log('Getting token usage statistics', { event });

// Get user ID from Cognito
const userId = event.requestContext.authorizer!.claims['cognito:username'];
const { startDate, endDate } = event.queryStringParameters || {};

if (!startDate || !endDate) {
return {
statusCode: 400,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify({
message: 'startDate and endDate parameters are required',
}),
};
}

// Get aggregated data for the specified period
const stats = await aggregateTokenUsage(startDate, endDate, [userId]);

return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify(stats),
};
} catch (error) {
console.error('Error getting token usage statistics:', error);
return {
statusCode: 500,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify({
message: 'Internal server error',
error: error instanceof Error ? error.message : 'Unknown error',
}),
};
}
};
Loading