Usage Object Members in SuiteScript for Token Management

Understand the Usage Object Members in SuiteScript for managing tokens in LLM requests and responses.

·2 min read·View Oracle Docs

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 NameReturn TypeDescription
Usage.completionTokensnumberNumber of tokens in the response from the LLM.
Usage.promptTokensnumberNumber of tokens in the request sent to the LLM.
Usage.totalTokensnumberTotal 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:

suitescript
1// Sample SuiteScript demonstrating usage object members
2const 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?
Developers can use the Usage Object Members to track the number of tokens in both requests and responses with LLMs, making it possible to manage API usage costs effectively by monitoring these metrics.
What should developers be aware of when working with Usage Object Member properties in SuiteScript?
Developers should note that properties such as `Usage.completionTokens`, `Usage.promptTokens`, and `Usage.totalTokens` are read-only and any attempt to modify these values will result in a `READ_ONLY` error.
Do Usage Object Members apply to all types of NetSuite scripts?
Usage Object Members are specifically designed for use within server scripts that engage with large language models, enabling token tracking during these interactions.
What kind of information can be gathered from `Usage.totalTokens` in the Usage Object?
The `Usage.totalTokens` property provides the total number of tokens utilized throughout the request/response cycle, which is essential for understanding the overall resource consumption during LLM interactions.
Source: Usage Object Members Oracle NetSuite Help Center. This article was generated from official Oracle documentation and enriched with additional context and best practices.

Was this article helpful?

More in SuiteScript

View all SuiteScript articles →