Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add toolUsePromptTokenCount and toolUsePromptTokensDetails
  • Loading branch information
andrewheard authored and paulb777 committed Sep 22, 2025
commit b86db5655d3523f7d41e1f3aae9e52c6e993c295
13 changes: 13 additions & 0 deletions FirebaseAI/Sources/GenerateContentResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public struct GenerateContentResponse: Sendable {
/// The total number of tokens across the generated response candidates.
public let candidatesTokenCount: Int

/// The number of tokens present in tool-use prompt(s).
public let toolUsePromptTokenCount: Int

/// The number of tokens used by the model's internal "thinking" process.
///
/// For models that support thinking (like Gemini 2.5 Pro and Flash), this represents the actual
Expand All @@ -44,6 +47,9 @@ public struct GenerateContentResponse: Sendable {

/// The breakdown, by modality, of how many tokens are consumed by the candidates
public let candidatesTokensDetails: [ModalityTokenCount]

/// List of modalities that were processed for tool-use request inputs.
public let toolUsePromptTokensDetails: [ModalityTokenCount]
}

/// A list of candidate response content, ordered from best to worst.
Expand Down Expand Up @@ -474,17 +480,21 @@ extension GenerateContentResponse.UsageMetadata: Decodable {
enum CodingKeys: CodingKey {
case promptTokenCount
case candidatesTokenCount
case toolUsePromptTokenCount
case thoughtsTokenCount
case totalTokenCount
case promptTokensDetails
case candidatesTokensDetails
case toolUsePromptTokensDetails
}

public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
promptTokenCount = try container.decodeIfPresent(Int.self, forKey: .promptTokenCount) ?? 0
candidatesTokenCount =
try container.decodeIfPresent(Int.self, forKey: .candidatesTokenCount) ?? 0
toolUsePromptTokenCount =
try container.decodeIfPresent(Int.self, forKey: .toolUsePromptTokenCount) ?? 0
thoughtsTokenCount = try container.decodeIfPresent(Int.self, forKey: .thoughtsTokenCount) ?? 0
totalTokenCount = try container.decodeIfPresent(Int.self, forKey: .totalTokenCount) ?? 0
promptTokensDetails =
Expand All @@ -493,6 +503,9 @@ extension GenerateContentResponse.UsageMetadata: Decodable {
[ModalityTokenCount].self,
forKey: .candidatesTokensDetails
) ?? []
toolUsePromptTokensDetails = try container.decodeIfPresent(
[ModalityTokenCount].self, forKey: .toolUsePromptTokensDetails
) ?? []
}
}

Expand Down