Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
10 changes: 8 additions & 2 deletions Sources/SwiftAgentKit/Core/AgentLLMResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand All @@ -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
)
}
}
Expand Down
10 changes: 10 additions & 0 deletions Tests/SwiftAgentKitReplayTests/StreamingUsageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading