Agent: Added UsagePerRequest, Example: Added an example for custom cost component. #164
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This pull request introduces a robust framework for granular usage tracking within the core agent and adds a new, standalone example to demonstrate a practical application: real-time cost calculation.
The changes are split into two main parts:
Core Agent Changes
These changes provide the foundational capabilities for usage tracking.
feat:
usagePerRequest
TableusagePerRequest
table has been created to log usage metrics for each individual step or API call an agent makes. This provides highly granular data for monitoring and analysis.feat: Expose IDs in
onStepFinish
onStepFinish
callback now exposesmessageId
,threadId
, anduserId
. This allows developers to easily correlate usage data with specific user interactions and conversation threads.chore: Added Helper Functions
usagePerRequest
table more convenient.New Example: Real-time Cost Component
To showcase the power of the new usage tracking features, a comprehensive new example has been added. This example is self-contained and demonstrates how to:
Create a Dynamic Pricing Table:
pricing
table that periodically fetches and caches the latest model prices from an external source (models.dev
) every 5 minutes, ensuring cost calculations are always up-to-date.Build a Custom Cost Component:
Cost
) that consumes datapricing
tables. This component calculates and displays the cost of agent operations in real-time for the end-user.