diff --git a/Package.swift b/Package.swift index 3a36f13..13b426a 100644 --- a/Package.swift +++ b/Package.swift @@ -20,7 +20,7 @@ let package = Package( .library(name: "SwiftAgentKitMac", targets: ["SwiftAgentKitMac"]), ], dependencies: [ - .package(url: "https://github.com/ayman3000/LLMProviderKit.git", from: "0.1.0-alpha.42"), + .package(url: "https://github.com/ayman3000/LLMProviderKit.git", from: "0.1.0-alpha.43"), .package(url: "https://github.com/swiftlang/swift-syntax.git", from: "603.0.0"), .package(url: "https://github.com/modelcontextprotocol/swift-sdk.git", from: "0.12.1"), .package(url: "https://github.com/swiftlang/swift-docc-plugin.git", from: "1.4.0"), diff --git a/Sources/SwiftAgentKit/Core/AgentLLMResponse.swift b/Sources/SwiftAgentKit/Core/AgentLLMResponse.swift index 7908057..bdf6f61 100644 --- a/Sources/SwiftAgentKit/Core/AgentLLMResponse.swift +++ b/Sources/SwiftAgentKit/Core/AgentLLMResponse.swift @@ -126,12 +126,17 @@ public struct AgentTokenUsage: Sendable, Equatable { /// Prompt tokens served from the provider's prompt cache (subset of /// promptTokens); nil when the provider didn't report it. public let cachedTokens: Int? + /// Prompt tokens written into the provider's cache (Anthropic), billed + /// above plain input; nil when the provider didn't report it. + public let cacheWriteTokens: Int? - public init(promptTokens: Int?, completionTokens: Int?, totalTokens: Int?, cachedTokens: Int? = nil) { + public init(promptTokens: Int?, completionTokens: Int?, totalTokens: Int?, cachedTokens: Int? = nil, + cacheWriteTokens: Int? = nil) { self.promptTokens = promptTokens self.completionTokens = completionTokens self.totalTokens = totalTokens self.cachedTokens = cachedTokens + self.cacheWriteTokens = cacheWriteTokens } public static func from(_ usage: LLMUsage?) -> AgentTokenUsage? { @@ -140,7 +145,8 @@ public struct AgentTokenUsage: Sendable, Equatable { promptTokens: usage.promptTokens, completionTokens: usage.completionTokens, totalTokens: usage.totalTokens, - cachedTokens: usage.cachedTokens + cachedTokens: usage.cachedTokens, + cacheWriteTokens: usage.cacheWriteTokens ) } } diff --git a/Tests/SwiftAgentKitReplayTests/StreamingUsageTests.swift b/Tests/SwiftAgentKitReplayTests/StreamingUsageTests.swift index 7d62924..a49edec 100644 --- a/Tests/SwiftAgentKitReplayTests/StreamingUsageTests.swift +++ b/Tests/SwiftAgentKitReplayTests/StreamingUsageTests.swift @@ -28,3 +28,13 @@ import SwiftAgentKit #expect(usage?.promptTokens == 123) #expect(usage?.completionTokens == 45) } + +/// Cache writes (Anthropic) reach the host apart from reads, so it can price +/// them at their own rate. +@Test func usageCarriesCacheWritesFromTheProvider() { + let u = AgentTokenUsage.from(LLMUsage(promptTokens: 990, completionTokens: 3, totalTokens: 993, + cachedTokens: 900, cacheWriteTokens: 40)) + #expect(u?.cachedTokens == 900) + #expect(u?.cacheWriteTokens == 40) + #expect(AgentTokenUsage.from(LLMUsage(promptTokens: 5))?.cacheWriteTokens == nil) +}