Skip to content

Commit

Permalink
Log token usage of AI SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
Rindrics committed Oct 21, 2024
1 parent a6e8332 commit bdfdbfb
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/(playground)/p/[agentId]/beta-proto/graph/server-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ type GenerateArtifactStreamParams = {
userPrompt: string;
systemPrompt?: string;
};

function recordTokenUsage({
promptTokens,
completionTokens,
totalTokens,
}: TokenUsage) {
console.log("Prompt tokens:", promptTokens);
console.log("Completion tokens:", completionTokens);
console.log("Total tokens:", totalTokens);
}

export async function generateArtifactStream(
params: GenerateArtifactStreamParams,
) {
Expand Down Expand Up @@ -67,6 +78,8 @@ export async function generateArtifactStream(

const result = await object;

result.usage.then(recordTokenUsage);

stream.done();
})();

Expand Down

0 comments on commit bdfdbfb

Please sign in to comment.