Usage Object Members in SuiteScript for Token Management
Understand the Usage Object Members in SuiteScript for managing tokens in LLM requests and responses.
The Usage Object Members in SuiteScript provide essential metrics for tracking the token usage when interacting with large language models (LLMs). This includes properties that monitor requests and responses, which is crucial for optimizing API usage and managing costs effectively.
What Are Usage Object Members?
The Usage Object contains properties that give developers insight into how many tokens are being processed during interactions with LLMs. These members specifically cater to server scripts and track tokens as follows:
| Member Name | Return Type | Description |
|---|---|---|
Usage.completionTokens | number | Number of tokens in the response from the LLM. |
Usage.promptTokens | number | Number of tokens in the request sent to the LLM. |
Usage.totalTokens | number | Total number of tokens for the entire LLM interaction. |
Property Descriptions
Usage.completionTokens: This property returns the number of tokens in the LLM's response, allowing for efficiency assessments in output generation.Usage.promptTokens: It indicates the number of tokens included in the prompt sent to the LLM, necessary for understanding request sizes and potential costs.Usage.totalTokens: Represents the total tokens utilized in the request/response cycle, which is essential for overall resource management.
Error Codes
When working with these members, you may encounter specific error codes:
READ_ONLY: This error occurs if there is an attempt to set the value of a property that is read-only.
Example Usage in Scripts
Here's a sample code snippet demonstrating how to access the token properties within a SuiteScript:
1// Sample SuiteScript demonstrating usage object members2const response = llm.generateText({3 prompt: "Write a 200 word pitch for a TV show about elephants."4});5 6const usageCompletionTokens = response.usage.completionTokens; 7const usagePromptTokens = response.usage.promptTokens; 8const totalTokens = response.usage.totalTokens;This snippet showcases the retrieval of token-related data after generating text through the LLM, which can be invaluable for monitoring and optimizing your LLM utilization.
Who This Affects
- Administrators: Those managing API quotas and monitoring system resources.
- Developers: Individuals implementing LLM features using SuiteScript.
Key Takeaways
- The Usage Object Members help track token usage efficiently during LLM interactions.
- Crucial for developers to manage request costs and optimize performance.
- Understanding token metrics can enhance API management and application responsiveness.
Source: This article is based on Oracle's official NetSuite documentation.
Frequently Asked Questions (4)
How can SuiteScript developers use the Usage Object Members to optimize API usage?
What should developers be aware of when working with Usage Object Member properties in SuiteScript?
Do Usage Object Members apply to all types of NetSuite scripts?
What kind of information can be gathered from `Usage.totalTokens` in the Usage Object?
Was this article helpful?
More in SuiteScript
- SuiteScript 2.1 Enhancements in NetSuite February Updates
SuiteScript 2.1 now supports async features and PATCH method. Discover the latest API and SuiteProcurement improvements.
- Custom Tool Script Enhancements in NetSuite
Custom tool scripts in NetSuite gain execution log support and a new management page in February 16, 2026.
- Scheduling Map/Reduce Script Deployments in NetSuite
Learn to schedule map/reduce script submissions, including one-time and recurring options in NetSuite.
- API Governance Units Calculation in NetSuite 2026.1
NetSuite 2026.1 introduces examples illustrating API governance unit calculations for both user event and scheduled scripts.
