Skip to content

Commit df1288b

Browse files
jaycee-licopybara-github
authored andcommitted
feat: Add ServiceTier to UsageMetadata
PiperOrigin-RevId: 926773736
1 parent 54da8a4 commit df1288b

3 files changed

Lines changed: 124 additions & 49 deletions

File tree

src/main/java/com/google/genai/types/MediaModality.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,27 @@
2323
import com.google.common.base.Ascii;
2424
import java.util.Objects;
2525

26-
/** Server content modalities. */
26+
/** The modality that this token count applies to. */
2727
public class MediaModality {
2828

2929
/** Enum representing the known values for MediaModality. */
3030
public enum Known {
31-
/** The modality is unspecified. */
31+
/** When a modality is not specified, it is treated as `TEXT`. */
3232
MODALITY_UNSPECIFIED,
3333

34-
/** Plain text. */
34+
/** The `Part` contains plain text. */
3535
TEXT,
3636

37-
/** Images. */
37+
/** The `Part` contains an image. */
3838
IMAGE,
3939

40-
/** Video. */
40+
/** The `Part` contains a video. */
4141
VIDEO,
4242

43-
/** Audio. */
43+
/** The `Part` contains audio. */
4444
AUDIO,
4545

46-
/** Document, e.g. PDF. */
46+
/** The `Part` contains a document, such as a PDF. */
4747
DOCUMENT,
4848

4949
MEDIA_MODALITY_UNSPECIFIED

src/main/java/com/google/genai/types/ModalityTokenCount.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,17 @@
2626
import com.google.genai.JsonSerializable;
2727
import java.util.Optional;
2828

29-
/** Represents token counting info for a single modality. */
29+
/**
30+
* Represents a breakdown of token usage by modality. This message is used in CountTokensResponse
31+
* and GenerateContentResponse.UsageMetadata to provide a detailed view of how many tokens are used
32+
* by each modality (e.g., text, image, video) in a request. This is particularly useful for
33+
* multimodal models, allowing you to track and manage token consumption for billing and quota
34+
* purposes.
35+
*/
3036
@AutoValue
3137
@JsonDeserialize(builder = ModalityTokenCount.Builder.class)
3238
public abstract class ModalityTokenCount extends JsonSerializable {
33-
/** The modality associated with this token count. */
39+
/** The modality that this token count applies to. */
3440
@JsonProperty("modality")
3541
public abstract Optional<MediaModality> modality();
3642

@@ -59,7 +65,7 @@ private static Builder create() {
5965
/**
6066
* Setter for modality.
6167
*
62-
* <p>modality: The modality associated with this token count.
68+
* <p>modality: The modality that this token count applies to.
6369
*/
6470
@JsonProperty("modality")
6571
public abstract Builder modality(MediaModality modality);
@@ -77,7 +83,7 @@ public Builder clearModality() {
7783
/**
7884
* Setter for modality given a known enum.
7985
*
80-
* <p>modality: The modality associated with this token count.
86+
* <p>modality: The modality that this token count applies to.
8187
*/
8288
@CanIgnoreReturnValue
8389
public Builder modality(MediaModality.Known knownType) {
@@ -87,7 +93,7 @@ public Builder modality(MediaModality.Known knownType) {
8793
/**
8894
* Setter for modality given a string.
8995
*
90-
* <p>modality: The modality associated with this token count.
96+
* <p>modality: The modality that this token count applies to.
9197
*/
9298
@CanIgnoreReturnValue
9399
public Builder modality(String modality) {

src/main/java/com/google/genai/types/UsageMetadata.java

Lines changed: 106 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,55 +35,71 @@
3535
@JsonDeserialize(builder = UsageMetadata.Builder.class)
3636
public abstract class UsageMetadata extends JsonSerializable {
3737
/**
38-
* Number of tokens in the prompt. When `cached_content` is set, this is still the total effective
39-
* prompt size meaning this includes the number of tokens in the cached content.
38+
* The total number of tokens in the prompt. This includes any text, images, or other media
39+
* provided in the request. When `cached_content` is set, this also includes the number of tokens
40+
* in the cached content.
4041
*/
4142
@JsonProperty("promptTokenCount")
4243
public abstract Optional<Integer> promptTokenCount();
4344

44-
/** Number of tokens in the cached part of the prompt (the cached content). */
45+
/** Output only. The number of tokens in the cached content that was used for this request. */
4546
@JsonProperty("cachedContentTokenCount")
4647
public abstract Optional<Integer> cachedContentTokenCount();
4748

4849
/** Total number of tokens across all the generated response candidates. */
4950
@JsonProperty("responseTokenCount")
5051
public abstract Optional<Integer> responseTokenCount();
5152

52-
/** Number of tokens present in tool-use prompt(s). */
53+
/**
54+
* Output only. The number of tokens in the results from tool executions, which are provided back
55+
* to the model as input, if applicable.
56+
*/
5357
@JsonProperty("toolUsePromptTokenCount")
5458
public abstract Optional<Integer> toolUsePromptTokenCount();
5559

56-
/** Number of tokens of thoughts for thinking models. */
60+
/**
61+
* Output only. The number of tokens that were part of the model's generated "thoughts" output, if
62+
* applicable.
63+
*/
5764
@JsonProperty("thoughtsTokenCount")
5865
public abstract Optional<Integer> thoughtsTokenCount();
5966

60-
/** Total token count for prompt, response candidates, and tool-use prompts(if present). */
67+
/**
68+
* The total number of tokens for the entire request. This is the sum of `prompt_token_count`,
69+
* `candidates_token_count`, `tool_use_prompt_token_count`, and `thoughts_token_count`.
70+
*/
6171
@JsonProperty("totalTokenCount")
6272
public abstract Optional<Integer> totalTokenCount();
6373

64-
/** List of modalities that were processed in the request input. */
74+
/** Output only. A detailed breakdown of the token count for each modality in the prompt. */
6575
@JsonProperty("promptTokensDetails")
6676
public abstract Optional<List<ModalityTokenCount>> promptTokensDetails();
6777

68-
/** List of modalities that were processed in the cache input. */
78+
/**
79+
* Output only. A detailed breakdown of the token count for each modality in the cached content.
80+
*/
6981
@JsonProperty("cacheTokensDetails")
7082
public abstract Optional<List<ModalityTokenCount>> cacheTokensDetails();
7183

7284
/** List of modalities that were returned in the response. */
7385
@JsonProperty("responseTokensDetails")
7486
public abstract Optional<List<ModalityTokenCount>> responseTokensDetails();
7587

76-
/** List of modalities that were processed in the tool-use prompt. */
88+
/**
89+
* Output only. A detailed breakdown by modality of the token counts from the results of tool
90+
* executions, which are provided back to the model as input.
91+
*/
7792
@JsonProperty("toolUsePromptTokensDetails")
7893
public abstract Optional<List<ModalityTokenCount>> toolUsePromptTokensDetails();
7994

80-
/**
81-
* Traffic type. This shows whether a request consumes Pay-As-You-Go or Provisioned Throughput
82-
* quota.
83-
*/
95+
/** Output only. The traffic type for this request. This field is not supported in Gemini API. */
8496
@JsonProperty("trafficType")
8597
public abstract Optional<TrafficType> trafficType();
8698

99+
/** Output only. Service tier of the request. This field is not supported in Vertex AI. */
100+
@JsonProperty("serviceTier")
101+
public abstract Optional<ServiceTier> serviceTier();
102+
87103
/** Instantiates a builder for UsageMetadata. */
88104
@ExcludeFromGeneratedCoverageReport
89105
public static Builder builder() {
@@ -105,9 +121,9 @@ private static Builder create() {
105121
/**
106122
* Setter for promptTokenCount.
107123
*
108-
* <p>promptTokenCount: Number of tokens in the prompt. When `cached_content` is set, this is
109-
* still the total effective prompt size meaning this includes the number of tokens in the
110-
* cached content.
124+
* <p>promptTokenCount: The total number of tokens in the prompt. This includes any text,
125+
* images, or other media provided in the request. When `cached_content` is set, this also
126+
* includes the number of tokens in the cached content.
111127
*/
112128
@JsonProperty("promptTokenCount")
113129
public abstract Builder promptTokenCount(Integer promptTokenCount);
@@ -125,8 +141,8 @@ public Builder clearPromptTokenCount() {
125141
/**
126142
* Setter for cachedContentTokenCount.
127143
*
128-
* <p>cachedContentTokenCount: Number of tokens in the cached part of the prompt (the cached
129-
* content).
144+
* <p>cachedContentTokenCount: Output only. The number of tokens in the cached content that was
145+
* used for this request.
130146
*/
131147
@JsonProperty("cachedContentTokenCount")
132148
public abstract Builder cachedContentTokenCount(Integer cachedContentTokenCount);
@@ -162,7 +178,8 @@ public Builder clearResponseTokenCount() {
162178
/**
163179
* Setter for toolUsePromptTokenCount.
164180
*
165-
* <p>toolUsePromptTokenCount: Number of tokens present in tool-use prompt(s).
181+
* <p>toolUsePromptTokenCount: Output only. The number of tokens in the results from tool
182+
* executions, which are provided back to the model as input, if applicable.
166183
*/
167184
@JsonProperty("toolUsePromptTokenCount")
168185
public abstract Builder toolUsePromptTokenCount(Integer toolUsePromptTokenCount);
@@ -180,7 +197,8 @@ public Builder clearToolUsePromptTokenCount() {
180197
/**
181198
* Setter for thoughtsTokenCount.
182199
*
183-
* <p>thoughtsTokenCount: Number of tokens of thoughts for thinking models.
200+
* <p>thoughtsTokenCount: Output only. The number of tokens that were part of the model's
201+
* generated "thoughts" output, if applicable.
184202
*/
185203
@JsonProperty("thoughtsTokenCount")
186204
public abstract Builder thoughtsTokenCount(Integer thoughtsTokenCount);
@@ -198,8 +216,9 @@ public Builder clearThoughtsTokenCount() {
198216
/**
199217
* Setter for totalTokenCount.
200218
*
201-
* <p>totalTokenCount: Total token count for prompt, response candidates, and tool-use
202-
* prompts(if present).
219+
* <p>totalTokenCount: The total number of tokens for the entire request. This is the sum of
220+
* `prompt_token_count`, `candidates_token_count`, `tool_use_prompt_token_count`, and
221+
* `thoughts_token_count`.
203222
*/
204223
@JsonProperty("totalTokenCount")
205224
public abstract Builder totalTokenCount(Integer totalTokenCount);
@@ -217,15 +236,17 @@ public Builder clearTotalTokenCount() {
217236
/**
218237
* Setter for promptTokensDetails.
219238
*
220-
* <p>promptTokensDetails: List of modalities that were processed in the request input.
239+
* <p>promptTokensDetails: Output only. A detailed breakdown of the token count for each
240+
* modality in the prompt.
221241
*/
222242
@JsonProperty("promptTokensDetails")
223243
public abstract Builder promptTokensDetails(List<ModalityTokenCount> promptTokensDetails);
224244

225245
/**
226246
* Setter for promptTokensDetails.
227247
*
228-
* <p>promptTokensDetails: List of modalities that were processed in the request input.
248+
* <p>promptTokensDetails: Output only. A detailed breakdown of the token count for each
249+
* modality in the prompt.
229250
*/
230251
@CanIgnoreReturnValue
231252
public Builder promptTokensDetails(ModalityTokenCount... promptTokensDetails) {
@@ -235,7 +256,8 @@ public Builder promptTokensDetails(ModalityTokenCount... promptTokensDetails) {
235256
/**
236257
* Setter for promptTokensDetails builder.
237258
*
238-
* <p>promptTokensDetails: List of modalities that were processed in the request input.
259+
* <p>promptTokensDetails: Output only. A detailed breakdown of the token count for each
260+
* modality in the prompt.
239261
*/
240262
@CanIgnoreReturnValue
241263
public Builder promptTokensDetails(ModalityTokenCount.Builder... promptTokensDetailsBuilders) {
@@ -258,15 +280,17 @@ public Builder clearPromptTokensDetails() {
258280
/**
259281
* Setter for cacheTokensDetails.
260282
*
261-
* <p>cacheTokensDetails: List of modalities that were processed in the cache input.
283+
* <p>cacheTokensDetails: Output only. A detailed breakdown of the token count for each modality
284+
* in the cached content.
262285
*/
263286
@JsonProperty("cacheTokensDetails")
264287
public abstract Builder cacheTokensDetails(List<ModalityTokenCount> cacheTokensDetails);
265288

266289
/**
267290
* Setter for cacheTokensDetails.
268291
*
269-
* <p>cacheTokensDetails: List of modalities that were processed in the cache input.
292+
* <p>cacheTokensDetails: Output only. A detailed breakdown of the token count for each modality
293+
* in the cached content.
270294
*/
271295
@CanIgnoreReturnValue
272296
public Builder cacheTokensDetails(ModalityTokenCount... cacheTokensDetails) {
@@ -276,7 +300,8 @@ public Builder cacheTokensDetails(ModalityTokenCount... cacheTokensDetails) {
276300
/**
277301
* Setter for cacheTokensDetails builder.
278302
*
279-
* <p>cacheTokensDetails: List of modalities that were processed in the cache input.
303+
* <p>cacheTokensDetails: Output only. A detailed breakdown of the token count for each modality
304+
* in the cached content.
280305
*/
281306
@CanIgnoreReturnValue
282307
public Builder cacheTokensDetails(ModalityTokenCount.Builder... cacheTokensDetailsBuilders) {
@@ -342,7 +367,8 @@ public Builder clearResponseTokensDetails() {
342367
/**
343368
* Setter for toolUsePromptTokensDetails.
344369
*
345-
* <p>toolUsePromptTokensDetails: List of modalities that were processed in the tool-use prompt.
370+
* <p>toolUsePromptTokensDetails: Output only. A detailed breakdown by modality of the token
371+
* counts from the results of tool executions, which are provided back to the model as input.
346372
*/
347373
@JsonProperty("toolUsePromptTokensDetails")
348374
public abstract Builder toolUsePromptTokensDetails(
@@ -351,7 +377,8 @@ public abstract Builder toolUsePromptTokensDetails(
351377
/**
352378
* Setter for toolUsePromptTokensDetails.
353379
*
354-
* <p>toolUsePromptTokensDetails: List of modalities that were processed in the tool-use prompt.
380+
* <p>toolUsePromptTokensDetails: Output only. A detailed breakdown by modality of the token
381+
* counts from the results of tool executions, which are provided back to the model as input.
355382
*/
356383
@CanIgnoreReturnValue
357384
public Builder toolUsePromptTokensDetails(ModalityTokenCount... toolUsePromptTokensDetails) {
@@ -361,7 +388,8 @@ public Builder toolUsePromptTokensDetails(ModalityTokenCount... toolUsePromptTok
361388
/**
362389
* Setter for toolUsePromptTokensDetails builder.
363390
*
364-
* <p>toolUsePromptTokensDetails: List of modalities that were processed in the tool-use prompt.
391+
* <p>toolUsePromptTokensDetails: Output only. A detailed breakdown by modality of the token
392+
* counts from the results of tool executions, which are provided back to the model as input.
365393
*/
366394
@CanIgnoreReturnValue
367395
public Builder toolUsePromptTokensDetails(
@@ -386,8 +414,8 @@ public Builder clearToolUsePromptTokensDetails() {
386414
/**
387415
* Setter for trafficType.
388416
*
389-
* <p>trafficType: Traffic type. This shows whether a request consumes Pay-As-You-Go or
390-
* Provisioned Throughput quota.
417+
* <p>trafficType: Output only. The traffic type for this request. This field is not supported
418+
* in Gemini API.
391419
*/
392420
@JsonProperty("trafficType")
393421
public abstract Builder trafficType(TrafficType trafficType);
@@ -405,8 +433,8 @@ public Builder clearTrafficType() {
405433
/**
406434
* Setter for trafficType given a known enum.
407435
*
408-
* <p>trafficType: Traffic type. This shows whether a request consumes Pay-As-You-Go or
409-
* Provisioned Throughput quota.
436+
* <p>trafficType: Output only. The traffic type for this request. This field is not supported
437+
* in Gemini API.
410438
*/
411439
@CanIgnoreReturnValue
412440
public Builder trafficType(TrafficType.Known knownType) {
@@ -416,14 +444,55 @@ public Builder trafficType(TrafficType.Known knownType) {
416444
/**
417445
* Setter for trafficType given a string.
418446
*
419-
* <p>trafficType: Traffic type. This shows whether a request consumes Pay-As-You-Go or
420-
* Provisioned Throughput quota.
447+
* <p>trafficType: Output only. The traffic type for this request. This field is not supported
448+
* in Gemini API.
421449
*/
422450
@CanIgnoreReturnValue
423451
public Builder trafficType(String trafficType) {
424452
return trafficType(new TrafficType(trafficType));
425453
}
426454

455+
/**
456+
* Setter for serviceTier.
457+
*
458+
* <p>serviceTier: Output only. Service tier of the request. This field is not supported in
459+
* Vertex AI.
460+
*/
461+
@JsonProperty("serviceTier")
462+
public abstract Builder serviceTier(ServiceTier serviceTier);
463+
464+
@ExcludeFromGeneratedCoverageReport
465+
abstract Builder serviceTier(Optional<ServiceTier> serviceTier);
466+
467+
/** Clears the value of serviceTier field. */
468+
@ExcludeFromGeneratedCoverageReport
469+
@CanIgnoreReturnValue
470+
public Builder clearServiceTier() {
471+
return serviceTier(Optional.empty());
472+
}
473+
474+
/**
475+
* Setter for serviceTier given a known enum.
476+
*
477+
* <p>serviceTier: Output only. Service tier of the request. This field is not supported in
478+
* Vertex AI.
479+
*/
480+
@CanIgnoreReturnValue
481+
public Builder serviceTier(ServiceTier.Known knownType) {
482+
return serviceTier(new ServiceTier(knownType));
483+
}
484+
485+
/**
486+
* Setter for serviceTier given a string.
487+
*
488+
* <p>serviceTier: Output only. Service tier of the request. This field is not supported in
489+
* Vertex AI.
490+
*/
491+
@CanIgnoreReturnValue
492+
public Builder serviceTier(String serviceTier) {
493+
return serviceTier(new ServiceTier(serviceTier));
494+
}
495+
427496
public abstract UsageMetadata build();
428497
}
429498

0 commit comments

Comments
 (0)