files/en-us/web/api/summarizer/measureinputusage/index.md
{{APIRef("Summarizer API")}}{{SeeCompatTable}}{{securecontext_header}}
The measureInputUsage() method of the {{domxref("Summarizer")}} interface reports how much {{domxref("Summarizer.inputQuota", "inputQuota")}} would be used by a summarize operation for a given text input.
measureInputUsage(input)
measureInputUsage(input, options)
input
options {{optional_inline}}
measureInputUsage() operation. Possible values include:
context
signal
measureInputUsage() operation to be aborted via the associated {{domxref("AbortController")}}.A {{jsxref("Promise")}} that fulfills with a number specifying the {{domxref("Summarizer.inputQuota", "inputQuota")}} usage of the given input text.
NotAllowedError {{domxref("DOMException")}}
NotReadableError {{domxref("DOMException")}}
NotSupportedError {{domxref("DOMException")}}
context is not in language the Summarizer supports.UnknownError {{domxref("DOMException")}}
measureInputUsage() call failed for any other reason, or a reason the user agent did not wish to disclose.In the below snippet, we create a new Summarizer instance using {{domxref("Summarizer.create_static", "create()")}}, then return the total input quota via {{domxref("Summarizer.inputQuota", "inputQuota")}} and the input quota usage for a summarizing a particular text string via measureInputUsage().
We then test to see if the individual input usage for that string is great than the total available quota. If so, we throw an appropriate error; it not, we commence summarizing the string using {{domxref("Summarizer.summarize", "summarize()")}}.
const summarizer = await Summarizer.create({
sharedContext:
"A general summary to help a user decide if the text is worth reading",
type: "tldr",
length: "short",
});
const totalInputQuota = summarizer.inputQuota;
const inputUsage = await summarizer.measureInputUsage(myTextString);
if (inputUsage > totalInputQuota) {
throw new Error("Boo, insufficient quota to generate a summary.");
} else {
console.log("Yay, quota available to generate a summary.");
const summary = await summarizer.summarize(myTextString);
// ...
}
{{Specifications}}
{{Compat}}